com.interface21.transaction.datasource
Class DataSourceTransactionManager

java.lang.Object
  |
  +--com.interface21.transaction.support.AbstractPlatformTransactionManager
        |
        +--com.interface21.transaction.datasource.DataSourceTransactionManager
All Implemented Interfaces:
InitializingBean, PlatformTransactionManager

public class DataSourceTransactionManager
extends AbstractPlatformTransactionManager
implements InitializingBean

PlatformTransactionManager implementation for single data sources. Binds a JDBC connection from the specified data source to the thread, potentially allowing for one thread connection per data source. Supports custom isolation levels but not timeouts.

Application code is required to retrieve the JDBC connection via DataSourceUtils.getConnection(DataSource) instead of J2EE's standard DataSource.getConnection. This is recommended anyway, as it throws unchecked com.interface21.dao exceptions instead of checked SQLException. All framework classes like JdbcTemplate use this strategy implicitly. If not used with this transaction manager, the lookup strategy behaves exactly like the common one - it can thus be used in any case.

This implementation can be used instead of JtaTransactionManager in the single resource case, as it does not require the container to support JTA. Switching between both is just a matter of configuration, if you stick to the required connection lookup pattern.

Since:
02.05.2003
Author:
Juergen Hoeller
See Also:
DataSourceUtils.getConnection(javax.sql.DataSource), SingleConnectionDataSource, ThreadObjectManager

Fields inherited from class com.interface21.transaction.support.AbstractPlatformTransactionManager
logger
 
Constructor Summary
DataSourceTransactionManager()
          Create a new DataSourceTransactionManager instance.
DataSourceTransactionManager(javax.sql.DataSource dataSource)
          Create a new DataSourceTransactionManager instance.
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied.
protected  void doBegin(java.lang.Object transaction, int isolationLevel, int timeout)
          This implementation sets the isolation level but ignores the timeout.
protected  void doCommit(TransactionStatus status)
          Perform an actual commit on the given transaction.
protected  java.lang.Object doGetTransaction()
          Return a current transaction object, i.e. a JTA UserTransaction.
protected  void doRollback(TransactionStatus status)
          Perform an actual rollback on the given transaction.
protected  void doSetRollbackOnly(TransactionStatus status)
          Set the given transaction rollback-only.
 javax.sql.DataSource getDataSource()
          Return the J2EE DataSource that this instance manages transactions for.
protected  boolean isExistingTransaction(java.lang.Object transaction)
          Check if the given transaction object indicates an existing, i.e. already begun, transaction.
 void setDataSource(javax.sql.DataSource dataSource)
          Set the J2EE DataSource that this instance should manage transactions for.
 
Methods inherited from class com.interface21.transaction.support.AbstractPlatformTransactionManager
commit, getAllowNonTransactionalExecution, getTransaction, getTransactionSynchronization, rollback, setAllowNonTransactionalExecution, setTransactionSynchronization
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DataSourceTransactionManager

public DataSourceTransactionManager()
Create a new DataSourceTransactionManager instance. A DataSource has to be set to be able to use it.
See Also:
setDataSource(javax.sql.DataSource)

DataSourceTransactionManager

public DataSourceTransactionManager(javax.sql.DataSource dataSource)
Create a new DataSourceTransactionManager instance.
Parameters:
dataSource - DataSource to manage transactions for
Method Detail

setDataSource

public final void setDataSource(javax.sql.DataSource dataSource)
Set the J2EE DataSource that this instance should manage transactions for.

getDataSource

public javax.sql.DataSource getDataSource()
Return the J2EE DataSource that this instance manages transactions for.

afterPropertiesSet

public void afterPropertiesSet()
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied.
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
Specified by:
afterPropertiesSet in interface InitializingBean
Following copied from interface: com.interface21.beans.factory.InitializingBean
Throws:
java.lang.Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails.

doGetTransaction

protected java.lang.Object doGetTransaction()
Description copied from class: AbstractPlatformTransactionManager
Return a current transaction object, i.e. a JTA UserTransaction.
Overrides:
doGetTransaction in class AbstractPlatformTransactionManager
Following copied from class: com.interface21.transaction.support.AbstractPlatformTransactionManager
Returns:
the current transaction object
Throws:
CannotCreateTransactionException - if transaction support is not available (e.g. no JTA UserTransaction retrievable from JNDI)
TransactionException - in case of lookup or system errors

isExistingTransaction

protected boolean isExistingTransaction(java.lang.Object transaction)
Description copied from class: AbstractPlatformTransactionManager
Check if the given transaction object indicates an existing, i.e. already begun, transaction.
Overrides:
isExistingTransaction in class AbstractPlatformTransactionManager
Following copied from class: com.interface21.transaction.support.AbstractPlatformTransactionManager
Parameters:
transaction - transaction object returned by doGetTransaction()
Returns:
if there is an existing transaction
Throws:
TransactionException - in case of system errors

doBegin

protected void doBegin(java.lang.Object transaction,
                       int isolationLevel,
                       int timeout)
This implementation sets the isolation level but ignores the timeout.
Overrides:
doBegin in class AbstractPlatformTransactionManager
Following copied from class: com.interface21.transaction.support.AbstractPlatformTransactionManager
Parameters:
transaction - transaction object returned by doGetTransaction()
isolationLevel - desired isolation level
timeout - transaction timeout (in seconds)
Throws:
TransactionException - in case of creation or system errors

doCommit

protected void doCommit(TransactionStatus status)
Description copied from class: AbstractPlatformTransactionManager
Perform an actual commit on the given transaction. An implementation does not need to check the rollback-only flag.
Overrides:
doCommit in class AbstractPlatformTransactionManager
Following copied from class: com.interface21.transaction.support.AbstractPlatformTransactionManager
Parameters:
status - status representation of the transaction
Throws:
TransactionException - in case of commit or system errors

doRollback

protected void doRollback(TransactionStatus status)
Description copied from class: AbstractPlatformTransactionManager
Perform an actual rollback on the given transaction. An implementation does not need to check the new transaction flag.
Overrides:
doRollback in class AbstractPlatformTransactionManager
Following copied from class: com.interface21.transaction.support.AbstractPlatformTransactionManager
Parameters:
status - status representation of the transaction
Throws:
TransactionException - in case of system errors

doSetRollbackOnly

protected void doSetRollbackOnly(TransactionStatus status)
Description copied from class: AbstractPlatformTransactionManager
Set the given transaction rollback-only. Only called on rollback if the current transaction takes part in an existing one.
Overrides:
doSetRollbackOnly in class AbstractPlatformTransactionManager
Following copied from class: com.interface21.transaction.support.AbstractPlatformTransactionManager
Parameters:
status - status representation of the transaction
Throws:
TransactionException - in case of system errors


Rod Johnson and Spring contributors 2001-2003.