diff --git a/hologresjdbcwriter/src/main/java/com/alibaba/datax/plugin/writer/hologresjdbcwriter/BaseWriter.java b/hologresjdbcwriter/src/main/java/com/alibaba/datax/plugin/writer/hologresjdbcwriter/BaseWriter.java index 89df08b10f..6f562cf072 100644 --- a/hologresjdbcwriter/src/main/java/com/alibaba/datax/plugin/writer/hologresjdbcwriter/BaseWriter.java +++ b/hologresjdbcwriter/src/main/java/com/alibaba/datax/plugin/writer/hologresjdbcwriter/BaseWriter.java @@ -191,32 +191,34 @@ public List split(Configuration originalConfig, // 一般来说,是需要推迟到 task 中进行post 的执行(单表情况例外) public void post(Configuration originalConfig) { - String username = originalConfig.getString(Key.USERNAME); - String password = originalConfig.getString(Key.PASSWORD); - - String jdbcUrl = originalConfig.getString(Key.JDBC_URL); + try { + String username = originalConfig.getString(Key.USERNAME); + String password = originalConfig.getString(Key.PASSWORD); - String table = originalConfig.getString(Key.TABLE); + String jdbcUrl = originalConfig.getString(Key.JDBC_URL); - List postSqls = originalConfig.getList(Key.POST_SQL, - String.class); - List renderedPostSqls = WriterUtil.renderPreOrPostSqls( - postSqls, table); + String table = originalConfig.getString(Key.TABLE); - if (null != renderedPostSqls && !renderedPostSqls.isEmpty()) { - // 说明有 postSql 配置,则此处删除掉 - originalConfig.remove(Key.POST_SQL); - String tempJdbcUrl = jdbcUrl.replace("postgresql", "hologres"); - Connection conn = DBUtil.getConnection(this.dataBaseType, - tempJdbcUrl, username, password); + List postSqls = originalConfig.getList(Key.POST_SQL, + String.class); + List renderedPostSqls = WriterUtil.renderPreOrPostSqls( + postSqls, table); - LOG.info( - "Begin to execute postSqls:[{}]. context info:{}.", - StringUtils.join(renderedPostSqls, ";"), tempJdbcUrl); - WriterUtil.executeSqls(conn, renderedPostSqls, tempJdbcUrl, dataBaseType); - DBUtil.closeDBResources(null, null, conn); + if (null != renderedPostSqls && !renderedPostSqls.isEmpty()) { + // 说明有 postSql 配置,则此处删除掉 + originalConfig.remove(Key.POST_SQL); + String tempJdbcUrl = jdbcUrl.replace("postgresql", "hologres"); + try (Connection conn = DriverManager.getConnection( + tempJdbcUrl, username, password)) { + LOG.info( + "Begin to execute postSqls:[{}]. context info:{}.", + StringUtils.join(renderedPostSqls, ";"), tempJdbcUrl); + WriterUtil.executeSqls(conn, renderedPostSqls, tempJdbcUrl, dataBaseType); + } + } + } catch (SQLException e) { + throw DataXException.asDataXException(DBUtilErrorCode.SQL_EXECUTE_FAIL, e); } - } public void destroy(Configuration originalConfig) {