public abstract class DataSourceUtils
extends java.lang.Object
DataSource
. Includes special support for Spring-managed
transactional Connections, e.g. managed by DataSourceTransactionManager
or JtaTransactionManager
.
Used internally by Spring's JdbcTemplate
,
Spring's JDBC operation objects and the JDBC DataSourceTransactionManager
.
Can also be used directly in application code.
getConnection(javax.sql.DataSource)
,
releaseConnection(java.sql.Connection, javax.sql.DataSource)
,
DataSourceTransactionManager
,
JtaTransactionManager
,
TransactionSynchronizationManager
Modifier and Type | Field and Description |
---|---|
static int |
CONNECTION_SYNCHRONIZATION_ORDER
Order value for TransactionSynchronization objects that clean up JDBC Connections.
|
Constructor and Description |
---|
DataSourceUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
applyTimeout(java.sql.Statement stmt,
javax.sql.DataSource dataSource,
int timeout)
Apply the specified timeout - overridden by the current transaction timeout,
if any - to the given JDBC Statement object.
|
static void |
applyTransactionTimeout(java.sql.Statement stmt,
javax.sql.DataSource dataSource)
Apply the current transaction timeout, if any,
to the given JDBC Statement object.
|
static void |
doCloseConnection(java.sql.Connection con,
javax.sql.DataSource dataSource)
Close the Connection, unless a
SmartDataSource doesn't want us to. |
static java.sql.Connection |
doGetConnection(javax.sql.DataSource dataSource)
Actually obtain a JDBC Connection from the given DataSource.
|
static void |
doReleaseConnection(java.sql.Connection con,
javax.sql.DataSource dataSource)
Actually close the given Connection, obtained from the given DataSource.
|
static java.sql.Connection |
getConnection(javax.sql.DataSource dataSource)
Obtain a Connection from the given DataSource.
|
static java.sql.Connection |
getTargetConnection(java.sql.Connection con)
Return the innermost target Connection of the given Connection.
|
static boolean |
isConnectionTransactional(java.sql.Connection con,
javax.sql.DataSource dataSource)
Determine whether the given JDBC Connection is transactional, that is,
bound to the current thread by Spring's transaction facilities.
|
static java.lang.Integer |
prepareConnectionForTransaction(java.sql.Connection con,
TransactionDefinition definition)
Prepare the given Connection with the given transaction semantics.
|
static void |
releaseConnection(java.sql.Connection con,
javax.sql.DataSource dataSource)
Close the given Connection, obtained from the given DataSource,
if it is not managed externally (that is, not bound to the thread).
|
static void |
resetConnectionAfterTransaction(java.sql.Connection con,
java.lang.Integer previousIsolationLevel)
Reset the given Connection after a transaction,
regarding read-only flag and isolation level.
|
public static final int CONNECTION_SYNCHRONIZATION_ORDER
public static java.sql.Connection getConnection(javax.sql.DataSource dataSource) throws CannotGetJdbcConnectionException
Is aware of a corresponding Connection bound to the current thread, for example
when using DataSourceTransactionManager
. Will bind a Connection to the
thread if transaction synchronization is active, e.g. when running within a
JTA
transaction).
dataSource
- the DataSource to obtain Connections fromCannotGetJdbcConnectionException
- if the attempt to get a Connection failedreleaseConnection(java.sql.Connection, javax.sql.DataSource)
public static java.sql.Connection doGetConnection(javax.sql.DataSource dataSource) throws java.sql.SQLException
getConnection(javax.sql.DataSource)
, but throwing the original SQLException.
Is aware of a corresponding Connection bound to the current thread, for example
when using DataSourceTransactionManager
. Will bind a Connection to the thread
if transaction synchronization is active (e.g. if in a JTA transaction).
Directly accessed by TransactionAwareDataSourceProxy
.
dataSource
- the DataSource to obtain Connections fromjava.sql.SQLException
- if thrown by JDBC methodsdoReleaseConnection(java.sql.Connection, javax.sql.DataSource)
@Nullable public static java.lang.Integer prepareConnectionForTransaction(java.sql.Connection con, @Nullable TransactionDefinition definition) throws java.sql.SQLException
con
- the Connection to preparedefinition
- the transaction definition to applyjava.sql.SQLException
- if thrown by JDBC methodsresetConnectionAfterTransaction(java.sql.Connection, java.lang.Integer)
public static void resetConnectionAfterTransaction(java.sql.Connection con, @Nullable java.lang.Integer previousIsolationLevel)
con
- the Connection to resetpreviousIsolationLevel
- the isolation level to restore, if anyprepareConnectionForTransaction(java.sql.Connection, org.springframework.transaction.TransactionDefinition)
public static boolean isConnectionTransactional(java.sql.Connection con, @Nullable javax.sql.DataSource dataSource)
con
- the Connection to checkdataSource
- the DataSource that the Connection was obtained from
(may be null
)public static void applyTransactionTimeout(java.sql.Statement stmt, @Nullable javax.sql.DataSource dataSource) throws java.sql.SQLException
stmt
- the JDBC Statement objectdataSource
- the DataSource that the Connection was obtained fromjava.sql.SQLException
- if thrown by JDBC methodsStatement.setQueryTimeout(int)
public static void applyTimeout(java.sql.Statement stmt, @Nullable javax.sql.DataSource dataSource, int timeout) throws java.sql.SQLException
stmt
- the JDBC Statement objectdataSource
- the DataSource that the Connection was obtained fromtimeout
- the timeout to apply (or 0 for no timeout outside of a transaction)java.sql.SQLException
- if thrown by JDBC methodsStatement.setQueryTimeout(int)
public static void releaseConnection(@Nullable java.sql.Connection con, @Nullable javax.sql.DataSource dataSource)
con
- the Connection to close if necessary
(if this is null
, the call will be ignored)dataSource
- the DataSource that the Connection was obtained from
(may be null
)getConnection(javax.sql.DataSource)
public static void doReleaseConnection(@Nullable java.sql.Connection con, @Nullable javax.sql.DataSource dataSource) throws java.sql.SQLException
releaseConnection(java.sql.Connection, javax.sql.DataSource)
, but throwing the original SQLException.
Directly accessed by TransactionAwareDataSourceProxy
.
con
- the Connection to close if necessary
(if this is null
, the call will be ignored)dataSource
- the DataSource that the Connection was obtained from
(may be null
)java.sql.SQLException
- if thrown by JDBC methodsdoGetConnection(javax.sql.DataSource)
public static void doCloseConnection(java.sql.Connection con, @Nullable javax.sql.DataSource dataSource) throws java.sql.SQLException
SmartDataSource
doesn't want us to.con
- the Connection to close if necessarydataSource
- the DataSource that the Connection was obtained fromjava.sql.SQLException
- if thrown by JDBC methodsConnection.close()
,
SmartDataSource.shouldClose(Connection)
public static java.sql.Connection getTargetConnection(java.sql.Connection con)
con
- the Connection proxy to unwrapConnectionProxy.getTargetConnection()