LocalDataSourceConnectionProvider checks against SmartDataSource before closing a Connection
Issue: SPR-9978
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2010 the original author or authors.
|
* Copyright 2002-2012 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -316,7 +316,6 @@ public abstract class DataSourceUtils {
|
|||||||
if (con == null) {
|
if (con == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dataSource != null) {
|
if (dataSource != null) {
|
||||||
ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
|
ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
|
||||||
if (conHolder != null && connectionEquals(conHolder, con)) {
|
if (conHolder != null && connectionEquals(conHolder, con)) {
|
||||||
@@ -325,11 +324,20 @@ public abstract class DataSourceUtils {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
logger.debug("Returning JDBC Connection to DataSource");
|
||||||
|
doCloseConnection(con, dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
// Leave the Connection open only if the DataSource is our
|
/**
|
||||||
// special SmartDataSoruce and it wants the Connection left open.
|
* Close the Connection, unless a {@link SmartDataSource} doesn't want us to.
|
||||||
|
* @param con the Connection to close if necessary
|
||||||
|
* @param dataSource the DataSource that the Connection was obtained from
|
||||||
|
* @throws SQLException if thrown by JDBC methods
|
||||||
|
* @see Connection#close()
|
||||||
|
* @see SmartDataSource#shouldClose(Connection)
|
||||||
|
*/
|
||||||
|
public static void doCloseConnection(Connection con, DataSource dataSource) throws SQLException {
|
||||||
if (!(dataSource instanceof SmartDataSource) || ((SmartDataSource) dataSource).shouldClose(con)) {
|
if (!(dataSource instanceof SmartDataSource) || ((SmartDataSource) dataSource).shouldClose(con)) {
|
||||||
logger.debug("Returning JDBC Connection to DataSource");
|
|
||||||
con.close();
|
con.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2009 the original author or authors.
|
* Copyright 2002-2012 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -25,6 +25,8 @@ import org.hibernate.HibernateException;
|
|||||||
import org.hibernate.connection.ConnectionProvider;
|
import org.hibernate.connection.ConnectionProvider;
|
||||||
import org.hibernate.util.JDBCExceptionReporter;
|
import org.hibernate.util.JDBCExceptionReporter;
|
||||||
|
|
||||||
|
import org.springframework.jdbc.datasource.DataSourceUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hibernate connection provider for local DataSource instances
|
* Hibernate connection provider for local DataSource instances
|
||||||
* in an application context. This provider will be used if
|
* in an application context. This provider will be used if
|
||||||
@@ -87,12 +89,12 @@ public class LocalDataSourceConnectionProvider implements ConnectionProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This implementation simply calls <code>Connection.close</code>.
|
* This implementation calls {@link DataSourceUtils#doCloseConnection},
|
||||||
* @see java.sql.Connection#close()
|
* checking against a {@link org.springframework.jdbc.datasource.SmartDataSource}.
|
||||||
*/
|
*/
|
||||||
public void closeConnection(Connection con) throws SQLException {
|
public void closeConnection(Connection con) throws SQLException {
|
||||||
try {
|
try {
|
||||||
con.close();
|
DataSourceUtils.doCloseConnection(con, this.dataSourceToUse);
|
||||||
}
|
}
|
||||||
catch (SQLException ex) {
|
catch (SQLException ex) {
|
||||||
JDBCExceptionReporter.logExceptions(ex);
|
JDBCExceptionReporter.logExceptions(ex);
|
||||||
|
|||||||
Reference in New Issue
Block a user