Skip to content

Commit 53d0673

Browse files
committed
Merge branch '5.1.x'
2 parents 9be3279 + 7c2e2d4 commit 53d0673

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
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: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ private void doTestTransactionCommitRestoringAutoCommit(
125125
if (lazyConnection) {
126126
given(con.getAutoCommit()).willReturn(autoCommit);
127127
given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
128+
given(con.getWarnings()).willThrow(new SQLException());
128129
}
129130

130131
if (!lazyConnection || createStatement) {
@@ -152,6 +153,10 @@ protected void doInTransactionWithoutResult(TransactionStatus status) throws Run
152153
if (createStatement) {
153154
tCon.createStatement();
154155
}
156+
else {
157+
tCon.getWarnings();
158+
tCon.clearWarnings();
159+
}
155160
}
156161
catch (SQLException ex) {
157162
throw new UncategorizedSQLException("", "", ex);
@@ -703,7 +708,6 @@ public void testPropagationRequiresNewWithExistingTransactionAndUnrelatedFailing
703708
SQLException failure = new SQLException();
704709
given(ds2.getConnection()).willThrow(failure);
705710

706-
707711
final TransactionTemplate tt = new TransactionTemplate(tm);
708712
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
709713

@@ -1024,12 +1028,12 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
10241028
ordered.verify(con).setAutoCommit(false);
10251029
ordered.verify(con).setAutoCommit(true);
10261030
verify(con).close();
1027-
10281031
}
10291032

10301033
@Test
10311034
public void testTransactionAwareDataSourceProxy() throws Exception {
10321035
given(con.getAutoCommit()).willReturn(true);
1036+
given(con.getWarnings()).willThrow(new SQLException());
10331037

10341038
TransactionTemplate tt = new TransactionTemplate(tm);
10351039
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
@@ -1041,6 +1045,9 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
10411045
assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
10421046
TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
10431047
try {
1048+
Connection tCon = dsProxy.getConnection();
1049+
tCon.getWarnings();
1050+
tCon.clearWarnings();
10441051
assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
10451052
// should be ignored
10461053
dsProxy.getConnection().close();
@@ -1286,7 +1293,8 @@ protected void doInTransactionWithoutResult(TransactionStatus status) throws Run
12861293
assertThat(condition).as("Hasn't thread connection").isTrue();
12871294
}
12881295

1289-
@Test public void testTransactionWithPropagationNotSupported() throws Exception {
1296+
@Test
1297+
public void testTransactionWithPropagationNotSupported() throws Exception {
12901298
TransactionTemplate tt = new TransactionTemplate(tm);
12911299
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
12921300
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);

0 commit comments

Comments
 (0)