From 1a2cffab4d22c042c448adf3b606daff9a511834 Mon Sep 17 00:00:00 2001 From: chenlw <874313221@qq.com> Date: Mon, 20 Feb 2017 17:19:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=80=E6=9C=89=E7=9A=84connection=E9=83=BD?= =?UTF-8?q?=E6=94=BE=E5=85=A5=20finally=E5=9D=97=20=E5=85=B3=E9=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/OracleExtractService.java | 10 + .../platform/service/OracleExtractTask.java | 14 +- .../impl/OracleExtractServiceImpl.java | 352 ++++++++++-------- .../thread/ThreadCheckoutStandardOracle.java | 12 +- 4 files changed, 231 insertions(+), 157 deletions(-) diff --git a/src/com/platform/service/OracleExtractService.java b/src/com/platform/service/OracleExtractService.java index 2e841408..bd08b4d4 100644 --- a/src/com/platform/service/OracleExtractService.java +++ b/src/com/platform/service/OracleExtractService.java @@ -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() { diff --git a/src/com/platform/service/OracleExtractTask.java b/src/com/platform/service/OracleExtractTask.java index 05d59d92..8b504be5 100644 --- a/src/com/platform/service/OracleExtractTask.java +++ b/src/com/platform/service/OracleExtractTask.java @@ -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() { diff --git a/src/com/platform/service/impl/OracleExtractServiceImpl.java b/src/com/platform/service/impl/OracleExtractServiceImpl.java index fc136cac..edb54739 100644 --- a/src/com/platform/service/impl/OracleExtractServiceImpl.java +++ b/src/com/platform/service/impl/OracleExtractServiceImpl.java @@ -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 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 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 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); + 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=1"; + // sql日志记录时间: + FileOperateHelper.fileWrite( + Configs.EXTRACT_STANDARD_LOG_LOCALTION + + collectOracle.getName() + ".log", + "\r\n 开始抽取标准表 \r\n " + + DateForm.date2StringBysecond(new Date()) + + "\n\r\n"); + List 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); - 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) { - // 抽取成功 - data.setPayResultLast(Constant.CHECKOUT_STATUS_SEVEN); + 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); + } } - } - if (Constant.CHECKOUT_STATUS_THREE.equals(tmpdata - .getExecResultLast()) - || Constant.CHECKOUT_STATUS_FIVE.equals(tmpdata - .getExecResultLast())) { - // 抽取中 - data.setExecResultLast(Constant.CHECKOUT_STATUS_SIX); + // 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); - 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; diff --git a/src/com/platform/service/thread/ThreadCheckoutStandardOracle.java b/src/com/platform/service/thread/ThreadCheckoutStandardOracle.java index cb8ae0b2..f405a94b 100644 --- a/src/com/platform/service/thread/ThreadCheckoutStandardOracle.java +++ b/src/com/platform/service/thread/ThreadCheckoutStandardOracle.java @@ -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); }