类 DataSourceUtils
- java.lang.Object
- org.springframework.jdbc.datasource.DataSourceUtils
public abstract class DataSourceUtils extends Object
Helper class that provides static methods for obtaining JDBC Connections from aDataSource
. Includes special support for Spring-managed transactional Connections, e.g. managed byDataSourceTransactionManager
orJtaTransactionManager
.Used internally by Spring's
JdbcTemplate
, Spring's JDBC operation objects and the JDBCDataSourceTransactionManager
. Can also be used directly in application code.
字段概要
字段 修饰符和类型 字段 说明 static int
CONNECTION_SYNCHRONIZATION_ORDER
Order value for TransactionSynchronization objects that clean up JDBC Connections.
构造器概要
构造器 构造器 说明 DataSourceUtils()
方法概要
所有方法 静态方法 具体方法 已过时的方法 修饰符和类型 方法 说明 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 aSmartDataSource
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)
已过时。as of 5.1.11, in favor ofresetConnectionAfterTransaction(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.
字段详细资料
CONNECTION_SYNCHRONIZATION_ORDER
public static final int CONNECTION_SYNCHRONIZATION_ORDER
Order value for TransactionSynchronization objects that clean up JDBC Connections.- 另请参阅:
- 常量字段值
构造器详细资料
DataSourceUtils
public DataSourceUtils()
方法详细资料
getConnection
public 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, e.g. when running within aJTA
transaction).- 参数:
dataSource
- the DataSource to obtain Connections from- 返回:
- a JDBC Connection from the given DataSource
- 抛出:
CannotGetJdbcConnectionException
- if the attempt to get a Connection failed- 另请参阅:
releaseConnection(java.sql.Connection, javax.sql.DataSource)
doGetConnection
public static Connection doGetConnection(DataSource dataSource) throws SQLException
Actually obtain a JDBC Connection from the given DataSource. Same asgetConnection(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 from- 返回:
- a JDBC Connection from the given DataSource
- 抛出:
SQLException
- if thrown by JDBC methods- 另请参阅:
doReleaseConnection(java.sql.Connection, javax.sql.DataSource)
prepareConnectionForTransaction
@Nullable public static Integer prepareConnectionForTransaction(Connection con, @Nullable TransactionDefinition definition) throws SQLException
Prepare the given Connection with the given transaction semantics.- 参数:
con
- the Connection to preparedefinition
- the transaction definition to apply- 返回:
- the previous isolation level, if any
- 抛出:
SQLException
- if thrown by JDBC methods- 另请参阅:
resetConnectionAfterTransaction(java.sql.Connection, java.lang.Integer, boolean)
,Connection.setTransactionIsolation(int)
,Connection.setReadOnly(boolean)
resetConnectionAfterTransaction
public static void resetConnectionAfterTransaction(Connection con, @Nullable Integer previousIsolationLevel, boolean resetReadOnly)
Reset the given Connection after a transaction, regarding read-only flag and isolation level.- 参数:
con
- the Connection to resetpreviousIsolationLevel
- the isolation level to restore, if anyresetReadOnly
- whether to reset the connection's read-only flag- 从以下版本开始:
- 5.2.1
- 另请参阅:
prepareConnectionForTransaction(java.sql.Connection, org.springframework.transaction.TransactionDefinition)
,Connection.setTransactionIsolation(int)
,Connection.setReadOnly(boolean)
resetConnectionAfterTransaction
@Deprecated public static void resetConnectionAfterTransaction(Connection con, @Nullable Integer previousIsolationLevel)
已过时。as of 5.1.11, in favor ofresetConnectionAfterTransaction(Connection, Integer, boolean)
Reset the given Connection after a transaction, regarding read-only flag and isolation level.- 参数:
con
- the Connection to resetpreviousIsolationLevel
- the isolation level to restore, if any
isConnectionTransactional
public static boolean isConnectionTransactional(Connection con, @Nullable DataSource dataSource)
Determine whether the given JDBC Connection is transactional, that is, bound to the current thread by Spring's transaction facilities.- 参数:
con
- the Connection to checkdataSource
- the DataSource that the Connection was obtained from (may benull
)- 返回:
- whether the Connection is transactional
applyTransactionTimeout
public static void applyTransactionTimeout(Statement stmt, @Nullable DataSource dataSource) throws SQLException
Apply the current transaction timeout, if any, to the given JDBC Statement object.- 参数:
stmt
- the JDBC Statement objectdataSource
- the DataSource that the Connection was obtained from- 抛出:
SQLException
- if thrown by JDBC methods- 另请参阅:
Statement.setQueryTimeout(int)
applyTimeout
public 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.- 参数:
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 methods- 另请参阅:
Statement.setQueryTimeout(int)
releaseConnection
public static void releaseConnection(@Nullable Connection con, @Nullable DataSource dataSource)
Close the given Connection, obtained from the given DataSource, if it is not managed externally (that is, not bound to the thread).- 参数:
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
)- 另请参阅:
getConnection(javax.sql.DataSource)
doReleaseConnection
public 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.Directly accessed by
TransactionAwareDataSourceProxy
.- 参数:
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
)- 抛出:
SQLException
- if thrown by JDBC methods- 另请参阅:
doGetConnection(javax.sql.DataSource)
doCloseConnection
public static void doCloseConnection(Connection con, @Nullable DataSource dataSource) throws SQLException
Close the Connection, unless aSmartDataSource
doesn't want us to.- 参数:
con
- the Connection to close if necessarydataSource
- the DataSource that the Connection was obtained from- 抛出:
SQLException
- if thrown by JDBC methods- 另请参阅:
Connection.close()
,SmartDataSource.shouldClose(Connection)
getTargetConnection
public static Connection getTargetConnection(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.- 参数:
con
- the Connection proxy to unwrap- 返回:
- the innermost target Connection, or the passed-in one if no proxy
- 另请参阅:
ConnectionProxy.getTargetConnection()