diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java index 8e57f8e39f..8d2b0f1d41 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -63,7 +63,7 @@ public ConnectionTemplate(final DataSource dataSource) { public T readResult(final String sql, final SelectQueryExecutor selectQueryExecutor, final Object... parameters) { - final Connection connection = DataSourceUtils.getConnection(dataSource); + final Connection connection = DataSourceUtils.getConnectionForTransactionSupports(dataSource); try (final PreparedStatement preparedStatement = connection.prepareStatement(sql)) { setQueryParameter(preparedStatement, parameters); final ResultSet resultSet = preparedStatement.executeQuery(); @@ -73,14 +73,14 @@ public T readResult(final String sql, log.error(e.getMessage(), e); throw new DataAccessException(e); } finally { - DataSourceUtils.releaseConnectionIfNotInTransaction(connection, dataSource); + DataSourceUtils.releaseConnectionForTransactionSupports(connection, dataSource); } } public void update(final String sql, final UpdateQueryExecutor updateQueryExecutor, final Object... parameters) { - final Connection connection = DataSourceUtils.getConnection(dataSource); + final Connection connection = DataSourceUtils.getConnectionForTransactionSupports(dataSource); try (final PreparedStatement preparedStatement = connection.prepareStatement(sql)) { setQueryParameter(preparedStatement, parameters); @@ -89,7 +89,7 @@ public void update(final String sql, log.error(e.getMessage(), e); throw new DataAccessException(e); } finally { - DataSourceUtils.releaseConnectionIfNotInTransaction(connection, dataSource); + DataSourceUtils.releaseConnectionForTransactionSupports(connection, dataSource); } } diff --git a/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java b/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java index 3d9ad9f672..280dbcde36 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java +++ b/jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java @@ -11,7 +11,7 @@ public abstract class DataSourceUtils { private DataSourceUtils() {} - public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException { + public static Connection getConnectionForTransactionRequired(DataSource dataSource) throws CannotGetJdbcConnectionException { Connection connection = TransactionSynchronizationManager.getResource(dataSource); if (connection != null) { return connection; @@ -26,7 +26,7 @@ public static Connection getConnection(DataSource dataSource) throws CannotGetJd } } - public static void releaseConnection(Connection connection, DataSource dataSource) { + public static void releaseConnectionForTransactionRequired(Connection connection, DataSource dataSource) { try { connection.close(); TransactionSynchronizationManager.unbindResource(dataSource); @@ -35,7 +35,20 @@ public static void releaseConnection(Connection connection, DataSource dataSourc } } - public static void releaseConnectionIfNotInTransaction(Connection connection, DataSource dataSource) { + public static Connection getConnectionForTransactionSupports(DataSource dataSource) throws CannotGetJdbcConnectionException { + Connection connection = TransactionSynchronizationManager.getResource(dataSource); + if (connection != null) { + return connection; + } + + try { + return dataSource.getConnection(); + } catch (SQLException ex) { + throw new CannotGetJdbcConnectionException("Failed to obtain JDBC Connection", ex); + } + } + + public static void releaseConnectionForTransactionSupports(Connection connection, DataSource dataSource) { if (TransactionSynchronizationManager.isActiveTransaction(dataSource)) { return; } diff --git a/jdbc/src/main/java/org/springframework/transaction/Transaction.java b/jdbc/src/main/java/org/springframework/transaction/Transaction.java index 29f42f92ff..8e3ec764d2 100644 --- a/jdbc/src/main/java/org/springframework/transaction/Transaction.java +++ b/jdbc/src/main/java/org/springframework/transaction/Transaction.java @@ -4,7 +4,6 @@ import org.slf4j.LoggerFactory; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.datasource.DataSourceUtils; -import org.springframework.transaction.support.TransactionSynchronizationManager; import javax.sql.DataSource; import java.sql.Connection; @@ -21,7 +20,7 @@ public Transaction(final DataSource dataSource) { } public T run(final TransactionTemplate transactionTemplate) { - final Connection connection = DataSourceUtils.getConnection(dataSource); + final Connection connection = DataSourceUtils.getConnectionForTransactionRequired(dataSource); start(connection); try { final T result = transactionTemplate.execute(connection); @@ -32,7 +31,7 @@ public T run(final TransactionTemplate transactionTemplate) { rollback(connection); throw new DataAccessException(exception); } finally { - DataSourceUtils.releaseConnection(connection, dataSource); + DataSourceUtils.releaseConnectionForTransactionRequired(connection, dataSource); } }