Consistent suppression of get/clearWarnings without target connection

See gh-23346
This commit is contained in:
Juergen Hoeller
2019-08-02 01:19:16 +02:00
parent f99b2f11da
commit 13b9f58c4a
3 changed files with 22 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -340,18 +340,12 @@ public class LazyConnectionDataSourceProxy extends DelegatingDataSource {
this.readOnly = (Boolean) args[0];
return null;
}
else if (method.getName().equals("commit")) {
else if (method.getName().equals("commit") || method.getName().equals("rollback")) {
// Ignore: no statements created yet.
return null;
}
else if (method.getName().equals("rollback")) {
// Ignore: no statements created yet.
return null;
}
else if (method.getName().equals("getWarnings")) {
return null;
}
else if (method.getName().equals("clearWarnings")) {
else if (method.getName().equals("getWarnings") || method.getName().equals("clearWarnings")) {
// Ignore: no warnings to expose yet.
return null;
}
else if (method.getName().equals("close")) {

View File

@@ -207,10 +207,6 @@ public class TransactionAwareDataSourceProxy extends DelegatingDataSource {
return true;
}
}
else if (method.getName().equals("getWarnings") || method.getName().equals("clearWarnings")) {
// Avoid creation of target Connection on pre-close cleanup (e.g. in Hibernate Session)
return null;
}
else if (method.getName().equals("close")) {
// Handle close method: only close if not within a transaction.
DataSourceUtils.doReleaseConnection(this.target, this.targetDataSource);
@@ -222,6 +218,10 @@ public class TransactionAwareDataSourceProxy extends DelegatingDataSource {
}
if (this.target == null) {
if (method.getName().equals("getWarnings") || method.getName().equals("clearWarnings")) {
// Avoid creation of target Connection on pre-close cleanup (e.g. Hibernate Session)
return null;
}
if (this.closed) {
throw new SQLException("Connection handle already closed");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -66,7 +66,7 @@ public class DataSourceTransactionManagerTests {
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
ds = mock(DataSource.class);
con = mock(Connection.class);
given(ds.getConnection()).willReturn(con);
@@ -118,6 +118,7 @@ public class DataSourceTransactionManagerTests {
if (lazyConnection) {
given(con.getAutoCommit()).willReturn(autoCommit);
given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
given(con.getWarnings()).willThrow(new SQLException());
}
if (!lazyConnection || createStatement) {
@@ -144,6 +145,10 @@ public class DataSourceTransactionManagerTests {
tCon.createStatement();
assertEquals(con, new SimpleNativeJdbcExtractor().getNativeConnection(tCon));
}
else {
tCon.getWarnings();
tCon.clearWarnings();
}
}
catch (SQLException ex) {
throw new UncategorizedSQLException("", "", ex);
@@ -211,7 +216,7 @@ public class DataSourceTransactionManagerTests {
}
final DataSource dsToUse = (lazyConnection ? new LazyConnectionDataSourceProxy(ds) : ds);
tm = new DataSourceTransactionManager(dsToUse);
tm = new DataSourceTransactionManager(dsToUse);
TransactionTemplate tt = new TransactionTemplate(tm);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
@@ -671,7 +676,6 @@ public class DataSourceTransactionManagerTests {
SQLException failure = new SQLException();
given(ds2.getConnection()).willThrow(failure);
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
@@ -976,12 +980,12 @@ public class DataSourceTransactionManagerTests {
ordered.verify(con).setAutoCommit(false);
ordered.verify(con).setAutoCommit(true);
verify(con).close();
}
@Test
public void testTransactionAwareDataSourceProxy() throws Exception {
given(con.getAutoCommit()).willReturn(true);
given(con.getWarnings()).willThrow(new SQLException());
TransactionTemplate tt = new TransactionTemplate(tm);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
@@ -992,6 +996,9 @@ public class DataSourceTransactionManagerTests {
assertEquals(con, DataSourceUtils.getConnection(ds));
TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
try {
Connection tCon = dsProxy.getConnection();
tCon.getWarnings();
tCon.clearWarnings();
assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
assertEquals(con, new SimpleNativeJdbcExtractor().getNativeConnection(dsProxy.getConnection()));
// should be ignored
@@ -1249,7 +1256,8 @@ public class DataSourceTransactionManagerTests {
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
}
@Test public void testTransactionWithPropagationNotSupported() throws Exception {
@Test
public void testTransactionWithPropagationNotSupported() throws Exception {
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));