org.springframework.jdbc.datasource
Class TransactionAwareDataSourceProxy
java.lang.Object
org.springframework.jdbc.datasource.DelegatingDataSource
org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy
- All Implemented Interfaces:
- DataSource, InitializingBean
- public class TransactionAwareDataSourceProxy
- extends DelegatingDataSource
Proxy for a target DataSource, adding awareness of Spring-managed transactions.
Similar to a transactional JNDI DataSource as provided by a J2EE server.
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 work 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 DataAccessUtils, 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 Statements.
This means that all operations performed by the SqlMapClient will
automatically participate in Spring-managed transaction timeouts.
NOTE: This DataSource proxy needs to return wrapped Connections to
handle close calls on them 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.
- Since:
- 1.1
- Author:
- Juergen Hoeller
- See Also:
DataSource.getConnection()
,
Connection.close()
,
DataSourceUtils.doGetConnection(javax.sql.DataSource, boolean)
,
DataSourceUtils.doCloseConnectionIfNecessary(java.sql.Connection, javax.sql.DataSource)
,
DataSourceUtils.applyTransactionTimeout(java.sql.Statement, javax.sql.DataSource)
,
ConnectionProxy
,
NativeJdbcExtractor
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
TransactionAwareDataSourceProxy
public TransactionAwareDataSourceProxy()
- Create a new TransactionAwareDataSourceProxy.
- See Also:
DelegatingDataSource.setTargetDataSource(javax.sql.DataSource)
TransactionAwareDataSourceProxy
public TransactionAwareDataSourceProxy(DataSource targetDataSource)
- Create a new TransactionAwareDataSourceProxy.
- Parameters:
targetDataSource
- the target DataSource
getConnection
public Connection getConnection()
throws SQLException
- Delegate 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.
- Specified by:
getConnection
in interface DataSource
- Overrides:
getConnection
in class DelegatingDataSource
- Returns:
- a transactional Connection if any, a new one else
- Throws:
SQLException
- See Also:
DataSourceUtils.doGetConnection(javax.sql.DataSource, boolean)
,
ConnectionProxy.getTargetConnection()
getTransactionAwareConnectionProxy
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.
- Parameters:
target
- the original Connection to wrapdataSource
- DataSource that the Connection came from
- Returns:
- the wrapped Connection
- See Also:
DataSourceUtils.doCloseConnectionIfNecessary(java.sql.Connection, javax.sql.DataSource)
Copyright (C) 2003-2004 The Spring Framework Project.