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.
execute, setTransactionManager, org.springframework.transaction.PlatformTransactionManager
| Modifier and Type | Field and Description |
|---|---|
| protected final Log | logger
Logger available to subclasses. |
| private PlatformTransactionManager |
| Access | Constructor and Description |
|---|---|
| public | |
| 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. |
| Modifier and Type | Method and Description |
|---|---|
| public void | afterPropertiesSet()
Implements org. Invoked by the containing 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. This implementation compares the |
| public <T> T | execute(TransactionCallback<T>
the callback object that specifies the transactional action action)Implements org. Execute the action specified by the given callback object within a transaction. |
| public PlatformTransactionManager | |
| 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. |
| logger | back to summary |
|---|---|
| protected final Log logger Logger available to subclasses. | |
| transactionManager | back to summary |
|---|---|
| private PlatformTransactionManager transactionManager
| |
| TransactionTemplate | back to summary |
|---|---|
| public TransactionTemplate() Construct a new TransactionTemplate for bean usage. Note The PlatformTransactionManager needs to be set before
any
| |
| TransactionTemplate | back to summary |
|---|---|
| public TransactionTemplate(PlatformTransactionManager transactionManager) Construct a new TransactionTemplate using the given transaction manager.
| |
| TransactionTemplate | back 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.
| |
| afterPropertiesSet | back to summary |
|---|---|
| public void afterPropertiesSet() Implements org. Doc from org. Invoked by the containing This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object other) Overrides org. Doc from org. This implementation compares the
| |
| execute | back to summary |
|---|---|
| public <T> T execute(TransactionCallback<T> action) throws TransactionException Implements org. Doc from org. 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.
| |
| getTransactionManager | back to summary |
|---|---|
| public PlatformTransactionManager getTransactionManager() Return the transaction management strategy to be used.
| |
| rollbackOnException | back to summary |
|---|---|
| private void rollbackOnException(TransactionStatus status, Throwable ex) throws TransactionException Perform a rollback, handling rollback exceptions properly.
| |
| setTransactionManager | back to summary |
|---|---|
| public void setTransactionManager(PlatformTransactionManager transactionManager) Set the transaction management strategy to be used. | |