diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index a99b2592d97..87220092a3b 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -16,6 +16,7 @@ Add changes here for all PR submitted to the 2.x branch. - [[#6182](https://github.com/apache/incubator-seata/pull/6182)] fix guava-32.0.0-jre.jar zip file is empty in ci - [[#6196](https://github.com/apache/incubator-seata/pull/6196)] fix asf config file format error - [[#6204](https://github.com/apache/incubator-seata/pull/6204)] fix the problem that The incorrect configuration needs to be fixed +- [[#6248](https://github.com/apache/incubator-seata/pull/6248)] fix JDBC resultSet, statement, connection closing order ### optimize: - [[#6031](https://github.com/apache/incubator-seata/pull/6031)] add a check for the existence of the undolog table diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index 309ee68f4e0..fd356729050 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -16,6 +16,7 @@ - [[#6182](https://github.com/apache/incubator-seata/pull/6182)] 修复在ci中guava-32.0.0-jre.jar zip文件为空的问题 - [[#6196](https://github.com/apache/incubator-seata/pull/6196)] 修复asf配置格式错误的问题 - [[#6204](https://github.com/apache/incubator-seata/pull/6204)] 修复错误配置问题 +- [[#6248](https://github.com/apache/incubator-seata/pull/6248)] 修复JDBC resultSet, statement, connection关闭顺序 ### optimize: - [[#6031](https://github.com/apache/incubator-seata/pull/6031)] 添加undo_log表的存在性校验 diff --git a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/BaseTransactionalExecutor.java b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/BaseTransactionalExecutor.java index 93f706a0e6a..ca6d24025d5 100644 --- a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/BaseTransactionalExecutor.java +++ b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/BaseTransactionalExecutor.java @@ -486,8 +486,10 @@ protected SQLUndoLog buildUndoItem(TableRecords beforeImage, TableRecords afterI * @throws SQLException the sql exception */ protected TableRecords buildTableRecords(TableMeta tableMeta, String selectSQL, ArrayList> paramAppenderList) throws SQLException { + PreparedStatement ps = null; ResultSet rs = null; - try (PreparedStatement ps = statementProxy.getConnection().prepareStatement(selectSQL)) { + try { + ps = statementProxy.getConnection().prepareStatement(selectSQL); if (CollectionUtils.isNotEmpty(paramAppenderList)) { for (int i = 0, ts = paramAppenderList.size(); i < ts; i++) { List paramAppender = paramAppenderList.get(i); @@ -499,7 +501,7 @@ protected TableRecords buildTableRecords(TableMeta tableMeta, String selectSQL, rs = ps.executeQuery(); return TableRecords.buildRecords(tableMeta, rs); } finally { - IOUtil.close(rs); + IOUtil.close(rs, ps); } } @@ -524,9 +526,10 @@ protected TableRecords buildTableRecords(Map> pkValuesMap) List needColumns = getNeedColumns(tableMeta.getTableName(), sqlRecognizer.getTableAlias(), insertColumnsUnEscape); needColumns.forEach(selectSQLJoin::add); + PreparedStatement ps = null; ResultSet rs = null; - try (PreparedStatement ps = statementProxy.getConnection().prepareStatement(selectSQLJoin.toString())) { - + try { + ps = statementProxy.getConnection().prepareStatement(selectSQLJoin.toString()); int paramIndex = 1; for (int r = 0; r < rowSize; r++) { for (int c = 0; c < pkColumnNameList.size(); c++) { @@ -539,7 +542,7 @@ protected TableRecords buildTableRecords(Map> pkValuesMap) rs = ps.executeQuery(); return TableRecords.buildRecords(getTableMeta(), rs); } finally { - IOUtil.close(rs); + IOUtil.close(rs, ps); } } diff --git a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/MultiUpdateExecutor.java b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/MultiUpdateExecutor.java index eea71481967..1c6c9427fa5 100644 --- a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/MultiUpdateExecutor.java +++ b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/MultiUpdateExecutor.java @@ -127,13 +127,15 @@ protected TableRecords afterImage(TableRecords beforeImage) throws SQLException } TableMeta tmeta = getTableMeta(sqlRecognizers.get(0).getTableName()); String selectSQL = buildAfterImageSQL(tmeta, beforeImage); + PreparedStatement pst = null; ResultSet rs = null; - try (PreparedStatement pst = statementProxy.getConnection().prepareStatement(selectSQL)) { + try { + pst = statementProxy.getConnection().prepareStatement(selectSQL); SqlGenerateUtils.setParamForPk(beforeImage.pkRows(), getTableMeta().getPrimaryKeyOnlyName(), pst); rs = pst.executeQuery(); return TableRecords.buildRecords(tmeta, rs); } finally { - IOUtil.close(rs); + IOUtil.close(rs, pst); } } diff --git a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/UpdateExecutor.java b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/UpdateExecutor.java index dd31d5f6093..77a955039b3 100644 --- a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/UpdateExecutor.java +++ b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/UpdateExecutor.java @@ -100,13 +100,15 @@ protected TableRecords afterImage(TableRecords beforeImage) throws SQLException return TableRecords.empty(getTableMeta()); } String selectSQL = buildAfterImageSQL(tmeta, beforeImage); + PreparedStatement pst = null; ResultSet rs = null; - try (PreparedStatement pst = statementProxy.getConnection().prepareStatement(selectSQL)) { + try { + pst = statementProxy.getConnection().prepareStatement(selectSQL); SqlGenerateUtils.setParamForPk(beforeImage.pkRows(), getTableMeta().getPrimaryKeyOnlyName(), pst); rs = pst.executeQuery(); return TableRecords.buildRecords(tmeta, rs); } finally { - IOUtil.close(rs); + IOUtil.close(rs, pst); } } diff --git a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/mysql/MySQLInsertOnDuplicateUpdateExecutor.java b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/mysql/MySQLInsertOnDuplicateUpdateExecutor.java index b1c55da0aa5..5c783e86a37 100644 --- a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/mysql/MySQLInsertOnDuplicateUpdateExecutor.java +++ b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/mysql/MySQLInsertOnDuplicateUpdateExecutor.java @@ -271,9 +271,11 @@ public TableRecords buildTableRecords2(TableMeta tableMeta, String selectSQL, Ar if (CollectionUtils.isEmpty(paramAppenderList)) { throw new NotSupportYetException("the SQL statement has no primary key or unique index value, it will not hit any row data.recommend to convert to a normal insert statement"); } + PreparedStatement ps = null; ResultSet rs = null; - try (PreparedStatement ps = statementProxy.getConnection() - .prepareStatement(primaryKeys.isEmpty() ? selectSQL + " FOR UPDATE" : selectSQL)) { + try { + ps = statementProxy.getConnection() + .prepareStatement(primaryKeys.isEmpty() ? selectSQL + " FOR UPDATE" : selectSQL); int paramAppenderCount = 0; int ts = CollectionUtils.isEmpty(paramAppenderList) ? 0 : paramAppenderList.size(); for (int i = 0; i < ts; i++) { @@ -291,7 +293,7 @@ public TableRecords buildTableRecords2(TableMeta tableMeta, String selectSQL, Ar rs = ps.executeQuery(); return TableRecords.buildRecords(tableMeta, rs); } finally { - IOUtil.close(rs); + IOUtil.close(rs, ps); } } diff --git a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/mysql/MySQLUpdateJoinExecutor.java b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/mysql/MySQLUpdateJoinExecutor.java index 47ad51d59cb..267d3f5ab60 100644 --- a/rm-datasource/src/main/java/io/seata/rm/datasource/exec/mysql/MySQLUpdateJoinExecutor.java +++ b/rm-datasource/src/main/java/io/seata/rm/datasource/exec/mysql/MySQLUpdateJoinExecutor.java @@ -154,14 +154,16 @@ protected TableRecords afterImage(TableRecords beforeImage) throws SQLException continue; } String selectSQL = buildAfterImageSQL(joinTable, tableItems[i], tableBeforeImage); + PreparedStatement pst = null; ResultSet rs = null; - try (PreparedStatement pst = statementProxy.getConnection().prepareStatement(selectSQL)) { + try { + pst = statementProxy.getConnection().prepareStatement(selectSQL); setAfterImageSQLPlaceHolderParams(joinConditionParams,tableBeforeImage.pkRows(), getTableMeta(tableItems[i]).getPrimaryKeyOnlyName(), pst); rs = pst.executeQuery(); TableRecords afterImage = TableRecords.buildRecords(getTableMeta(tableItems[i]), rs); afterImagesMap.put(tableItems[i], afterImage); } finally { - IOUtil.close(rs); + IOUtil.close(rs, pst); } } return null; diff --git a/server/src/main/java/io/seata/server/console/impl/db/BranchSessionDBServiceImpl.java b/server/src/main/java/io/seata/server/console/impl/db/BranchSessionDBServiceImpl.java index 4bfe0b3f04a..f17582aa488 100644 --- a/server/src/main/java/io/seata/server/console/impl/db/BranchSessionDBServiceImpl.java +++ b/server/src/main/java/io/seata/server/console/impl/db/BranchSessionDBServiceImpl.java @@ -81,10 +81,13 @@ public PageResult queryByXid(String xid) { String branchSessionSQL = LogStoreSqlsFactory.getLogStoreSqls(dbType).getAllBranchSessionSQL(branchTable, whereCondition); List list = new ArrayList<>(); - ResultSet rs = null; - try (Connection conn = dataSource.getConnection(); - PreparedStatement ps = conn.prepareStatement(branchSessionSQL)) { + Connection conn = null; + PreparedStatement ps = null; + ResultSet rs = null; + try { + conn = dataSource.getConnection(); + ps = conn.prepareStatement(branchSessionSQL); ps.setObject(1, xid); rs = ps.executeQuery(); while (rs.next()) { @@ -93,7 +96,7 @@ public PageResult queryByXid(String xid) { } catch (SQLException e) { throw new StoreException(e); } finally { - IOUtil.close(rs); + IOUtil.close(rs, ps, conn); } return PageResult.success(list, list.size(), 0, 0, 0); } diff --git a/server/src/main/java/io/seata/server/console/impl/db/GlobalLockDBServiceImpl.java b/server/src/main/java/io/seata/server/console/impl/db/GlobalLockDBServiceImpl.java index 740b5212807..37ec54a0027 100644 --- a/server/src/main/java/io/seata/server/console/impl/db/GlobalLockDBServiceImpl.java +++ b/server/src/main/java/io/seata/server/console/impl/db/GlobalLockDBServiceImpl.java @@ -88,12 +88,15 @@ public PageResult query(GlobalLockParam param) { List list = new ArrayList<>(); int count = 0; + Connection conn = null; + PreparedStatement ps = null; + PreparedStatement countPs = null; ResultSet rs = null; ResultSet countRs = null; - - try (Connection conn = dataSource.getConnection(); - PreparedStatement ps = conn.prepareStatement(queryLockSql); - PreparedStatement countPs = conn.prepareStatement(lockCountSql)) { + try { + conn = dataSource.getConnection(); + ps = conn.prepareStatement(queryLockSql); + countPs = conn.prepareStatement(lockCountSql); PageUtil.setObject(ps, sqlParamList); rs = ps.executeQuery(); while (rs.next()) { @@ -107,7 +110,7 @@ public PageResult query(GlobalLockParam param) { } catch (SQLException e) { throw new StoreException(e); } finally { - IOUtil.close(rs, countRs); + IOUtil.close(rs, countRs, ps, countPs, conn); } return PageResult.success(list, count, param.getPageNum(), param.getPageSize()); } diff --git a/server/src/main/java/io/seata/server/console/impl/db/GlobalSessionDBServiceImpl.java b/server/src/main/java/io/seata/server/console/impl/db/GlobalSessionDBServiceImpl.java index 2d553aa8eed..3389bba40d5 100644 --- a/server/src/main/java/io/seata/server/console/impl/db/GlobalSessionDBServiceImpl.java +++ b/server/src/main/java/io/seata/server/console/impl/db/GlobalSessionDBServiceImpl.java @@ -96,12 +96,15 @@ public PageResult query(GlobalSessionParam param) { int count = 0; + Connection conn = null; + PreparedStatement ps = null; + PreparedStatement countPs = null; ResultSet rs = null; ResultSet countRs = null; - - try (Connection conn = dataSource.getConnection(); - PreparedStatement ps = conn.prepareStatement(querySessionSql); - PreparedStatement countPs = conn.prepareStatement(sessionCountSql)) { + try { + conn = dataSource.getConnection(); + ps = conn.prepareStatement(querySessionSql); + countPs = conn.prepareStatement(sessionCountSql); PageUtil.setObject(ps, sqlParamList); rs = ps.executeQuery(); while (rs.next()) { @@ -122,7 +125,7 @@ public PageResult query(GlobalSessionParam param) { } catch (SQLException e) { throw new StoreException(e); } finally { - IOUtil.close(rs, countRs); + IOUtil.close(rs, countRs, ps, countPs, conn); } return PageResult.success(list, count, param.getPageNum(), param.getPageSize()); } diff --git a/server/src/test/java/io/seata/server/lock/db/DataBaseLockManagerImplTest.java b/server/src/test/java/io/seata/server/lock/db/DataBaseLockManagerImplTest.java index c27df6550e6..c934d0a3480 100644 --- a/server/src/test/java/io/seata/server/lock/db/DataBaseLockManagerImplTest.java +++ b/server/src/test/java/io/seata/server/lock/db/DataBaseLockManagerImplTest.java @@ -68,9 +68,10 @@ public static void start(ApplicationContext context){ private static void prepareTable(BasicDataSource dataSource) { Connection conn = null; + Statement s = null; try { conn = dataSource.getConnection(); - Statement s = conn.createStatement(); + s = conn.createStatement(); try { s.execute("drop table lock_table"); } catch (Exception e) { @@ -81,7 +82,7 @@ private static void prepareTable(BasicDataSource dataSource) { } catch (Exception e) { e.printStackTrace(); } finally { - IOUtil.close(conn); + IOUtil.close(s, conn); } } @@ -157,13 +158,14 @@ public void re_acquireLock() throws TransactionException, SQLException { Assertions.assertFalse(lockManager.acquireLock(branchSession3)); String delSql = "delete from lock_table where xid in( 'abc-123:65867978' , 'abc-123:65867978' , 'abc-123:5678789' )" ; - Connection conn = null; + Connection conn = null; + Statement stmt = null; try { conn = dataSource.getConnection(); - - conn.createStatement().execute(delSql); + stmt = conn.createStatement(); + stmt.execute(delSql); } finally { - IOUtil.close(conn); + IOUtil.close(stmt, conn); } } @@ -253,12 +255,13 @@ public void isLockable() throws TransactionException, SQLException { String delSql = "delete from lock_table where xid in( 'abc-123:56877898' , 'abc-123:56877898' , 'abc-123:4575614354' )" ; Connection conn = null; + Statement stmt = null; try { conn = dataSource.getConnection(); - - conn.createStatement().execute(delSql); + stmt = conn.createStatement(); + stmt.execute(delSql); } finally { - IOUtil.close(conn); + IOUtil.close(stmt, conn); } } diff --git a/server/src/test/java/io/seata/server/lock/db/DataBaseLockStoreDAOTest.java b/server/src/test/java/io/seata/server/lock/db/DataBaseLockStoreDAOTest.java index cb9f6f493cc..e5c95ab092f 100644 --- a/server/src/test/java/io/seata/server/lock/db/DataBaseLockStoreDAOTest.java +++ b/server/src/test/java/io/seata/server/lock/db/DataBaseLockStoreDAOTest.java @@ -61,20 +61,21 @@ public static void start(ApplicationContext context){ private static void prepareTable(BasicDataSource dataSource) { Connection conn = null; + Statement stmt = null; try { conn = dataSource.getConnection(); - Statement s = conn.createStatement(); + stmt = conn.createStatement(); try { - s.execute("drop table lock_table"); + stmt.execute("drop table lock_table"); } catch (Exception e) { } - s.execute("CREATE TABLE lock_table ( xid varchar(96) , transaction_id long , branch_id long, resource_id varchar(32) ,table_name varchar(32) ,pk varchar(32) , row_key varchar(128) primary key not null , status integer , gmt_create TIMESTAMP(6) ,gmt_modified TIMESTAMP(6) ) "); + stmt.execute("CREATE TABLE lock_table ( xid varchar(96) , transaction_id long , branch_id long, resource_id varchar(32) ,table_name varchar(32) ,pk varchar(32) , row_key varchar(128) primary key not null , status integer , gmt_create TIMESTAMP(6) ,gmt_modified TIMESTAMP(6) ) "); System.out.println("create table lock_table success."); } catch (Exception e) { e.printStackTrace(); } finally { - IOUtil.close(conn); + IOUtil.close(stmt, conn); } } @@ -98,16 +99,17 @@ public void test_acquireLocks() throws SQLException { String sql = "select * from lock_table where xid = 'abc-123:123' and table_name = 't' and row_key in ('abc-0','abc-1','abc-2')" ; Connection conn = null; + ResultSet rs = null; try{ conn = dataSource.getConnection(); - ResultSet rs = conn.createStatement().executeQuery(sql); - if(rs.next()){ + rs = conn.createStatement().executeQuery(sql); + if (rs.next()) { Assertions.assertTrue(true); - }else { + } else { Assertions.fail(); } } finally { - IOUtil.close(conn); + IOUtil.close(rs, conn); } Assertions.assertTrue(dataBaseLockStoreDAO.unLock(lockDOs)); @@ -135,16 +137,17 @@ public void test_re_acquireLocks() throws SQLException { String sql = "select * from lock_table where xid = 'abc-123:123' and table_name = 't' and row_key in ('abc-0','abc-1','abc-2')" ; Connection conn = null; + ResultSet rs = null; try{ conn = dataSource.getConnection(); - ResultSet rs = conn.createStatement().executeQuery(sql); - if(rs.next()){ + rs = conn.createStatement().executeQuery(sql); + if (rs.next()) { Assertions.assertTrue(true); - }else { + } else { Assertions.fail(); } } finally { - IOUtil.close(conn); + IOUtil.close(rs, conn); } //lock again @@ -174,12 +177,13 @@ public void tes_unLocks() throws SQLException { String sql = "select * from lock_table where xid = 'abc-456:123' and table_name = 't' and row_key in ('abc-0','abc-1','abc-2')" ; Connection conn = null; + ResultSet rs = null; try{ conn = dataSource.getConnection(); - ResultSet rs = conn.createStatement().executeQuery(sql); - if(rs.next()){ + rs = conn.createStatement().executeQuery(sql); + if (rs.next()) { Assertions.assertTrue(true); - }else { + } else { Assertions.fail(); } rs.close(); @@ -188,14 +192,13 @@ public void tes_unLocks() throws SQLException { Assertions.assertTrue(dataBaseLockStoreDAO.unLock(lockDOs)); rs = conn.createStatement().executeQuery(sql); - if(rs.next()){ + if (rs.next()) { Assertions.fail(); - }else { + } else { Assertions.assertTrue(true); } - } finally { - IOUtil.close(conn); + IOUtil.close(rs, conn); } @@ -244,16 +247,17 @@ public void test_isLockable_cannot() throws SQLException { String sql = "select * from lock_table where xid = 'abc-123:222' and table_name = 't' and row_key in ('abc-0','abc-1','abc-2')" ; Connection conn = null; + ResultSet rs = null; try{ conn = dataSource.getConnection(); - ResultSet rs = conn.createStatement().executeQuery(sql); - if(rs.next()){ + rs = conn.createStatement().executeQuery(sql); + if (rs.next()) { Assertions.assertTrue(true); - }else { + } else { Assertions.fail(); } } finally { - IOUtil.close(conn); + IOUtil.close(rs, conn); } List lockDOs_2 = new ArrayList<>(); diff --git a/server/src/test/java/io/seata/server/session/db/DataBaseSessionManagerTest.java b/server/src/test/java/io/seata/server/session/db/DataBaseSessionManagerTest.java index 0488e8479b6..9a5713f7260 100644 --- a/server/src/test/java/io/seata/server/session/db/DataBaseSessionManagerTest.java +++ b/server/src/test/java/io/seata/server/session/db/DataBaseSessionManagerTest.java @@ -89,9 +89,10 @@ public static void start(ApplicationContext context) throws Exception { private static void prepareTable(BasicDataSource dataSource) { Connection conn = null; + Statement s = null; try { conn = dataSource.getConnection(); - Statement s = conn.createStatement(); + s = conn.createStatement(); try { s.execute("drop table global_table"); } catch (Exception e) { @@ -109,7 +110,7 @@ private static void prepareTable(BasicDataSource dataSource) { } catch (Exception e) { e.printStackTrace(); } finally { - IOUtil.close(conn); + IOUtil.close(s, conn); } } diff --git a/server/src/test/java/io/seata/server/store/db/LogStoreDataBaseDAOTest.java b/server/src/test/java/io/seata/server/store/db/LogStoreDataBaseDAOTest.java index 9e463b70bfd..37044ae5880 100644 --- a/server/src/test/java/io/seata/server/store/db/LogStoreDataBaseDAOTest.java +++ b/server/src/test/java/io/seata/server/store/db/LogStoreDataBaseDAOTest.java @@ -64,9 +64,10 @@ public static void start(ApplicationContext context){ private static void prepareTable(BasicDataSource dataSource) { Connection conn = null; + Statement s = null; try { conn = dataSource.getConnection(); - Statement s = conn.createStatement(); + s = conn.createStatement(); try { s.execute("drop table global_table"); } catch (Exception e) { @@ -86,7 +87,7 @@ private static void prepareTable(BasicDataSource dataSource) { } catch (Exception e) { e.printStackTrace(); } finally { - IOUtil.close(conn); + IOUtil.close(s, conn); } } @@ -123,12 +124,14 @@ public void queryGlobalTransactionDO_by_xid() throws SQLException { String delSql = "delete from global_table where xid= 'abc-123:978786'"; Connection conn = null; - try{ + Statement stmt = null; + try { conn = dataSource.getConnection(); //delete - conn.createStatement().execute(delSql); - }finally { - IOUtil.close(conn); + stmt = conn.createStatement(); + stmt.execute(delSql); + } finally { + IOUtil.close(stmt, conn); } } @@ -165,12 +168,14 @@ public void queryGlobalTransactionDO_by_transaction_id() throws SQLException { String delSql = "delete from global_table where xid= 'abc-123:978786'"; Connection conn = null; - try{ + Statement stmt = null; + try { conn = dataSource.getConnection(); //delete - conn.createStatement().execute(delSql); - }finally { - IOUtil.close(conn); + stmt = conn.createStatement(); + stmt.execute(delSql); + } finally { + IOUtil.close(stmt, conn); } } @@ -236,11 +241,13 @@ public void queryGlobalTransactionDO_by_statuses() throws SQLException { String delSql = "delete from global_table where xid in ('abc-123:1267', 'abc-123:6978', 'abc-123:5657')"; Connection conn = null; - try{ + Statement stmt = null; + try { conn = dataSource.getConnection(); - conn.createStatement().execute(delSql); - }finally { - IOUtil.close(conn); + stmt = conn.createStatement(); + stmt.execute(delSql); + } finally { + IOUtil.close(stmt, conn); } } @@ -303,11 +310,13 @@ public void queryGlobalTransactionDO_by_statuses_limit() throws SQLException { String delSql = "delete from global_table where xid in ('abc-123:1267', 'abc-123:6978', 'abc-123:5657')"; Connection conn = null; - try{ + Statement stmt = null; + try { conn = dataSource.getConnection(); - conn.createStatement().execute(delSql); - }finally { - IOUtil.close(conn); + stmt = conn.createStatement(); + stmt.execute(delSql); + } finally { + IOUtil.close(stmt, conn); } } @@ -331,19 +340,22 @@ public void insertGlobalTransactionDO() throws SQLException { String sql = "select * from global_table where xid= 'abc-123:333'"; String delSql = "delete from global_table where xid= 'abc-123:333'"; Connection conn = null; - try{ + Statement stmt = null; + ResultSet rs = null; + try { conn = dataSource.getConnection(); - ResultSet rs = conn.createStatement().executeQuery(sql); - if(rs.next()){ + stmt = conn.createStatement(); + rs = stmt.executeQuery(sql); + if (rs.next()) { Assertions.assertTrue(true); - }else{ + } else{ Assertions.fail(); } - conn.createStatement().execute(delSql); + stmt.execute(delSql); - }finally { - IOUtil.close(conn); + } finally { + IOUtil.close(rs, stmt, conn); } } @@ -366,9 +378,11 @@ public void updateGlobalTransactionDO() throws SQLException { String sql = "select * from global_table where xid= 'abc-123:222'"; String delSql = "delete from global_table where xid= 'abc-123:222'"; Connection conn = null; - try{ + Statement stmt = null; + try { conn = dataSource.getConnection(); - ResultSet rs = conn.createStatement().executeQuery(sql); + stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(sql); if(rs.next()){ Assertions.assertTrue(true); Assertions.assertEquals(1, rs.getInt("status")); @@ -381,20 +395,20 @@ public void updateGlobalTransactionDO() throws SQLException { globalTransactionDO.setStatus(2); Assertions.assertTrue(logStoreDataBaseDAO.updateGlobalTransactionDO(globalTransactionDO)); - rs = conn.createStatement().executeQuery(sql); - if(rs.next()){ + rs = stmt.executeQuery(sql); + if (rs.next()) { Assertions.assertTrue(true); Assertions.assertEquals(2, rs.getInt("status")); - }else{ + } else{ Assertions.fail(); } rs.close(); //delete - conn.createStatement().execute(delSql); + stmt.execute(delSql); - }finally { - IOUtil.close(conn); + } finally { + IOUtil.close(stmt, conn); } } @@ -418,13 +432,15 @@ public void updateGlobalTransactionDOExpected() throws SQLException { String sql = "select * from global_table where xid= 'abc-123:222'"; String delSql = "delete from global_table where xid= 'abc-123:222'"; Connection conn = null; - try{ + Statement stmt = null; + try { conn = dataSource.getConnection(); - ResultSet rs = conn.createStatement().executeQuery(sql); - if(rs.next()){ + stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(sql); + if (rs.next()) { Assertions.assertTrue(true); Assertions.assertEquals(1, rs.getInt("status")); - }else{ + } else { Assertions.assertTrue(false); } rs.close(); @@ -433,20 +449,20 @@ public void updateGlobalTransactionDOExpected() throws SQLException { globalTransactionDO.setStatus(2); Assertions.assertTrue(logStoreDataBaseDAO.updateGlobalTransactionDO(globalTransactionDO,1)); - rs = conn.createStatement().executeQuery(sql); - if(rs.next()){ + rs = stmt.executeQuery(sql); + if (rs.next()) { Assertions.assertTrue(true); Assertions.assertEquals(2, rs.getInt("status")); - }else{ + } else{ Assertions.assertTrue(false); } rs.close(); //delete - conn.createStatement().execute(delSql); + stmt.execute(delSql); - }finally { - IOUtil.close(conn); + } finally { + IOUtil.close(stmt, conn); } } @@ -474,17 +490,19 @@ public void deleteGlobalTransactionDO() throws SQLException { String sql = "select * from global_table where xid= 'abc-123:555'"; Connection conn = null; - try{ + Statement stmt = null; + try { conn = dataSource.getConnection(); - ResultSet rs = conn.createStatement().executeQuery(sql); - if(rs.next()){ + stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(sql); + if (rs.next()) { Assertions.fail(); - }else{ + } else{ Assertions.assertTrue(true); } rs.close(); - }finally { - IOUtil.close(conn); + } finally { + IOUtil.close(stmt, conn); } } @@ -531,23 +549,25 @@ public void queryBranchTransactionDO() throws SQLException { Assertions.assertTrue(CollectionUtils.isNotEmpty(rets)); Assertions.assertEquals(2, rets.size()); - if(78563453 == rets.get(0).getBranchId() && 345465676 == rets.get(1).getBranchId()){ + if (78563453 == rets.get(0).getBranchId() && 345465676 == rets.get(1).getBranchId()) { Assertions.assertTrue(true); - }else if(78563453 == rets.get(1).getBranchId() && 345465676 == rets.get(0).getBranchId()){ + } else if (78563453 == rets.get(1).getBranchId() && 345465676 == rets.get(0).getBranchId()) { Assertions.assertTrue(true); - }else { + } else { Assertions.fail(); } String delSql = "delete from branch_table where xid= 'abc-123:6789' "; Connection conn = null; - try{ + Statement stmt = null; + try { conn = dataSource.getConnection(); - conn.createStatement().execute(delSql); + stmt = conn.createStatement(); + stmt.execute(delSql); - }finally { - IOUtil.close(conn); + } finally { + IOUtil.close(stmt, conn); } } @@ -573,19 +593,21 @@ public void insertBranchTransactionDO() throws SQLException { String sql = "select * from branch_table where xid= 'abc-123:7777' and branch_id = 1234508"; String delSql = "delete from branch_table where xid= 'abc-123:7777' and branch_id = 1234508"; Connection conn = null; - try{ + Statement stmt = null; + try { conn = dataSource.getConnection(); - ResultSet rs = conn.createStatement().executeQuery(sql); - if(rs.next()){ + stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(sql); + if (rs.next()) { Assertions.assertTrue(true); - }else { + } else { Assertions.fail(); } - conn.createStatement().execute(delSql); - }finally { - IOUtil.close(conn); + stmt.execute(delSql); + } finally { + IOUtil.close(stmt, conn); } } @@ -614,20 +636,22 @@ public void updateBranchTransactionDO() throws SQLException { String sql = "select * from branch_table where xid= 'abc-123:8888' and branch_id = 343434318"; String delSql = "delete from branch_table where xid= 'abc-123:8888' and branch_id = 343434318"; Connection conn = null; - try{ + Statement stmt = null; + try { conn = dataSource.getConnection(); - ResultSet rs = conn.createStatement().executeQuery(sql); - if(rs.next()){ + stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(sql); + if (rs.next()) { Assertions.assertTrue(true); Assertions.assertEquals(3, rs.getInt("status")); - }else { + } else { Assertions.fail(); } - conn.createStatement().execute(delSql); - }finally { - IOUtil.close(conn); + stmt.execute(delSql); + } finally { + IOUtil.close(stmt, conn); } } @@ -653,13 +677,15 @@ public void deleteBranchTransactionDO() throws SQLException { String sql = "select * from branch_table where xid= 'abc-123:9999' and branch_id = 34567798"; String delSql = "delete from branch_table where xid= 'abc-123:9999' and branch_id = 34567798"; Connection conn = null; - try{ + Statement stmt = null; + try { conn = dataSource.getConnection(); - ResultSet rs = conn.createStatement().executeQuery(sql); - if(rs.next()){ + stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(sql); + if (rs.next()) { Assertions.assertTrue(true); - }else { + } else { Assertions.fail(); } rs.close(); @@ -667,16 +693,16 @@ public void deleteBranchTransactionDO() throws SQLException { //delete logStoreDataBaseDAO.deleteBranchTransactionDO(branchTransactionDO); - rs = conn.createStatement().executeQuery(sql); - if(rs.next()){ + rs = stmt.executeQuery(sql); + if (rs.next()) { Assertions.fail(); - }else { + } else { Assertions.assertTrue(true); } rs.close(); - }finally { - IOUtil.close(conn); + } finally { + IOUtil.close(stmt, conn); } }