Top Description Fields Constructors Methods
org.springframework.transaction.support

public Class TransactionTemplate

extends DefaultTransactionDefinition
implements TransactionOperations, InitializingBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.InitializingBean, org.springframework.transaction.support.TransactionOperations
Annotations
@SuppressWarnings:serial
Imports
java.lang.reflect.UndeclaredThrowableException, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.factory.InitializingBean, org.springframework.lang.Nullable, org.springframework.transaction.PlatformTransactionManager, .TransactionDefinition, .TransactionException, .TransactionStatus, .TransactionSystemException, org.springframework.util.Assert

Template class that simplifies programmatic transaction demarcation and transaction exception handling.

The central method is execute, supporting transactional code that implements the TransactionCallback interface. This template 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 data access objects that use resources such as JDBC DataSources but are not transaction-aware themselves. Instead, they can implicitly participate in 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 passed 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 given to the prepared template.

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

Author
Juergen Hoeller
Since
17.03.2003
See Also
execute, setTransactionManager, org.springframework.transaction.PlatformTransactionManager

Field Summary

Modifier and TypeField and Description
protected final Log
logger

Logger available to subclasses.

private PlatformTransactionManager
Inherited from org.springframework.transaction.support.DefaultTransactionDefinition:
isolationConstantsPREFIX_ISOLATIONPREFIX_PROPAGATIONPREFIX_TIMEOUTpropagationConstantsREAD_ONLY_MARKER

Constructor Summary

AccessConstructor and Description
public
TransactionTemplate()

Construct a new TransactionTemplate for bean usage.

public
TransactionTemplate(PlatformTransactionManager
the transaction management strategy to be used
transactionManager
)

Construct a new TransactionTemplate using the given transaction manager.

public
TransactionTemplate(PlatformTransactionManager
the transaction management strategy to be used
transactionManager
,
TransactionDefinition
the transaction definition to copy the default settings from. Local properties can still be set to change values.
transactionDefinition
)

Construct a new TransactionTemplate using the given transaction manager, taking its default settings from the given transaction definition.

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides org.springframework.transaction.support.DefaultTransactionDefinition.equals.

This implementation compares the toString() results.
public <T> T
execute(TransactionCallback<T>
the callback object that specifies the transactional action
action
)

Implements org.springframework.transaction.support.TransactionOperations.execute.

Execute the action specified by the given callback object within a transaction.
public PlatformTransactionManager
getTransactionManager()

Return the transaction management strategy to be used.

private void
rollbackOnException(TransactionStatus
object representing the transaction
status
,
Throwable
the thrown application exception or error
ex
)

Perform a rollback, handling rollback exceptions properly.

public void
setTransactionManager(PlatformTransactionManager transactionManager)

Set the transaction management strategy to be used.

Inherited from org.springframework.transaction.support.DefaultTransactionDefinition:
getDefinitionDescriptiongetIsolationLevelgetIsolationLevelNamegetNamegetPropagationBehaviorgetTimeouthashCodeisReadOnlysetIsolationLevelsetIsolationLevelNamesetNamesetPropagationBehaviorsetPropagationBehaviorNamesetReadOnlysetTimeouttoString

Field Detail

loggerback to summary
protected final Log logger

Logger available to subclasses.

transactionManagerback to summary
private PlatformTransactionManager transactionManager
Annotations
@Nullable

Constructor Detail

TransactionTemplateback to summary
public TransactionTemplate()

Construct a new TransactionTemplate for bean usage.

Note

The PlatformTransactionManager needs to be set before any execute calls.

See Also
setTransactionManager
TransactionTemplateback to summary
public TransactionTemplate(PlatformTransactionManager transactionManager)

Construct a new TransactionTemplate using the given transaction manager.

Parameters
transactionManager:PlatformTransactionManager

the transaction management strategy to be used

TransactionTemplateback to summary
public TransactionTemplate(PlatformTransactionManager transactionManager, TransactionDefinition transactionDefinition)

Construct a new TransactionTemplate using the given transaction manager, taking its default settings from the given transaction definition.

Parameters
transactionManager:PlatformTransactionManager

the transaction management strategy to be used

transactionDefinition:TransactionDefinition

the transaction definition to copy the default settings from. Local properties can still be set to change values.

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Doc from org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

Annotations
@Override
equalsback to summary
public boolean equals(Object other)

Overrides org.springframework.transaction.support.DefaultTransactionDefinition.equals.

Doc from org.springframework.transaction.support.DefaultTransactionDefinition.equals.

This implementation compares the toString() results.

Parameters
other:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
executeback to summary
public <T> T execute(TransactionCallback<T> action) throws TransactionException

Implements org.springframework.transaction.support.TransactionOperations.execute.

Doc from org.springframework.transaction.support.TransactionOperations.execute.

Execute the action specified by the given callback object within a transaction.

Allows for returning a result object created within the transaction, that is, a domain object or a collection of domain objects. A RuntimeException thrown by the callback is treated as a fatal exception that enforces a rollback. Such an exception gets propagated to the caller of the template.

Parameters
action:TransactionCallback<T>

the callback object that specifies the transactional action

Returns:T

a result object returned by the callback, or null if none

Annotations
@Override
@Nullable
Exceptions
TransactionException:
in case of initialization, rollback, or system errors
getTransactionManagerback to summary
public PlatformTransactionManager getTransactionManager()

Return the transaction management strategy to be used.

Annotations
@Nullable
rollbackOnExceptionback to summary
private void rollbackOnException(TransactionStatus status, Throwable ex) throws TransactionException

Perform a rollback, handling rollback exceptions properly.

Parameters
status:TransactionStatus

object representing the transaction

ex:Throwable

the thrown application exception or error

Exceptions
TransactionException:
in case of a rollback error
setTransactionManagerback to summary
public void setTransactionManager(PlatformTransactionManager transactionManager)

Set the transaction management strategy to be used.