|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.springframework.jdbc.datasource.DataSourceUtils
public abstract class DataSourceUtils
Helper class that provides static methods to obtain JDBC Connections from a DataSource, and to close Connections if necessary. Has special support for Spring-managed connections, e.g. for use with DataSourceTransactionManager.
Used internally by JdbcTemplate, 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
,
JdbcTemplate
,
org.springframework.jdbc.object
Field Summary | |
---|---|
static int |
CONNECTION_SYNCHRONIZATION_ORDER
Order value for TransactionSynchronization objects that clean up JDBC Connections. |
Constructor Summary | |
---|---|
DataSourceUtils()
|
Method Summary | |
---|---|
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 Connection |
doGetConnection(DataSource dataSource)
Actually get a JDBC Connection for the given DataSource. |
static void |
doReleaseConnection(Connection con,
DataSource dataSource)
Actually release a JDBC Connection for the given DataSource. |
static Connection |
getConnection(DataSource dataSource)
Get a Connection from the given DataSource. |
static Connection |
getTargetConnection(Connection con)
Return the innermost target Connection of the given Connection. |
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, created via the given DataSource, if it is not managed externally (that is, not bound to the thread). |
static void |
resetConnectionAfterTransaction(Connection con,
Integer previousIsolationLevel)
Reset the given Connection after a transaction, regarding read-only flag and isolation level. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int CONNECTION_SYNCHRONIZATION_ORDER
Constructor Detail |
---|
public DataSourceUtils()
Method Detail |
---|
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. if in a JTA transaction).
dataSource
- DataSource to get Connection from
CannotGetJdbcConnectionException
- if the attempt to get a Connection failedTransactionSynchronizationManager
,
DataSourceTransactionManager
public static Connection doGetConnection(DataSource dataSource) throws SQLException
getConnection
, 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
- DataSource to get Connection from
SQLException
- if thrown by JDBC methodsgetConnection(DataSource)
,
TransactionAwareDataSourceProxy
public static Integer prepareConnectionForTransaction(Connection con, TransactionDefinition definition) throws SQLException
con
- the Connection to preparedefinition
- the transaction definition to apply
SQLException
- if thrown by JDBC methodsresetConnectionAfterTransaction(java.sql.Connection, java.lang.Integer)
public static void resetConnectionAfterTransaction(Connection con, Integer previousIsolationLevel)
con
- the Connection to resetpreviousIsolationLevel
- the isolation level to restore, if anyprepareConnectionForTransaction(java.sql.Connection, org.springframework.transaction.TransactionDefinition)
public static void applyTransactionTimeout(Statement stmt, DataSource dataSource) throws SQLException
stmt
- the JDBC Statement objectdataSource
- DataSource that the Connection came from
SQLException
Statement.setQueryTimeout(int)
public static void applyTimeout(Statement stmt, DataSource dataSource, int timeout) throws SQLException
stmt
- the JDBC Statement objectdataSource
- DataSource that the Connection came fromtimeout
- the timeout to apply (or 0 for no timeout outside of a transaction)
SQLException
Statement.setQueryTimeout(int)
public static void releaseConnection(Connection con, 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
)SmartDataSource.shouldClose(java.sql.Connection)
public static void doReleaseConnection(Connection con, DataSource dataSource) throws SQLException
releaseConnection
, 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 methodsreleaseConnection(java.sql.Connection, javax.sql.DataSource)
,
TransactionAwareDataSourceProxy
public static Connection getTargetConnection(Connection con)
con
- the Connection proxy to unwrap
ConnectionProxy.getTargetConnection()
|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |