Skip to content

Commit c64dae3

Browse files
committed
Avoid getTargetConnection call on transaction-aware Connection close
Closes gh-34484
1 parent 559ea6c commit c64dae3

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -439,7 +439,11 @@ private static boolean connectionEquals(ConnectionHolder conHolder, Connection p
439439
public static Connection getTargetConnection(Connection con) {
440440
Connection conToUse = con;
441441
while (conToUse instanceof ConnectionProxy connectionProxy) {
442-
conToUse = connectionProxy.getTargetConnection();
442+
Connection targetCon = connectionProxy.getTargetConnection();
443+
if (targetCon == conToUse) {
444+
break;
445+
}
446+
conToUse = targetCon;
443447
}
444448
return conToUse;
445449
}

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -210,13 +210,23 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
210210
sb.append('[').append(this.target).append(']');
211211
}
212212
else {
213-
sb.append(" from DataSource [").append(this.targetDataSource).append(']');
213+
sb.append("from DataSource [").append(this.targetDataSource).append(']');
214214
}
215215
return sb.toString();
216216
}
217217
case "close" -> {
218218
// Handle close method: only close if not within a transaction.
219-
DataSourceUtils.doReleaseConnection(this.target, this.targetDataSource);
219+
if (this.target != null) {
220+
ConnectionHolder conHolder = (ConnectionHolder)
221+
TransactionSynchronizationManager.getResource(this.targetDataSource);
222+
if (conHolder != null && conHolder.hasConnection() && conHolder.getConnection() == this.target) {
223+
// It's the transactional Connection: Don't close it.
224+
conHolder.released();
225+
}
226+
else {
227+
DataSourceUtils.doCloseConnection(this.target, this.targetDataSource);
228+
}
229+
}
220230
this.closed = true;
221231
return null;
222232
}

spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceTransactionManagerTests.java

+43-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -72,7 +72,7 @@ public class DataSourceTransactionManagerTests {
7272

7373
protected DataSource ds = mock();
7474

75-
protected Connection con = mock();
75+
protected ConnectionProxy con = mock();
7676

7777
protected DataSourceTransactionManager tm;
7878

@@ -81,6 +81,7 @@ public class DataSourceTransactionManagerTests {
8181
void setup() throws Exception {
8282
tm = createTransactionManager(ds);
8383
given(ds.getConnection()).willReturn(con);
84+
given(con.getTargetConnection()).willThrow(new UnsupportedOperationException());
8485
}
8586

8687
protected DataSourceTransactionManager createTransactionManager(DataSource ds) {
@@ -1073,9 +1074,9 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
10731074
Connection tCon = dsProxy.getConnection();
10741075
tCon.getWarnings();
10751076
tCon.clearWarnings();
1076-
assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
1077+
assertThat(((ConnectionProxy) tCon).getTargetConnection()).isEqualTo(con);
10771078
// should be ignored
1078-
dsProxy.getConnection().close();
1079+
tCon.close();
10791080
}
10801081
catch (SQLException ex) {
10811082
throw new UncategorizedSQLException("", "", ex);
@@ -1109,9 +1110,9 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
11091110
Connection tCon = dsProxy.getConnection();
11101111
assertThatExceptionOfType(SQLException.class).isThrownBy(tCon::getWarnings);
11111112
tCon.clearWarnings();
1112-
assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
1113+
assertThat(((ConnectionProxy) tCon).getTargetConnection()).isEqualTo(con);
11131114
// should be ignored
1114-
dsProxy.getConnection().close();
1115+
tCon.close();
11151116
}
11161117
catch (SQLException ex) {
11171118
throw new UncategorizedSQLException("", "", ex);
@@ -1127,6 +1128,42 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
11271128
verify(con).close();
11281129
}
11291130

1131+
@Test
1132+
void testTransactionAwareDataSourceProxyWithEarlyConnection() throws Exception {
1133+
given(ds.getConnection()).willReturn(mock(Connection.class), con);
1134+
given(con.getAutoCommit()).willReturn(true);
1135+
given(con.getWarnings()).willThrow(new SQLException());
1136+
1137+
TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
1138+
dsProxy.setLazyTransactionalConnections(false);
1139+
Connection tCon = dsProxy.getConnection();
1140+
1141+
TransactionTemplate tt = new TransactionTemplate(tm);
1142+
assertThat(TransactionSynchronizationManager.hasResource(ds)).isFalse();
1143+
tt.execute(new TransactionCallbackWithoutResult() {
1144+
@Override
1145+
protected void doInTransactionWithoutResult(TransactionStatus status) {
1146+
// something transactional
1147+
assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
1148+
try {
1149+
// should close the early Connection obtained before the transaction
1150+
tCon.close();
1151+
}
1152+
catch (SQLException ex) {
1153+
throw new UncategorizedSQLException("", "", ex);
1154+
}
1155+
}
1156+
});
1157+
1158+
assertThat(TransactionSynchronizationManager.hasResource(ds)).isFalse();
1159+
1160+
InOrder ordered = inOrder(con);
1161+
ordered.verify(con).setAutoCommit(false);
1162+
ordered.verify(con).commit();
1163+
ordered.verify(con).setAutoCommit(true);
1164+
verify(con).close();
1165+
}
1166+
11301167
@Test
11311168
void testTransactionAwareDataSourceProxyWithSuspension() throws Exception {
11321169
given(con.getAutoCommit()).willReturn(true);

0 commit comments

Comments
 (0)