Package io.micronaut.transaction.jdbc
Class DataSourceUtils
java.lang.Object
io.micronaut.transaction.jdbc.DataSourceUtils
Helper class that provides static methods for obtaining JDBC Connections from
 a 
DataSource. Includes special support for Spring-managed
 transactional Connections, e.g. managed by DataSourceTransactionManager- Author:
- Rod Johnson, Juergen Hoeller, graemerocher
- See Also:
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final intOrder value for TransactionSynchronization objects that clean up JDBC Connections.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic voidapplyTimeout(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 voidapplyTransactionTimeout(Statement stmt, DataSource dataSource) Apply the current transaction timeout, if any, to the given JDBC Statement object.static voiddoCloseConnection(Connection con, DataSource dataSource) Close the Connection.static voiddoReleaseConnection(Connection con, DataSource dataSource) Actually close the given Connection, obtained from the given DataSource.static ConnectiongetConnection(DataSource dataSource) Obtain a Connection from the given DataSource.static ConnectiongetConnection(DataSource dataSource, boolean allowCreate) Obtain a Connection from the given DataSource.static ConnectionReturn the innermost target Connection of the given Connection.static booleanisConnectionTransactional(Connection con, DataSource dataSource) Determine whether the given JDBC Connection is transactional, that is, bound to the current thread by Spring's transaction facilities.prepareConnectionForTransaction(Connection con, TransactionDefinition definition) Prepare the given Connection with the given transaction semantics.static voidreleaseConnection(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 voidresetConnectionAfterTransaction(Connection con, TransactionDefinition.Isolation previousIsolationLevel) Reset the given Connection after a transaction, regarding read-only flag and isolation level.
- 
Field Details- 
CONNECTION_SYNCHRONIZATION_ORDERpublic static final int CONNECTION_SYNCHRONIZATION_ORDEROrder value for TransactionSynchronization objects that clean up JDBC Connections.- See Also:
 
 
- 
- 
Constructor Details- 
DataSourceUtilspublic DataSourceUtils()
 
- 
- 
Method Details- 
getConnectionpublic static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException Obtain 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:
 
- 
getConnectionpublic static Connection getConnection(DataSource dataSource, boolean allowCreate) throws CannotGetJdbcConnectionException Obtain 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
- allowCreate- 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:
 
- 
prepareConnectionForTransaction@Nullable public static TransactionDefinition.Isolation prepareConnectionForTransaction(Connection con, @Nullable TransactionDefinition definition) throws SQLException Prepare the given Connection with the given transaction semantics.- Parameters:
- con- the Connection to prepare
- definition- the transaction definition to apply
- Returns:
- the previous isolation level, if any
- Throws:
- SQLException- if thrown by JDBC methods
- See Also:
 
- 
resetConnectionAfterTransactionpublic static void resetConnectionAfterTransaction(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 reset
- previousIsolationLevel- the isolation level to restore, if any
- See Also:
 
- 
isConnectionTransactionalDetermine whether the given JDBC Connection is transactional, that is, bound to the current thread by Spring's transaction facilities.- Parameters:
- con- the Connection to check
- dataSource- the DataSource that the Connection was obtained from (may be- null)
- Returns:
- whether the Connection is transactional
 
- 
applyTransactionTimeoutpublic static void applyTransactionTimeout(Statement stmt, @Nullable DataSource dataSource) throws SQLException Apply the current transaction timeout, if any, to the given JDBC Statement object.- Parameters:
- stmt- the JDBC Statement object
- dataSource- the DataSource that the Connection was obtained from
- Throws:
- SQLException- if thrown by JDBC methods
- See Also:
 
- 
applyTimeoutpublic static void applyTimeout(Statement stmt, @Nullable DataSource dataSource, int timeout) throws SQLException Apply the specified timeout - overridden by the current transaction timeout, if any - to the given JDBC Statement object.- Parameters:
- stmt- the JDBC Statement object
- dataSource- the DataSource that the Connection was obtained from
- timeout- the timeout to apply (or 0 for no timeout outside of a transaction)
- Throws:
- SQLException- if thrown by JDBC methods
- See Also:
 
- 
releaseConnectionClose 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 is- null, the call will be ignored)
- dataSource- the DataSource that the Connection was obtained from (may be- null)
- See Also:
 
- 
doReleaseConnectionpublic static void doReleaseConnection(@Nullable Connection con, @Nullable DataSource dataSource) throws SQLException Actually 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 is- null, the call will be ignored)
- dataSource- the DataSource that the Connection was obtained from (may be- null)
- Throws:
- SQLException- if thrown by JDBC methods
- See Also:
- 
- doGetConnection(javax.sql.DataSource, boolean)
 
 
- 
doCloseConnectionpublic static void doCloseConnection(Connection con, @Nullable DataSource dataSource) throws SQLException Close the Connection.- Parameters:
- con- the Connection to close if necessary
- dataSource- the DataSource that the Connection was obtained from
- Throws:
- SQLException- if thrown by JDBC methods
- See Also:
 
- 
getTargetConnectionReturn 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
 
 
-