com.interface21.transaction.support
Class TransactionTemplate

java.lang.Object
  |
  +--com.interface21.transaction.support.DefaultTransactionDefinition
        |
        +--com.interface21.transaction.support.TransactionTemplate
All Implemented Interfaces:
TransactionDefinition

public class TransactionTemplate
extends DefaultTransactionDefinition

Helper class that simplifies programmatic transaction demarcation (via the "execute" method) and exception handling.

The central method is "execute", supporting transactional code implementing the TransactionCallback interface. It handles the transaction lifecycle and possible exceptions such that neither the TransactionCallback implementation nor the calling code needs to explicitly handle transactions.

Typical usage: Allows for writing low-level application services that use (JNDI) resources but are not transaction-aware themselves. Instead, they can implicitly take part in (JTA) transactions handled by higher-level application services utilizing this class, making calls to the low-level services via an inner-class callback object.

Can be used within a service implementation via direct instantiation with a transaction manager reference, or get prepared in an application context and given to services as bean reference. Note: The transaction manager should always be configured as bean in the application context, in the first case given to the service directly, in the second case to the prepared template.

Supports setting the propagation behavior and the isolation level by name, for convenient configuration in context definitions.

Since:
17.03.2003
Author:
Juergen Hoeller
See Also:
TransactionCallback, PlatformTransactionManager, JtaTransactionManager

Field Summary
protected  PlatformTransactionManager transactionManager
           
 
Fields inherited from interface com.interface21.transaction.TransactionDefinition
ISOLATION_CONSTANT_PREFIX, ISOLATION_DEFAULT, ISOLATION_READ_COMMITTED, ISOLATION_READ_UNCOMMITTED, ISOLATION_REPEATABLE_READ, ISOLATION_SERIALIZABLE, PROPAGATION_CONSTANT_PREFIX, PROPAGATION_MANDATORY, PROPAGATION_REQUIRED, PROPAGATION_SUPPORTS, TIMEOUT_DEFAULT
 
Constructor Summary
TransactionTemplate()
          Create a new TransactionTemplate instance.
TransactionTemplate(PlatformTransactionManager transactionManager)
          Create a new TransactionTemplate instance.
 
Method Summary
 java.lang.Object execute(TransactionCallback action)
          Executes the action specified by the given callback object within a transaction.
 PlatformTransactionManager getTransactionManager()
          Return the transaction manager to be used.
 void setIsolationLevelName(java.lang.String constantName)
          Set the isolation level by the name of the respective constant in PlatformTransactionManager (e.g.
 void setPropagationBehaviorName(java.lang.String constantName)
          Set the propagation behavior by the name of the respective constant in PlatformTransactionManager (e.g.
 void setTransactionManager(PlatformTransactionManager transactionManager)
          Set the transaction manager to be used.
 
Methods inherited from class com.interface21.transaction.support.DefaultTransactionDefinition
getIsolationLevel, getPropagationBehavior, getTimeout, setIsolationLevel, setPropagationBehavior, setTimeout
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

transactionManager

protected PlatformTransactionManager transactionManager
Constructor Detail

TransactionTemplate

public TransactionTemplate()
Create a new TransactionTemplate instance. Mainly targetted at configuration by a bean factory. Note: Transaction manager property needs to be set before any execute calls.
See Also:
setTransactionManager(com.interface21.transaction.PlatformTransactionManager)

TransactionTemplate

public TransactionTemplate(PlatformTransactionManager transactionManager)
Create a new TransactionTemplate instance.
Parameters:
transactionManager - transaction manager to be used
See Also:
PlatformTransactionManager
Method Detail

setTransactionManager

public void setTransactionManager(PlatformTransactionManager transactionManager)
Set the transaction manager to be used.

getTransactionManager

public PlatformTransactionManager getTransactionManager()
Return the transaction manager to be used.

setPropagationBehaviorName

public void setPropagationBehaviorName(java.lang.String constantName)
                                throws java.lang.IllegalArgumentException
Set the propagation behavior by the name of the respective constant in PlatformTransactionManager (e.g. "PROPAGATION_REQUIRED");
Parameters:
constantName - name of the constant
Throws:
java.lang.IllegalArgumentException - if an invalid constant was specified
See Also:
PlatformTransactionManager

setIsolationLevelName

public void setIsolationLevelName(java.lang.String constantName)
                           throws java.lang.IllegalArgumentException
Set the isolation level by the name of the respective constant in PlatformTransactionManager (e.g. "ISOLATION_DEFAULT");
Parameters:
constantName - name of the constant
Throws:
java.lang.IllegalArgumentException - if an invalid constant was specified
See Also:
PlatformTransactionManager

execute

public java.lang.Object execute(TransactionCallback action)
                         throws TransactionException,
                                java.lang.RuntimeException
Executes the action specified by the given callback object within a transaction. Application exceptions thrown by the callback object get propagated to the caller. Allows for returning a result object created within the transaction, i.e. a business object or a collection of business objects.
Parameters:
action - callback object that specifies the transactional action
Returns:
a result object returned by the callback, or null
Throws:
TransactionException - in case of initialization, rollback, or system errors
java.lang.RuntimeException - in case of application exceptions thrown by the callback object


Rod Johnson and Spring contributors 2001-2003.