Skip to content

Commit 13b9f58

Browse files
committed
Consistent suppression of get/clearWarnings without target connection
See gh-23346
1 parent f99b2f1 commit 13b9f58

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -340,18 +340,12 @@ else if (method.getName().equals("setReadOnly")) {
340340
this.readOnly = (Boolean) args[0];
341341
return null;
342342
}
343-
else if (method.getName().equals("commit")) {
343+
else if (method.getName().equals("commit") || method.getName().equals("rollback")) {
344344
// Ignore: no statements created yet.
345345
return null;
346346
}
347-
else if (method.getName().equals("rollback")) {
348-
// Ignore: no statements created yet.
349-
return null;
350-
}
351-
else if (method.getName().equals("getWarnings")) {
352-
return null;
353-
}
354-
else if (method.getName().equals("clearWarnings")) {
347+
else if (method.getName().equals("getWarnings") || method.getName().equals("clearWarnings")) {
348+
// Ignore: no warnings to expose yet.
355349
return null;
356350
}
357351
else if (method.getName().equals("close")) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,6 @@ else if (method.getName().equals("isWrapperFor")) {
207207
return true;
208208
}
209209
}
210-
else if (method.getName().equals("getWarnings") || method.getName().equals("clearWarnings")) {
211-
// Avoid creation of target Connection on pre-close cleanup (e.g. in Hibernate Session)
212-
return null;
213-
}
214210
else if (method.getName().equals("close")) {
215211
// Handle close method: only close if not within a transaction.
216212
DataSourceUtils.doReleaseConnection(this.target, this.targetDataSource);
@@ -222,6 +218,10 @@ else if (method.getName().equals("isClosed")) {
222218
}
223219

224220
if (this.target == null) {
221+
if (method.getName().equals("getWarnings") || method.getName().equals("clearWarnings")) {
222+
// Avoid creation of target Connection on pre-close cleanup (e.g. Hibernate Session)
223+
return null;
224+
}
225225
if (this.closed) {
226226
throw new SQLException("Connection handle already closed");
227227
}

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -66,7 +66,7 @@ public class DataSourceTransactionManagerTests {
6666

6767

6868
@Before
69-
public void setUp() throws Exception {
69+
public void setup() throws Exception {
7070
ds = mock(DataSource.class);
7171
con = mock(Connection.class);
7272
given(ds.getConnection()).willReturn(con);
@@ -118,6 +118,7 @@ private void doTestTransactionCommitRestoringAutoCommit(
118118
if (lazyConnection) {
119119
given(con.getAutoCommit()).willReturn(autoCommit);
120120
given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
121+
given(con.getWarnings()).willThrow(new SQLException());
121122
}
122123

123124
if (!lazyConnection || createStatement) {
@@ -144,6 +145,10 @@ protected void doInTransactionWithoutResult(TransactionStatus status) throws Run
144145
tCon.createStatement();
145146
assertEquals(con, new SimpleNativeJdbcExtractor().getNativeConnection(tCon));
146147
}
148+
else {
149+
tCon.getWarnings();
150+
tCon.clearWarnings();
151+
}
147152
}
148153
catch (SQLException ex) {
149154
throw new UncategorizedSQLException("", "", ex);
@@ -211,7 +216,7 @@ private void doTestTransactionRollbackRestoringAutoCommit(
211216
}
212217

213218
final DataSource dsToUse = (lazyConnection ? new LazyConnectionDataSourceProxy(ds) : ds);
214-
tm = new DataSourceTransactionManager(dsToUse);
219+
tm = new DataSourceTransactionManager(dsToUse);
215220
TransactionTemplate tt = new TransactionTemplate(tm);
216221
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
217222
assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
@@ -671,7 +676,6 @@ public void testPropagationRequiresNewWithExistingTransactionAndUnrelatedFailing
671676
SQLException failure = new SQLException();
672677
given(ds2.getConnection()).willThrow(failure);
673678

674-
675679
final TransactionTemplate tt = new TransactionTemplate(tm);
676680
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
677681

@@ -976,12 +980,12 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
976980
ordered.verify(con).setAutoCommit(false);
977981
ordered.verify(con).setAutoCommit(true);
978982
verify(con).close();
979-
980983
}
981984

982985
@Test
983986
public void testTransactionAwareDataSourceProxy() throws Exception {
984987
given(con.getAutoCommit()).willReturn(true);
988+
given(con.getWarnings()).willThrow(new SQLException());
985989

986990
TransactionTemplate tt = new TransactionTemplate(tm);
987991
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
@@ -992,6 +996,9 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
992996
assertEquals(con, DataSourceUtils.getConnection(ds));
993997
TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
994998
try {
999+
Connection tCon = dsProxy.getConnection();
1000+
tCon.getWarnings();
1001+
tCon.clearWarnings();
9951002
assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
9961003
assertEquals(con, new SimpleNativeJdbcExtractor().getNativeConnection(dsProxy.getConnection()));
9971004
// should be ignored
@@ -1249,7 +1256,8 @@ protected void doInTransactionWithoutResult(TransactionStatus status) throws Run
12491256
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
12501257
}
12511258

1252-
@Test public void testTransactionWithPropagationNotSupported() throws Exception {
1259+
@Test
1260+
public void testTransactionWithPropagationNotSupported() throws Exception {
12531261
TransactionTemplate tt = new TransactionTemplate(tm);
12541262
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
12551263
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));

0 commit comments

Comments
 (0)