Skip to content

Commit

Permalink
bugfix: JDBC resultSet, statement and connection closing order (#6248)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbxyyx authored Jan 8, 2024
1 parent 2433b8f commit cd5d816
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 141 deletions.
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -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表的存在性校验
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,10 @@ protected SQLUndoLog buildUndoItem(TableRecords beforeImage, TableRecords afterI
* @throws SQLException the sql exception
*/
protected TableRecords buildTableRecords(TableMeta tableMeta, String selectSQL, ArrayList<List<Object>> 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<Object> paramAppender = paramAppenderList.get(i);
Expand All @@ -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);
}
}

Expand All @@ -524,9 +526,10 @@ protected TableRecords buildTableRecords(Map<String, List<Object>> pkValuesMap)
List<String> 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++) {
Expand All @@ -539,7 +542,7 @@ protected TableRecords buildTableRecords(Map<String, List<Object>> pkValuesMap)
rs = ps.executeQuery();
return TableRecords.buildRecords(getTableMeta(), rs);
} finally {
IOUtil.close(rs);
IOUtil.close(rs, ps);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,13 @@ public PageResult<BranchSessionVO> queryByXid(String xid) {
String branchSessionSQL = LogStoreSqlsFactory.getLogStoreSqls(dbType).getAllBranchSessionSQL(branchTable, whereCondition);

List<BranchSessionVO> 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()) {
Expand All @@ -93,7 +96,7 @@ public PageResult<BranchSessionVO> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ public PageResult<GlobalLockVO> query(GlobalLockParam param) {
List<GlobalLockVO> 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()) {
Expand All @@ -107,7 +110,7 @@ public PageResult<GlobalLockVO> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@ public PageResult<GlobalSessionVO> 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()) {
Expand All @@ -122,7 +125,7 @@ public PageResult<GlobalSessionVO> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -81,7 +82,7 @@ private static void prepareTable(BasicDataSource dataSource) {
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtil.close(conn);
IOUtil.close(s, conn);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
Loading

0 comments on commit cd5d816

Please sign in to comment.