|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.jdbc.datasource.DelegatingDataSource org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy
public class TransactionAwareDataSourceProxy
Proxy for a target JDBC DataSource
, adding awareness of
Spring-managed transactions. Similar to a transactional JNDI DataSource
as provided by a J2EE 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
or
UserCredentialsDataSourceAdapter
.
Delegates to DataSourceUtils
for automatically participating in
thread-bound transactions, for example managed by DataSourceTransactionManager
.
getConnection
calls and close
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 J2EE/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 like OracleConnection or to a connection
pool implementation type. Use a corresponding
NativeJdbcExtractor
to retrieve the native JDBC Connection.
DataSource.getConnection()
,
Connection.close()
,
DataSourceUtils.doGetConnection(javax.sql.DataSource)
,
DataSourceUtils.applyTransactionTimeout(java.sql.Statement, javax.sql.DataSource)
,
DataSourceUtils.doReleaseConnection(java.sql.Connection, javax.sql.DataSource)
Constructor Summary | |
---|---|
TransactionAwareDataSourceProxy()
Create a new TransactionAwareDataSourceProxy. |
|
TransactionAwareDataSourceProxy(DataSource targetDataSource)
Create a new TransactionAwareDataSourceProxy. |
Method Summary | |
---|---|
Connection |
getConnection()
Delegate to DataSourceUtils for automatically participating in Spring-managed transactions. |
protected Connection |
getTransactionAwareConnectionProxy(Connection target,
DataSource dataSource)
Wrap the given Connection with a proxy that delegates every method call to it but delegates close calls to DataSourceUtils. |
Methods inherited from class org.springframework.jdbc.datasource.DelegatingDataSource |
---|
afterPropertiesSet, getConnection, getLoginTimeout, getLogWriter, getTargetDataSource, setLoginTimeout, setLogWriter, setTargetDataSource |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public TransactionAwareDataSourceProxy()
DelegatingDataSource.setTargetDataSource(javax.sql.DataSource)
public TransactionAwareDataSourceProxy(DataSource targetDataSource)
targetDataSource
- the target DataSourceMethod Detail |
---|
public Connection getConnection() throws SQLException
The returned Connection handle implements the ConnectionProxy interface, allowing to retrieve the underlying target Connection.
getConnection
in interface DataSource
getConnection
in class DelegatingDataSource
SQLException
DataSourceUtils.doGetConnection(javax.sql.DataSource)
,
ConnectionProxy.getTargetConnection()
protected Connection getTransactionAwareConnectionProxy(Connection target, DataSource dataSource)
close
calls to DataSourceUtils.
target
- the original Connection to wrapdataSource
- DataSource that the Connection came from
Connection.close()
,
DataSourceUtils.doReleaseConnection(java.sql.Connection, javax.sql.DataSource)
|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |