站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Spring Framework 1.2.9 API 文档英文版

TransactionTemplate (Spring Framework) - Spring Framework 1.2.9 API 文档英文版


org.springframework.transaction.support
Class TransactionTemplate

java.lang.Object
  extended by org.springframework.transaction.support.DefaultTransactionDefinition
      extended by org.springframework.transaction.support.TransactionTemplate
All Implemented Interfaces:
Serializable, InitializingBean, TransactionDefinition

public class TransactionTemplate
extends DefaultTransactionDefinition
implements InitializingBean

Helper class that simplifies programmatic transaction demarcation and transaction 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, Serialized Form

Field Summary
protected  Log logger
           
 
Fields inherited from class org.springframework.transaction.support.DefaultTransactionDefinition
READ_ONLY_MARKER, TIMEOUT_PREFIX
 
Fields inherited from interface org.springframework.transaction.TransactionDefinition
ISOLATION_CONSTANT_PREFIX, ISOLATION_DEFAULT, ISOLATION_READ_COMMITTED, ISOLATION_READ_UNCOMMITTED, ISOLATION_REPEATABLE_READ, ISOLATION_SERIALIZABLE, PROPAGATION_CONSTANT_PREFIX, PROPAGATION_MANDATORY, PROPAGATION_NESTED, PROPAGATION_NEVER, PROPAGATION_NOT_SUPPORTED, PROPAGATION_REQUIRED, PROPAGATION_REQUIRES_NEW, PROPAGATION_SUPPORTS, TIMEOUT_DEFAULT
 
Constructor Summary
TransactionTemplate()
          Create a new TransactionTemplate instance.
TransactionTemplate(PlatformTransactionManager transactionManager)
          Create a new TransactionTemplate instance.
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
 Object execute(TransactionCallback action)
          Execute the action specified by the given callback object within a transaction.
 PlatformTransactionManager getTransactionManager()
          Return the transaction management strategy to be used.
 void setTransactionManager(PlatformTransactionManager transactionManager)
          Set the transaction management strategy to be used.
 
Methods inherited from class org.springframework.transaction.support.DefaultTransactionDefinition
equals, getDefinitionDescription, getIsolationLevel, getName, getPropagationBehavior, getTimeout, hashCode, isReadOnly, setIsolationLevel, setIsolationLevelName, setName, setPropagationBehavior, setPropagationBehaviorName, setReadOnly, setTimeout, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

logger

protected final Log logger
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(org.springframework.transaction.PlatformTransactionManager)

TransactionTemplate

public TransactionTemplate(PlatformTransactionManager transactionManager)
Create a new TransactionTemplate instance.

Parameters:
transactionManager - transaction management strategy to be used
Method Detail

setTransactionManager

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


getTransactionManager

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


afterPropertiesSet

public void afterPropertiesSet()
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

Specified by:
afterPropertiesSet in interface InitializingBean

execute

public Object execute(TransactionCallback action)
               throws TransactionException
Execute the action specified by the given callback object within a transaction.

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

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


Copyright (c) 2002-2007 The Spring Framework Project.