Skip to content

Commit d8e624e

Browse files
committed
Consistent suppression of get/clearWarnings without target connection
See gh-23346
1 parent 762ea3e commit d8e624e

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-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.
@@ -354,18 +354,12 @@ else if (method.getName().equals("setHoldability")) {
354354
this.holdability = (Integer) args[0];
355355
return null;
356356
}
357-
else if (method.getName().equals("commit")) {
357+
else if (method.getName().equals("commit") || method.getName().equals("rollback")) {
358358
// Ignore: no statements created yet.
359359
return null;
360360
}
361-
else if (method.getName().equals("rollback")) {
362-
// Ignore: no statements created yet.
363-
return null;
364-
}
365-
else if (method.getName().equals("getWarnings")) {
366-
return null;
367-
}
368-
else if (method.getName().equals("clearWarnings")) {
361+
else if (method.getName().equals("getWarnings") || method.getName().equals("clearWarnings")) {
362+
// Ignore: no warnings to expose yet.
369363
return null;
370364
}
371365
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: 22 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.
@@ -48,8 +48,18 @@
4848
import org.springframework.transaction.support.TransactionSynchronizationManager;
4949
import org.springframework.transaction.support.TransactionTemplate;
5050

51-
import static org.junit.Assert.*;
52-
import static org.mockito.BDDMockito.*;
51+
import static org.junit.Assert.assertEquals;
52+
import static org.junit.Assert.assertFalse;
53+
import static org.junit.Assert.assertNull;
54+
import static org.junit.Assert.assertSame;
55+
import static org.junit.Assert.assertTrue;
56+
import static org.junit.Assert.fail;
57+
import static org.mockito.BDDMockito.given;
58+
import static org.mockito.BDDMockito.inOrder;
59+
import static org.mockito.BDDMockito.mock;
60+
import static org.mockito.BDDMockito.times;
61+
import static org.mockito.BDDMockito.verify;
62+
import static org.mockito.BDDMockito.willThrow;
5363

5464
/**
5565
* @author Juergen Hoeller
@@ -117,6 +127,7 @@ private void doTestTransactionCommitRestoringAutoCommit(
117127
if (lazyConnection) {
118128
given(con.getAutoCommit()).willReturn(autoCommit);
119129
given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
130+
given(con.getWarnings()).willThrow(new SQLException());
120131
}
121132

122133
if (!lazyConnection || createStatement) {
@@ -142,6 +153,10 @@ protected void doInTransactionWithoutResult(TransactionStatus status) throws Run
142153
if (createStatement) {
143154
tCon.createStatement();
144155
}
156+
else {
157+
tCon.getWarnings();
158+
tCon.clearWarnings();
159+
}
145160
}
146161
catch (SQLException ex) {
147162
throw new UncategorizedSQLException("", "", ex);
@@ -669,7 +684,6 @@ public void testPropagationRequiresNewWithExistingTransactionAndUnrelatedFailing
669684
SQLException failure = new SQLException();
670685
given(ds2.getConnection()).willThrow(failure);
671686

672-
673687
final TransactionTemplate tt = new TransactionTemplate(tm);
674688
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
675689

@@ -974,12 +988,12 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
974988
ordered.verify(con).setAutoCommit(false);
975989
ordered.verify(con).setAutoCommit(true);
976990
verify(con).close();
977-
978991
}
979992

980993
@Test
981994
public void testTransactionAwareDataSourceProxy() throws Exception {
982995
given(con.getAutoCommit()).willReturn(true);
996+
given(con.getWarnings()).willThrow(new SQLException());
983997

984998
TransactionTemplate tt = new TransactionTemplate(tm);
985999
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
@@ -990,6 +1004,9 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
9901004
assertEquals(con, DataSourceUtils.getConnection(ds));
9911005
TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
9921006
try {
1007+
Connection tCon = dsProxy.getConnection();
1008+
tCon.getWarnings();
1009+
tCon.clearWarnings();
9931010
assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
9941011
// should be ignored
9951012
dsProxy.getConnection().close();

0 commit comments

Comments
 (0)