public abstract class DataSourceUtils extends 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(Statement stmt,
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(Statement stmt,
DataSource dataSource)
Apply the current transaction timeout, if any,
to the given JDBC Statement object.
|
static void |
doCloseConnection(Connection con,
DataSource dataSource)
Close the Connection, unless a
SmartDataSource doesn't want us to. |
static Connection |
doGetConnection(DataSource dataSource)
Actually obtain a JDBC Connection from the given DataSource.
|
static void |
doReleaseConnection(Connection con,
DataSource dataSource)
Actually close the given Connection, obtained from the given DataSource.
|
static Connection |
getConnection(DataSource dataSource)
Obtain a Connection from the given DataSource.
|
static Connection |
getTargetConnection(Connection con)
Return the innermost target Connection of the given Connection.
|
static boolean |
isConnectionTransactional(Connection con,
DataSource dataSource)
Determine whether the given JDBC Connection is transactional, that is,
bound to the current thread by Spring's transaction facilities.
|
static Integer |
prepareConnectionForTransaction(Connection con,
TransactionDefinition definition)
Prepare the given Connection with the given transaction semantics.
|
static void |
releaseConnection(Connection con,
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(Connection con,
Integer previousIsolationLevel)
Deprecated.
as of 5.1.11, in favor of
resetConnectionAfterTransaction(Connection, Integer, boolean) |
static void |
resetConnectionAfterTransaction(Connection con,
Integer previousIsolationLevel,
boolean resetReadOnly)
Reset the given Connection after a transaction,
regarding read-only flag and isolation level.
|
public static final int CONNECTION_SYNCHRONIZATION_ORDER
public static Connection getConnection(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(Connection, DataSource)
,
isConnectionTransactional(Connection, DataSource)
public static Connection doGetConnection(DataSource dataSource) throws 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 fromSQLException
- if thrown by JDBC methodsdoReleaseConnection(java.sql.Connection, javax.sql.DataSource)
@Nullable public static Integer prepareConnectionForTransaction(Connection con, @Nullable TransactionDefinition definition) throws SQLException
con
- the Connection to preparedefinition
- the transaction definition to applySQLException
- if thrown by JDBC methodsresetConnectionAfterTransaction(java.sql.Connection, java.lang.Integer, boolean)
,
Connection.setTransactionIsolation(int)
,
Connection.setReadOnly(boolean)
public static void resetConnectionAfterTransaction(Connection con, @Nullable Integer previousIsolationLevel, boolean resetReadOnly)
con
- the Connection to resetpreviousIsolationLevel
- the isolation level to restore, if anyresetReadOnly
- whether to reset the connection's read-only flagprepareConnectionForTransaction(java.sql.Connection, org.springframework.transaction.TransactionDefinition)
,
Connection.setTransactionIsolation(int)
,
Connection.setReadOnly(boolean)
@Deprecated public static void resetConnectionAfterTransaction(Connection con, @Nullable Integer previousIsolationLevel)
resetConnectionAfterTransaction(Connection, Integer, boolean)
con
- the Connection to resetpreviousIsolationLevel
- the isolation level to restore, if anypublic static boolean isConnectionTransactional(Connection con, @Nullable DataSource dataSource)
con
- the Connection to checkdataSource
- the DataSource that the Connection was obtained from
(may be null
)getConnection(DataSource)
public static void applyTransactionTimeout(Statement stmt, @Nullable DataSource dataSource) throws SQLException
stmt
- the JDBC Statement objectdataSource
- the DataSource that the Connection was obtained fromSQLException
- if thrown by JDBC methodsStatement.setQueryTimeout(int)
public static void applyTimeout(Statement stmt, @Nullable DataSource dataSource, int timeout) throws 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)SQLException
- if thrown by JDBC methodsStatement.setQueryTimeout(int)
public static void releaseConnection(@Nullable Connection con, @Nullable 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 Connection con, @Nullable DataSource dataSource) throws 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
)SQLException
- if thrown by JDBC methodsdoGetConnection(javax.sql.DataSource)
public static void doCloseConnection(Connection con, @Nullable DataSource dataSource) throws SQLException
SmartDataSource
doesn't want us to.con
- the Connection to close if necessarydataSource
- the DataSource that the Connection was obtained fromSQLException
- if thrown by JDBC methodsConnection.close()
,
SmartDataSource.shouldClose(Connection)
public static Connection getTargetConnection(Connection con)
con
- the Connection proxy to unwrapConnectionProxy.getTargetConnection()