com.interface21.transaction.jta
Class JtaTransactionManager

java.lang.Object
  |
  +--com.interface21.transaction.support.AbstractPlatformTransactionManager
        |
        +--com.interface21.transaction.jta.JtaTransactionManager
All Implemented Interfaces:
PlatformTransactionManager

public class JtaTransactionManager
extends AbstractPlatformTransactionManager

PlatformTransactionManager implementation for JTA.

Set allowNonTransactionalExecution to be able to fall back to non-transactional execution if JTA isn't available in the container. Transaction synchronization is active by default, as it is typically used for transactional cache handling with JTA (e.g. with Hibernate).

This transaction manager is appropriate for handling distributed transactions, i.e. transactions that span multiple resources, and for managing transactions on a J2EE Connector (e.g. a persistence toolkit registered as Connector). For a single JDBC DataSource, DataSourceTransactionManager is perfectly sufficient, and for accessing a single resource with Hibernate (including transactional cache), HibernateTransactionManager is appropriate.

Note: This implementation does not handle isolation levels. This needs to be done by server-specific subclasses, overriding applyIsolationLevel. DataSourceTransactionManager and HibernateTransactionManager do support custom isolation levels, though.

Since:
24.03.2003
Author:
Juergen Hoeller
See Also:
AbstractPlatformTransactionManager.setAllowNonTransactionalExecution(boolean), AbstractPlatformTransactionManager.setTransactionSynchronization(boolean), applyIsolationLevel(javax.transaction.UserTransaction, int), DataSourceTransactionManager, HibernateTransactionManager

Field Summary
static java.lang.String DEFAULT_USER_TRANSACTION_NAME
           
 
Fields inherited from class com.interface21.transaction.support.AbstractPlatformTransactionManager
logger
 
Constructor Summary
JtaTransactionManager()
          Create a new JtaTransactionManager instance.
 
Method Summary
protected  void applyIsolationLevel(javax.transaction.UserTransaction ut, int isolationLevel)
          Initialize the given UserTransaction with the given isolation level.
protected  void doBegin(java.lang.Object transaction, int isolationLevel, int timeout)
          Begin a new transaction with the given isolation level.
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.
protected  boolean isExistingTransaction(java.lang.Object transaction)
          Check if the given transaction object indicates an existing, i.e. already begun, transaction.
 void setJndiTemplate(JndiTemplate jndiTemplate)
          Set the JndiTemplate to use for JNDI lookup.
 void setUserTransactionName(java.lang.String userTransactionName)
          Set the JNDI name of the UserTransaction.
 
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
 

Field Detail

DEFAULT_USER_TRANSACTION_NAME

public static final java.lang.String DEFAULT_USER_TRANSACTION_NAME
Constructor Detail

JtaTransactionManager

public JtaTransactionManager()
Create a new JtaTransactionManager instance.
Method Detail

setJndiTemplate

public final void setJndiTemplate(JndiTemplate jndiTemplate)
Set the JndiTemplate to use for JNDI lookup. A default one is used if not set.

setUserTransactionName

public void setUserTransactionName(java.lang.String userTransactionName)
Set the JNDI name of the UserTransaction. The default one is used if not set.
See Also:
DEFAULT_USER_TRANSACTION_NAME

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)
Description copied from class: AbstractPlatformTransactionManager
Begin a new transaction with the given isolation level.
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

applyIsolationLevel

protected void applyIsolationLevel(javax.transaction.UserTransaction ut,
                                   int isolationLevel)
Initialize the given UserTransaction with the given isolation level.

This standard JTA implementation simply ignores the isolation level. To be overridden by server-specific subclasses that actually handle the isolation level.

Parameters:
ut - UserTransaction instance representing the JTA transaction
isolationLevel - the isolation level to set

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.