所有的connection都放入 finally块 关闭

This commit is contained in:
chenlw 2017-02-20 17:19:47 +08:00
parent 582f55a619
commit 1a2cffab4d
4 changed files with 233 additions and 159 deletions

View File

@ -2,6 +2,8 @@ package com.platform.service;
import java.sql.SQLException;
import com.base.CustomException;
import com.platform.entities.OracleConnectorParams;
import com.platform.oracle.OracleConnector;
@ -19,6 +21,14 @@ public class OracleExtractService extends Thread implements Runnable {
} catch (CustomException e) {
this.conn = null;
}
finally{
try {
this.conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void run() {

View File

@ -1,6 +1,7 @@
package com.platform.service;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
@ -45,9 +46,10 @@ public class OracleExtractTask extends AbstractOracleExtractTask {
@Override
public void handler() {
Connection conn = null;
// TODO Auto-generated method stub
try {
Connection conn = OracleConnector.connectionBuilder(
conn = OracleConnector.connectionBuilder(
createConnectUrl(), gatherOracleInfo.getUser(),
gatherOracleInfo.getPassword(), oc);
if (null != conn) {
@ -90,11 +92,19 @@ public class OracleExtractTask extends AbstractOracleExtractTask {
.date2StringBysecond(new Date())
+ "]>>>>>>>>>>>>>>>>汇总结束 >>>>>>>>>>>>>>>>>>\r\n\r\n");
OracleExtract.updateDataExtractStatus(oc, 2); // 更新数据库的状态
conn.close(); // 关闭连接
}
} catch (Exception e) {
log.error(Custom4exception.OracleSQL_Except, e);
}
finally{
if(conn != null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 关闭连接
}
}
private String createConnectUrl() {

View File

@ -1,6 +1,7 @@
package com.platform.service.impl;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
@ -56,6 +57,7 @@ public class OracleExtractServiceImpl implements IOracleExtractService {
List<OracleConnectorParams> datainfos, GatherOracleInfo oracleModel)
throws Exception {
boolean isSuccess = false;
Connection conn = null;
try {
// map转 bean(汇总库信息-带tableName的)
// GatherOracleInfo oracleModel = oracleConnect;
@ -64,7 +66,7 @@ public class OracleExtractServiceImpl implements IOracleExtractService {
if (datainfos.size() == 0) {
return false;
}
Connection conn = OracleConnector.connectionBuilder(
conn = OracleConnector.connectionBuilder(
"jdbc:oracle:thin:@" + oracleModel.getIp() + ":"
+ oracleModel.getPort() + ":"
+ oracleModel.getDatabaseName(),
@ -172,6 +174,16 @@ public class OracleExtractServiceImpl implements IOracleExtractService {
} catch (Exception e) {
new CustomException(Custom4exception.OracleSQL_Except, e);
}
finally{
if(conn != null){
try {
conn.close();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
return isSuccess;
}
@ -179,21 +191,38 @@ public class OracleExtractServiceImpl implements IOracleExtractService {
public boolean isConnectTotalOracle(GatherOracleInfo oracleModel)
throws Exception {
boolean isConnect = false;
Connection conn = OracleConnector.connectionBuilder(
"jdbc:oracle:thin:@" + oracleModel.getIp() + ":"
+ oracleModel.getPort() + ":"
+ oracleModel.getDatabaseName(), oracleModel.getUser(),
oracleModel.getPassword(), null);
if (null == conn) {
isConnect = false;
throw new CustomException(Custom4exception.connect_Oracle_Except,
null, oracleModel);
// FileOperateHelper.fileWrite(Configs.EXTRACT_LOG_LOCALTION
// + dataInfolist.get(0).getName(), "创建oracle连接失败: [" + conn +
// "]\r\n");
} else {
isConnect = oracleExtract.testConnect(conn);
conn.close(); // 关闭连接
Exception excep = null;
Connection conn = null;
try {
conn = OracleConnector.connectionBuilder(
"jdbc:oracle:thin:@" + oracleModel.getIp() + ":"
+ oracleModel.getPort() + ":"
+ oracleModel.getDatabaseName(), oracleModel.getUser(),
oracleModel.getPassword(), null);
if (null == conn) {
isConnect = false;
throw new CustomException(Custom4exception.connect_Oracle_Except,
null, oracleModel);
// FileOperateHelper.fileWrite(Configs.EXTRACT_LOG_LOCALTION
// + dataInfolist.get(0).getName(), "创建oracle连接失败: [" + conn +
// "]\r\n");
} else {
isConnect = oracleExtract.testConnect(conn);
}
} catch (Exception e) {
excep = e;
}
finally{
if(conn != null){
try {
conn.close();// 关闭连接
} catch (Exception e2) {
// TODO: handle exception
}
}
if (excep != null) {
throw excep;
}
}
return isConnect;
}
@ -201,7 +230,7 @@ public class OracleExtractServiceImpl implements IOracleExtractService {
@Override
public boolean extractStandardTable(String name,
List<OracleConnectorParams> dataInfolist,
GatherOracleInfo oracleConnect) throws Exception {
GatherOracleInfo oracleConnect) {
boolean isSuccess = false;
// map转 bean(汇总库信息-带tableName的)
GatherOracleInfo oracleModel = oracleConnect;
@ -210,154 +239,169 @@ public class OracleExtractServiceImpl implements IOracleExtractService {
if (datainfos.size() == 0) {
return false;
}
Connection conn = OracleConnector.connectionBuilder(
"jdbc:oracle:thin:@" + oracleModel.getIp() + ":"
+ oracleModel.getPort() + ":"
+ oracleModel.getDatabaseName(), oracleModel.getUser(),
oracleModel.getPassword(), dataInfolist.get(0));
if (null == conn) {
FileOperateHelper.fileWrite(Configs.EXTRACT_LOG_LOCALTION
+ dataInfolist.get(0).getName() + ".log", "创建oracle连接失败: ["
+ conn + "]\r\n");
return false;
}
for (OracleConnectorParams collectOracle : datainfos) {
String replicasName = collectOracle.getName();
DataInfoEntity data = new DataInfoEntity();
try {
if (null != collectOracle.getDataId()
&& !"".equals(collectOracle.getDataId())) {
data.setId(Integer.valueOf(collectOracle.getDataId()));
// 设置为 标准表 抽取中
data.setCheckoutFlag(Constant.CHECKOUTFLAG_SIX);
data.setStandardExtractStatus("1");
dataInfoDao.update(data);
collectOracle.setName("CQ"
+ collectOracle.getName().replace("-", "_"));
String cmd = "kubectl --server "
+ Configs.KUBE_MASTER_ADDRESS
+ " annotate --overwrite rc " + replicasName
+ " standardExtractStatus=1";
// sql日志记录时间
FileOperateHelper.fileWrite(
Configs.EXTRACT_STANDARD_LOG_LOCALTION
+ collectOracle.getName() + ".log",
"\r\n 开始抽取标准表 \r\n "
+ DateForm.date2StringBysecond(new Date())
+ "\n\r\n");
List<String> rList = Constant.ganymedSSH
.execCmdWaitAcquiescent(cmd);
StringBuffer sb = new StringBuffer();
for (String string : rList)
sb.append(string).append("\n");
Configs.CONSOLE_LOGGER.info(sb.toString());
// client.updateOrAddReplicasLabelById(collectOracle.getName(),
// "isExtract", "1");
// //更新oracle汇总状态0标示为未汇总1标示汇总中2标示汇总完成
oracleExtract.createStandardDBLink(conn, collectOracle); // 创建dblink
oracleExtract.createStardardTableSpace(conn, collectOracle,
oracleModel); // 创建表空间
oracleExtract.createOnlyUser(conn, collectOracle,
oracleModel);// 创建 抽取标准表的 用户并授权
DataInfoEntity tmpdata = dataInfoDao.findById(data.getId());
if (null != tmpdata) {
if (Constant.CHECKOUT_STATUS_THREE.equals(tmpdata
.getPayResultLast())
|| Constant.CHECKOUT_STATUS_FIVE.equals(tmpdata
.getPayResultLast())) {
// 抽取中
data.setPayResultLast(Constant.CHECKOUT_STATUS_SIX);
dataInfoDao.update(data);
boolean isExtrac = true;
try {
oracleExtract.extractStandardPayTable(conn,
collectOracle, oracleModel);// 执行抽取
} catch (Exception e) {
// 改回 校验存在的状态
data.setPayResultLast(Constant.CHECKOUT_STATUS_THREE);
dataInfoDao.update(data);
isExtrac = false;
}
if (isExtrac) {
// 抽取成功
data.setPayResultLast(Constant.CHECKOUT_STATUS_SEVEN);
dataInfoDao.update(data);
}
}
if (Constant.CHECKOUT_STATUS_THREE.equals(tmpdata
.getExecResultLast())
|| Constant.CHECKOUT_STATUS_FIVE.equals(tmpdata
.getExecResultLast())) {
// 抽取中
data.setExecResultLast(Constant.CHECKOUT_STATUS_SIX);
dataInfoDao.update(data);
boolean isExtrac = true;
try {
oracleExtract.extractStandardExecTable(conn,
collectOracle, oracleModel);// 执行抽取
} catch (Exception e) {
// 改回 校验存在的状态
data.setExecResultLast(Constant.CHECKOUT_STATUS_THREE);
dataInfoDao.update(data);
isExtrac = false;
}
if (isExtrac) {
data.setExecResultLast(Constant.CHECKOUT_STATUS_SEVEN);
dataInfoDao.update(data);
}
}
// client.updateOrAddReplicasLabelById(collectOracle.getName(),
// "isExtract", "2");
// //更新oracle汇总状态0标示为未汇总1标示汇总中2标示汇总完成
cmd = "kubectl --server " + Configs.KUBE_MASTER_ADDRESS
Connection conn = null;
try {
conn = OracleConnector.connectionBuilder(
"jdbc:oracle:thin:@" + oracleModel.getIp() + ":"
+ oracleModel.getPort() + ":"
+ oracleModel.getDatabaseName(), oracleModel.getUser(),
oracleModel.getPassword(), dataInfolist.get(0));
if (null == conn) {
FileOperateHelper.fileWrite(Configs.EXTRACT_LOG_LOCALTION
+ dataInfolist.get(0).getName() + ".log", "创建oracle连接失败: ["
+ conn + "]\r\n");
return false;
}
for (OracleConnectorParams collectOracle : datainfos) {
String replicasName = collectOracle.getName();
DataInfoEntity data = new DataInfoEntity();
try {
if (null != collectOracle.getDataId()
&& !"".equals(collectOracle.getDataId())) {
data.setId(Integer.valueOf(collectOracle.getDataId()));
// 设置为 标准表 抽取中
data.setCheckoutFlag(Constant.CHECKOUTFLAG_SIX);
data.setStandardExtractStatus("1");
dataInfoDao.update(data);
collectOracle.setName("CQ"
+ collectOracle.getName().replace("-", "_"));
String cmd = "kubectl --server "
+ Configs.KUBE_MASTER_ADDRESS
+ " annotate --overwrite rc " + replicasName
+ " standardExtractStatus=2";
rList = Constant.ganymedSSH.execCmdWaitAcquiescent(cmd);
sb = new StringBuffer();
+ " standardExtractStatus=1";
// sql日志记录时间
FileOperateHelper.fileWrite(
Configs.EXTRACT_STANDARD_LOG_LOCALTION
+ collectOracle.getName() + ".log",
"\r\n 开始抽取标准表 \r\n "
+ DateForm.date2StringBysecond(new Date())
+ "\n\r\n");
List<String> rList = Constant.ganymedSSH
.execCmdWaitAcquiescent(cmd);
StringBuffer sb = new StringBuffer();
for (String string : rList)
sb.append(string).append("\n");
Configs.CONSOLE_LOGGER.info(sb.toString());
data.setCheckoutFlag(Constant.CHECKOUTFLAG_SEVEN);
data.setStandardExtractStatus("2");
dataInfoDao.update(data);
DataInfoEntity tmpSrcData = dataInfoDao.findById(data
.getId());
data.setId(tmpSrcData.getSrcId());
dataInfoDao.update(data);
// client.updateOrAddReplicasLabelById(collectOracle.getName(),
// "isExtract", "1");
// //更新oracle汇总状态0标示为未汇总1标示汇总中2标示汇总完成
oracleExtract.createStandardDBLink(conn, collectOracle); // 创建dblink
oracleExtract.createStardardTableSpace(conn, collectOracle,
oracleModel); // 创建表空间
oracleExtract.createOnlyUser(conn, collectOracle,
oracleModel);// 创建 抽取标准表的 用户并授权
DataInfoEntity tmpdata = dataInfoDao.findById(data.getId());
if (null != tmpdata) {
if (Constant.CHECKOUT_STATUS_THREE.equals(tmpdata
.getPayResultLast())
|| Constant.CHECKOUT_STATUS_FIVE.equals(tmpdata
.getPayResultLast())) {
// 抽取中
data.setPayResultLast(Constant.CHECKOUT_STATUS_SIX);
dataInfoDao.update(data);
boolean isExtrac = true;
try {
oracleExtract.extractStandardPayTable(conn,
collectOracle, oracleModel);// 执行抽取
} catch (Exception e) {
// 改回 校验存在的状态
data.setPayResultLast(Constant.CHECKOUT_STATUS_THREE);
dataInfoDao.update(data);
isExtrac = false;
}
if (isExtrac) {
// 抽取成功
data.setPayResultLast(Constant.CHECKOUT_STATUS_SEVEN);
dataInfoDao.update(data);
}
}
if (Constant.CHECKOUT_STATUS_THREE.equals(tmpdata
.getExecResultLast())
|| Constant.CHECKOUT_STATUS_FIVE.equals(tmpdata
.getExecResultLast())) {
// 抽取中
data.setExecResultLast(Constant.CHECKOUT_STATUS_SIX);
dataInfoDao.update(data);
boolean isExtrac = true;
try {
oracleExtract.extractStandardExecTable(conn,
collectOracle, oracleModel);// 执行抽取
} catch (Exception e) {
// 改回 校验存在的状态
data.setExecResultLast(Constant.CHECKOUT_STATUS_THREE);
dataInfoDao.update(data);
isExtrac = false;
}
if (isExtrac) {
data.setExecResultLast(Constant.CHECKOUT_STATUS_SEVEN);
dataInfoDao.update(data);
}
}
// client.updateOrAddReplicasLabelById(collectOracle.getName(),
// "isExtract", "2");
// //更新oracle汇总状态0标示为未汇总1标示汇总中2标示汇总完成
cmd = "kubectl --server " + Configs.KUBE_MASTER_ADDRESS
+ " annotate --overwrite rc " + replicasName
+ " standardExtractStatus=2";
rList = Constant.ganymedSSH.execCmdWaitAcquiescent(cmd);
sb = new StringBuffer();
for (String string : rList)
sb.append(string).append("\n");
Configs.CONSOLE_LOGGER.info(sb.toString());
data.setCheckoutFlag(Constant.CHECKOUTFLAG_SEVEN);
data.setStandardExtractStatus("2");
dataInfoDao.update(data);
DataInfoEntity tmpSrcData = dataInfoDao.findById(data
.getId());
data.setId(tmpSrcData.getSrcId());
dataInfoDao.update(data);
}
}
}
} catch (Exception e) {
String cmd = "kubectl --server " + Configs.KUBE_MASTER_ADDRESS
+ " annotate --overwrite rc " + replicasName
+ " standardExtractStatus=0";
Constant.ganymedSSH.execCmdWaitAcquiescent(cmd);
data.setStandardExtractStatus("0");
dataInfoDao.update(data);
DataInfoEntity tmpSrcData = dataInfoDao.findById(data.getId());
data.setId(tmpSrcData.getSrcId());
dataInfoDao.update(data);
log.error(Custom4exception.OracleSQL_Except, e);
} finally {
// 去掉保存的当前数据id,
CacheSetCantDelete.removeStandardId(collectOracle.getDataId());
String msg = "抽取标准表结束";
if (!"2".equals(data.getStandardExtractStatus())) {
msg += " 抽取有异常!状态重置为待抽取 ";
} catch (Exception e) {
String cmd = "kubectl --server " + Configs.KUBE_MASTER_ADDRESS
+ " annotate --overwrite rc " + replicasName
+ " standardExtractStatus=0";
Constant.ganymedSSH.execCmdWaitAcquiescent(cmd);
data.setStandardExtractStatus("0");
dataInfoDao.update(data);
DataInfoEntity tmpdata = dataInfoDao.findById(data.getId());
data.setId(tmpdata.getSrcId());
DataInfoEntity tmpSrcData = dataInfoDao.findById(data.getId());
data.setId(tmpSrcData.getSrcId());
dataInfoDao.update(data);
log.error(Custom4exception.OracleSQL_Except, e);
} finally {
// 去掉保存的当前数据id,
CacheSetCantDelete.removeStandardId(collectOracle.getDataId());
String msg = "抽取标准表结束";
if (!"2".equals(data.getStandardExtractStatus())) {
msg += " 抽取有异常!状态重置为待抽取 ";
data.setStandardExtractStatus("0");
dataInfoDao.update(data);
DataInfoEntity tmpdata = dataInfoDao.findById(data.getId());
data.setId(tmpdata.getSrcId());
dataInfoDao.update(data);
}
// sql日志记录时间
// sql日志记录时间
FileOperateHelper.fileWrite(
Configs.EXTRACT_STANDARD_LOG_LOCALTION
+ collectOracle.getName() + ".log",
" " + msg + " >>>>> "
+ DateForm.date2StringBysecond(new Date())
+ "\r\n\r\n\n");
}
}
} catch (Exception e) {
// TODO: handle exception
}
finally{
if(conn != null){
try {
conn.close();// 关闭连接
} catch (Exception e2) {
// TODO: handle exception
}
// sql日志记录时间
// sql日志记录时间
FileOperateHelper.fileWrite(
Configs.EXTRACT_STANDARD_LOG_LOCALTION
+ collectOracle.getName() + ".log",
" " + msg + " >>>>> "
+ DateForm.date2StringBysecond(new Date())
+ "\r\n\r\n\n");
}
}
isSuccess = true;

View File

@ -231,8 +231,9 @@ public class ThreadCheckoutStandardOracle extends Thread {
String logName = tmp.getAreaCode().toLowerCase() + "_"
+ tmp.getSysCode() + "_" + tmp.getDataVersion();
oc.setName(logName);
Connection conn = null;
try {
Connection conn = OracleConnector.connectionBuilder(
conn = OracleConnector.connectionBuilder(
url, Configs.ORACLE_USER, Configs.ORACLE_PSW,
oc);
// 支付--校验
@ -304,6 +305,15 @@ public class ThreadCheckoutStandardOracle extends Thread {
} catch (Exception e) {
log.error(e);
}
finally{
if(conn != null){
try {
conn.close();// 关闭连接
} catch (Exception e2) {
// TODO: handle exception
}
}
}
// 成功 清除 CacheOracleCheckoutEntity 的该条记录
CacheOracleCheckoutEntity.checkRemove(key);
}