Skip to content

Commit 5235721

Browse files
committed
Consistent suppression of get/clearWarnings without target connection
See gh-23346
1 parent 24b9bb9 commit 5235721

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
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.
@@ -348,18 +348,12 @@ else if (method.getName().equals("setReadOnly")) {
348348
this.readOnly = (Boolean) args[0];
349349
return null;
350350
}
351-
else if (method.getName().equals("commit")) {
351+
else if (method.getName().equals("commit") || method.getName().equals("rollback")) {
352352
// Ignore: no statements created yet.
353353
return null;
354354
}
355-
else if (method.getName().equals("rollback")) {
356-
// Ignore: no statements created yet.
357-
return null;
358-
}
359-
else if (method.getName().equals("getWarnings")) {
360-
return null;
361-
}
362-
else if (method.getName().equals("clearWarnings")) {
355+
else if (method.getName().equals("getWarnings") || method.getName().equals("clearWarnings")) {
356+
// Ignore: no warnings to expose yet.
363357
return null;
364358
}
365359
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
@@ -203,10 +203,6 @@ else if (method.getName().equals("isWrapperFor")) {
203203
return true;
204204
}
205205
}
206-
else if (method.getName().equals("getWarnings") || method.getName().equals("clearWarnings")) {
207-
// Avoid creation of target Connection on pre-close cleanup (e.g. in Hibernate Session)
208-
return null;
209-
}
210206
else if (method.getName().equals("close")) {
211207
// Handle close method: only close if not within a transaction.
212208
DataSourceUtils.doReleaseConnection(this.target, this.targetDataSource);
@@ -218,6 +214,10 @@ else if (method.getName().equals("isClosed")) {
218214
}
219215

220216
if (this.target == null) {
217+
if (method.getName().equals("getWarnings") || method.getName().equals("clearWarnings")) {
218+
// Avoid creation of target Connection on pre-close cleanup (e.g. Hibernate Session)
219+
return null;
220+
}
221221
if (this.closed) {
222222
throw new SQLException("Connection handle already closed");
223223
}

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

Lines changed: 13 additions & 5 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.
@@ -117,6 +117,7 @@ private void doTestTransactionCommitRestoringAutoCommit(
117117
if (lazyConnection) {
118118
given(con.getAutoCommit()).willReturn(autoCommit);
119119
given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
120+
given(con.getWarnings()).willThrow(new SQLException());
120121
}
121122

122123
if (!lazyConnection || createStatement) {
@@ -142,6 +143,10 @@ protected void doInTransactionWithoutResult(TransactionStatus status) throws Run
142143
if (createStatement) {
143144
tCon.createStatement();
144145
}
146+
else {
147+
tCon.getWarnings();
148+
tCon.clearWarnings();
149+
}
145150
}
146151
catch (SQLException ex) {
147152
throw new UncategorizedSQLException("", "", ex);
@@ -209,7 +214,7 @@ private void doTestTransactionRollbackRestoringAutoCommit(
209214
}
210215

211216
final DataSource dsToUse = (lazyConnection ? new LazyConnectionDataSourceProxy(ds) : ds);
212-
tm = new DataSourceTransactionManager(dsToUse);
217+
tm = new DataSourceTransactionManager(dsToUse);
213218
TransactionTemplate tt = new TransactionTemplate(tm);
214219
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
215220
assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
@@ -669,7 +674,6 @@ public void testPropagationRequiresNewWithExistingTransactionAndUnrelatedFailing
669674
SQLException failure = new SQLException();
670675
given(ds2.getConnection()).willThrow(failure);
671676

672-
673677
final TransactionTemplate tt = new TransactionTemplate(tm);
674678
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
675679

@@ -974,12 +978,12 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
974978
ordered.verify(con).setAutoCommit(false);
975979
ordered.verify(con).setAutoCommit(true);
976980
verify(con).close();
977-
978981
}
979982

980983
@Test
981984
public void testTransactionAwareDataSourceProxy() throws Exception {
982985
given(con.getAutoCommit()).willReturn(true);
986+
given(con.getWarnings()).willThrow(new SQLException());
983987

984988
TransactionTemplate tt = new TransactionTemplate(tm);
985989
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
@@ -990,6 +994,9 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
990994
assertEquals(con, DataSourceUtils.getConnection(ds));
991995
TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
992996
try {
997+
Connection tCon = dsProxy.getConnection();
998+
tCon.getWarnings();
999+
tCon.clearWarnings();
9931000
assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
9941001
// should be ignored
9951002
dsProxy.getConnection().close();
@@ -1242,7 +1249,8 @@ protected void doInTransactionWithoutResult(TransactionStatus status) throws Run
12421249
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
12431250
}
12441251

1245-
@Test public void testTransactionWithPropagationNotSupported() throws Exception {
1252+
@Test
1253+
public void testTransactionWithPropagationNotSupported() throws Exception {
12461254
TransactionTemplate tt = new TransactionTemplate(tm);
12471255
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
12481256
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));

0 commit comments

Comments
 (0)