diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxy.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxy.java index 5adde7fd901c96b8e558f36177ddd7edd138622b..f198c662d73244e2146a8b159a8920ca4e68e440 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxy.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/LazyConnectionDataSourceProxy.java @@ -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. @@ -354,18 +354,12 @@ public class LazyConnectionDataSourceProxy extends DelegatingDataSource { this.holdability = (Integer) 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")) { diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy.java index 97a2fdc9518bb04ebe8a026f1e3ebf883db516ff..7d29cc4785bc31ba0eaaed24b016f927e7e850d0 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy.java @@ -203,10 +203,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); @@ -218,6 +214,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"); } diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceTransactionManagerTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceTransactionManagerTests.java index f93f7995ab1a2e8fc84b97fc1eae7af55a681d93..266cd3a12a5e9cef4868aa6317a4507d3c267428 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceTransactionManagerTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceTransactionManagerTests.java @@ -125,6 +125,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) { @@ -152,6 +153,10 @@ public class DataSourceTransactionManagerTests { if (createStatement) { tCon.createStatement(); } + else { + tCon.getWarnings(); + tCon.clearWarnings(); + } } catch (SQLException ex) { throw new UncategorizedSQLException("", "", ex); @@ -703,7 +708,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); @@ -1024,12 +1028,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); boolean condition1 = !TransactionSynchronizationManager.hasResource(ds); @@ -1041,6 +1045,9 @@ public class DataSourceTransactionManagerTests { assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con); TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds); try { + Connection tCon = dsProxy.getConnection(); + tCon.getWarnings(); + tCon.clearWarnings(); assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con); // should be ignored dsProxy.getConnection().close(); @@ -1286,7 +1293,8 @@ public class DataSourceTransactionManagerTests { assertThat(condition).as("Hasn't thread connection").isTrue(); } - @Test public void testTransactionWithPropagationNotSupported() throws Exception { + @Test + public void testTransactionWithPropagationNotSupported() throws Exception { TransactionTemplate tt = new TransactionTemplate(tm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED); boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);