类 TransactionAwareDataSourceProxy
- java.lang.Object
- org.springframework.jdbc.datasource.DelegatingDataSource
- org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy
- 所有已实现的接口:
Wrapper
,CommonDataSource
,DataSource
,InitializingBean
public class TransactionAwareDataSourceProxy extends DelegatingDataSource
Proxy for a target JDBCDataSource
, adding awareness of Spring-managed transactions. Similar to a transactional JNDI DataSource as provided by a Java EE server.Data access code that should remain unaware of Spring's data access support can work with this proxy to seamlessly participate in Spring-managed transactions. Note that the transaction manager, for example
DataSourceTransactionManager
, still needs to work with the underlying DataSource, not with this proxy.Make sure that TransactionAwareDataSourceProxy is the outermost DataSource of a chain of DataSource proxies/adapters. TransactionAwareDataSourceProxy can delegate either directly to the target connection pool or to some intermediary proxy/adapter like
LazyConnectionDataSourceProxy
orUserCredentialsDataSourceAdapter
.Delegates to
DataSourceUtils
for automatically participating in thread-bound transactions, for example managed byDataSourceTransactionManager
.getConnection
calls andclose
calls on returned Connections will behave properly within a transaction, i.e. always operate on the transactional Connection. If not within a transaction, normal DataSource behavior applies.This proxy allows data access code to work with the plain JDBC API and still participate in Spring-managed transactions, similar to JDBC code in a Java EE/JTA environment. However, if possible, use Spring's DataSourceUtils, JdbcTemplate or JDBC operation objects to get transaction participation even without a proxy for the target DataSource, avoiding the need to define such a proxy in the first place.
As a further effect, using a transaction-aware DataSource will apply remaining transaction timeouts to all created JDBC (Prepared/Callable)Statement. This means that all operations performed through standard JDBC will automatically participate in Spring-managed transaction timeouts.
NOTE: This DataSource proxy needs to return wrapped Connections (which implement the
ConnectionProxy
interface) in order to handle close calls properly. Therefore, the returned Connections cannot be cast to a native JDBC Connection type such as OracleConnection or to a connection pool implementation type. Use a correspondingNativeJdbcExtractor
or JDBC 4'sWrapper.unwrap(java.lang.Class<T>)
to retrieve the native JDBC Connection.
构造器概要
构造器 构造器 说明 TransactionAwareDataSourceProxy()
Create a new TransactionAwareDataSourceProxy.TransactionAwareDataSourceProxy(DataSource targetDataSource)
Create a new TransactionAwareDataSourceProxy.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 Connection
getConnection()
Delegates to DataSourceUtils for automatically participating in Spring-managed transactions.protected Connection
getTransactionAwareConnectionProxy(DataSource targetDataSource)
Wraps the given Connection with a proxy that delegates every method call to it but delegatesclose()
calls to DataSourceUtils.void
setReobtainTransactionalConnections(boolean reobtainTransactionalConnections)
Specify whether to reobtain the target Connection for each operation performed within a transaction.protected boolean
shouldObtainFixedConnection(DataSource targetDataSource)
Determine whether to obtain a fixed target Connection for the proxy or to reobtain the target Connection for each operation.从类继承的方法 org.springframework.jdbc.datasource.DelegatingDataSource
afterPropertiesSet, getConnection, getLoginTimeout, getLogWriter, getParentLogger, getTargetDataSource, isWrapperFor, setLoginTimeout, setLogWriter, setTargetDataSource, unwrap
从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
从接口继承的方法 javax.sql.CommonDataSource
createShardingKeyBuilder
从接口继承的方法 javax.sql.DataSource
createConnectionBuilder
构造器详细资料
TransactionAwareDataSourceProxy
public TransactionAwareDataSourceProxy()
Create a new TransactionAwareDataSourceProxy.
TransactionAwareDataSourceProxy
public TransactionAwareDataSourceProxy(DataSource targetDataSource)
Create a new TransactionAwareDataSourceProxy.- 参数:
targetDataSource
- the target DataSource
方法详细资料
setReobtainTransactionalConnections
public void setReobtainTransactionalConnections(boolean reobtainTransactionalConnections)
Specify whether to reobtain the target Connection for each operation performed within a transaction.The default is "false". Specify "true" to reobtain transactional Connections for every call on the Connection proxy; this is advisable on JBoss if you hold on to a Connection handle across transaction boundaries.
The effect of this setting is similar to the "hibernate.connection.release_mode" value "after_statement".
getConnection
public Connection getConnection() throws SQLException
Delegates to DataSourceUtils for automatically participating in Spring-managed transactions. Throws the original SQLException, if any.The returned Connection handle implements the ConnectionProxy interface, allowing to retrieve the underlying target Connection.
- 指定者:
getConnection
在接口中DataSource
- 覆盖:
getConnection
在类中DelegatingDataSource
- 返回:
- a transactional Connection if any, a new one else
- 抛出:
SQLException
- 另请参阅:
DataSourceUtils.doGetConnection(javax.sql.DataSource)
,ConnectionProxy.getTargetConnection()
getTransactionAwareConnectionProxy
protected Connection getTransactionAwareConnectionProxy(DataSource targetDataSource)
Wraps the given Connection with a proxy that delegates every method call to it but delegatesclose()
calls to DataSourceUtils.- 参数:
targetDataSource
- the DataSource that the Connection came from- 返回:
- the wrapped Connection
- 另请参阅:
Connection.close()
,DataSourceUtils.doReleaseConnection(java.sql.Connection, javax.sql.DataSource)
shouldObtainFixedConnection
protected boolean shouldObtainFixedConnection(DataSource targetDataSource)
Determine whether to obtain a fixed target Connection for the proxy or to reobtain the target Connection for each operation.The default implementation returns
true
for all standard cases. This can be overridden through the"reobtainTransactionalConnections"
flag, which enforces a non-fixed target Connection within an active transaction. Note that non-transactional access will always use a fixed Connection.- 参数:
targetDataSource
- the target DataSource