Package io.micronaut.transaction.jdbc
Class DataSourceUtils
- java.lang.Object
-
- io.micronaut.transaction.jdbc.DataSourceUtils
-
public abstract class DataSourceUtils extends java.lang.ObjectHelper class that provides static methods for obtaining JDBC Connections from aDataSource. Includes special support for Spring-managed transactional Connections, e.g. managed byDataSourceTransactionManager- Author:
- Rod Johnson, Juergen Hoeller, graemerocher
- See Also:
getConnection(javax.sql.DataSource),releaseConnection(java.sql.Connection, javax.sql.DataSource),DataSourceTransactionManager
-
-
Field Summary
Fields Modifier and Type Field Description static intCONNECTION_SYNCHRONIZATION_ORDEROrder value for TransactionSynchronization objects that clean up JDBC Connections.
-
Constructor Summary
Constructors Constructor Description DataSourceUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidapplyTimeout(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 voidapplyTransactionTimeout(java.sql.Statement stmt, javax.sql.DataSource dataSource)Apply the current transaction timeout, if any, to the given JDBC Statement object.static voiddoCloseConnection(java.sql.Connection con, javax.sql.DataSource dataSource)Close the Connection.static voiddoReleaseConnection(java.sql.Connection con, javax.sql.DataSource dataSource)Actually close the given Connection, obtained from the given DataSource.static java.sql.ConnectiongetConnection(javax.sql.DataSource dataSource)Obtain a Connection from the given DataSource.static java.sql.ConnectiongetConnection(javax.sql.DataSource dataSource, boolean allowCreate)Obtain a Connection from the given DataSource.static java.sql.ConnectiongetTargetConnection(java.sql.Connection con)Return the innermost target Connection of the given Connection.static booleanisConnectionTransactional(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.IsolationprepareConnectionForTransaction(java.sql.Connection con, TransactionDefinition definition)Prepare the given Connection with the given transaction semantics.static voidreleaseConnection(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 voidresetConnectionAfterTransaction(java.sql.Connection con, TransactionDefinition.Isolation previousIsolationLevel)Reset the given Connection after a transaction, regarding read-only flag and isolation level.
-
-
-
Field Detail
-
CONNECTION_SYNCHRONIZATION_ORDER
public static final int CONNECTION_SYNCHRONIZATION_ORDER
Order value for TransactionSynchronization objects that clean up JDBC Connections.- See Also:
- Constant Field Values
-
-
Method Detail
-
getConnection
public static java.sql.Connection getConnection(javax.sql.DataSource dataSource) throws CannotGetJdbcConnectionExceptionObtain a Connection from the given DataSource. Translates SQLExceptions into the Spring hierarchy of unchecked generic data access exceptions, simplifying calling code and making any exception that is thrown more meaningful.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.- Parameters:
dataSource- the DataSource to obtain Connections from- Returns:
- a JDBC Connection from the given DataSource
- Throws:
CannotGetJdbcConnectionException- if the attempt to get a Connection failed- See Also:
releaseConnection(java.sql.Connection, javax.sql.DataSource)
-
getConnection
public static java.sql.Connection getConnection(javax.sql.DataSource dataSource, boolean allowCreate) throws CannotGetJdbcConnectionExceptionObtain a Connection from the given DataSource. Translates SQLExceptions into the Spring hierarchy of unchecked generic data access exceptions, simplifying calling code and making any exception that is thrown more meaningful.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.- Parameters:
dataSource- the DataSource to obtain Connections fromallowCreate- If true allow the creation of a new connection if non is bound- Returns:
- a JDBC Connection from the given DataSource
- Throws:
CannotGetJdbcConnectionException- if the attempt to get a Connection failed- See Also:
releaseConnection(java.sql.Connection, javax.sql.DataSource)
-
prepareConnectionForTransaction
@Nullable public static TransactionDefinition.Isolation prepareConnectionForTransaction(java.sql.Connection con, @Nullable TransactionDefinition definition) throws java.sql.SQLException
Prepare the given Connection with the given transaction semantics.- Parameters:
con- the Connection to preparedefinition- the transaction definition to apply- Returns:
- the previous isolation level, if any
- Throws:
java.sql.SQLException- if thrown by JDBC methods- See Also:
resetConnectionAfterTransaction(java.sql.Connection, io.micronaut.transaction.TransactionDefinition.Isolation)
-
resetConnectionAfterTransaction
public static void resetConnectionAfterTransaction(java.sql.Connection con, @Nullable TransactionDefinition.Isolation previousIsolationLevel)Reset the given Connection after a transaction, regarding read-only flag and isolation level.- Parameters:
con- the Connection to resetpreviousIsolationLevel- the isolation level to restore, if any- See Also:
prepareConnectionForTransaction(java.sql.Connection, io.micronaut.transaction.TransactionDefinition)
-
isConnectionTransactional
public static boolean isConnectionTransactional(java.sql.Connection con, @Nullable javax.sql.DataSource dataSource)Determine whether the given JDBC Connection is transactional, that is, bound to the current thread by Spring's transaction facilities.- Parameters:
con- the Connection to checkdataSource- the DataSource that the Connection was obtained from (may benull)- Returns:
- whether the Connection is transactional
-
applyTransactionTimeout
public static void applyTransactionTimeout(java.sql.Statement stmt, @Nullable javax.sql.DataSource dataSource) throws java.sql.SQLExceptionApply the current transaction timeout, if any, to the given JDBC Statement object.- Parameters:
stmt- the JDBC Statement objectdataSource- the DataSource that the Connection was obtained from- Throws:
java.sql.SQLException- if thrown by JDBC methods- See Also:
Statement.setQueryTimeout(int)
-
applyTimeout
public static void applyTimeout(java.sql.Statement stmt, @Nullable javax.sql.DataSource dataSource, int timeout) throws java.sql.SQLExceptionApply the specified timeout - overridden by the current transaction timeout, if any - to the given JDBC Statement object.- Parameters:
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)- Throws:
java.sql.SQLException- if thrown by JDBC methods- See Also:
Statement.setQueryTimeout(int)
-
releaseConnection
public static void releaseConnection(@Nullable java.sql.Connection con, @Nullable 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).- Parameters:
con- the Connection to close if necessary (if this isnull, the call will be ignored)dataSource- the DataSource that the Connection was obtained from (may benull)- See Also:
getConnection(javax.sql.DataSource)
-
doReleaseConnection
public static void doReleaseConnection(@Nullable java.sql.Connection con, @Nullable javax.sql.DataSource dataSource) throws java.sql.SQLExceptionActually close the given Connection, obtained from the given DataSource. Same asreleaseConnection(java.sql.Connection, javax.sql.DataSource), but throwing the original SQLException.- Parameters:
con- the Connection to close if necessary (if this isnull, the call will be ignored)dataSource- the DataSource that the Connection was obtained from (may benull)- Throws:
java.sql.SQLException- if thrown by JDBC methods- See Also:
doGetConnection(javax.sql.DataSource, boolean)
-
doCloseConnection
public static void doCloseConnection(java.sql.Connection con, @Nullable javax.sql.DataSource dataSource) throws java.sql.SQLExceptionClose the Connection.- Parameters:
con- the Connection to close if necessarydataSource- the DataSource that the Connection was obtained from- Throws:
java.sql.SQLException- if thrown by JDBC methods- See Also:
Connection.close()
-
getTargetConnection
public static java.sql.Connection getTargetConnection(java.sql.Connection con)
Return the innermost target Connection of the given Connection. If the given Connection is a proxy, it will be unwrapped until a non-proxy Connection is found. Otherwise, the passed-in Connection will be returned as-is.- Parameters:
con- the Connection proxy to unwrap- Returns:
- the innermost target Connection, or the passed-in one if no proxy
-
-