Class TransactionTemplate

All Implemented Interfaces:
Serializable, InitializingBean, TransactionOperations, TransactionDefinition

public class TransactionTemplate extends DefaultTransactionDefinition implements TransactionOperations, InitializingBean
Template class that simplifies programmatic transaction demarcation and transaction exception handling.

The central method is execute(<T>), 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.

Juergen Hoeller
See Also:
  • Field Details

    • logger

      protected final Log logger
      Logger available to subclasses.
  • Constructor Details

    • TransactionTemplate

      public TransactionTemplate()
      Construct a new TransactionTemplate for bean usage.

      Note: The PlatformTransactionManager needs to be set before any execute calls.

      See Also:
    • TransactionTemplate

      public TransactionTemplate(PlatformTransactionManager transactionManager)
      Construct a new TransactionTemplate using the given transaction manager.
      transactionManager - the transaction management strategy to be used
    • TransactionTemplate

      public TransactionTemplate(PlatformTransactionManager transactionManager, TransactionDefinition transactionDefinition)
      Construct a new TransactionTemplate using the given transaction manager, taking its default settings from the given transaction definition.
      transactionManager - the transaction management strategy to be used
      transactionDefinition - the transaction definition to copy the default settings from. Local properties can still be set to change values.
  • Method Details