public abstract class DataSourceUtils
extends java.lang.Object
DataSource
. Includes special support for Spring-managed
transactional Connections, e.g. managed by DataSourceTransactionManager
getConnection(javax.sql.DataSource)
,
releaseConnection(java.sql.Connection, javax.sql.DataSource)
,
DataSourceTransactionManager
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.
|
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 |
getConnection(javax.sql.DataSource dataSource,
boolean allowCreate)
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 TransactionDefinition.Isolation |
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,
TransactionDefinition.Isolation 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.
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 getConnection(javax.sql.DataSource dataSource, boolean allowCreate) 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.
dataSource
- the DataSource to obtain Connections fromallowCreate
- If true allow the creation of a new connection if non is boundCannotGetJdbcConnectionException
- if the attempt to get a Connection failedreleaseConnection(java.sql.Connection, javax.sql.DataSource)
@Nullable public static TransactionDefinition.Isolation 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, io.micronaut.transaction.TransactionDefinition.Isolation)
public static void resetConnectionAfterTransaction(java.sql.Connection con, @Nullable TransactionDefinition.Isolation previousIsolationLevel)
con
- the Connection to resetpreviousIsolationLevel
- the isolation level to restore, if anyprepareConnectionForTransaction(java.sql.Connection, io.micronaut.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.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, boolean)
public static void doCloseConnection(java.sql.Connection con, @Nullable javax.sql.DataSource dataSource) throws java.sql.SQLException
con
- the Connection to close if necessarydataSource
- the DataSource that the Connection was obtained fromjava.sql.SQLException
- if thrown by JDBC methodsConnection.close()
public static java.sql.Connection getTargetConnection(java.sql.Connection con)
con
- the Connection proxy to unwrap