所有的connection都放入 finally块 关闭

web_backend_develope
chenlw 8 years ago
parent 582f55a619
commit 1a2cffab4d

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

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

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

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

Loading…
Cancel
Save