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 2455b187c5..412400ed8e 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. @@ -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")) { 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 0b082f2378..aebd68300b 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 @@ -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"); } 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 0e1f85de3d..2f651a2afd 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 @@ -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));