类 TransactionTemplate
- java.lang.Object
- org.springframework.transaction.support.DefaultTransactionDefinition
- org.springframework.transaction.support.TransactionTemplate
public class TransactionTemplate extends DefaultTransactionDefinition implements TransactionOperations, InitializingBean
Template class that simplifies programmatic transaction demarcation and transaction exception handling.The central method is
execute(org.springframework.transaction.support.TransactionCallback<T>)
, supporting transactional code that implements theTransactionCallback
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.
- 从以下版本开始:
- 17.03.2003
- 作者:
- Juergen Hoeller
- 另请参阅:
execute(org.springframework.transaction.support.TransactionCallback<T>)
,setTransactionManager(org.springframework.transaction.PlatformTransactionManager)
,PlatformTransactionManager
, 序列化表格
字段概要
从类继承的字段 org.springframework.transaction.support.DefaultTransactionDefinition
PREFIX_ISOLATION, PREFIX_PROPAGATION, PREFIX_TIMEOUT, READ_ONLY_MARKER
从接口继承的字段 org.springframework.transaction.TransactionDefinition
ISOLATION_DEFAULT, ISOLATION_READ_COMMITTED, ISOLATION_READ_UNCOMMITTED, ISOLATION_REPEATABLE_READ, ISOLATION_SERIALIZABLE, PROPAGATION_MANDATORY, PROPAGATION_NESTED, PROPAGATION_NEVER, PROPAGATION_NOT_SUPPORTED, PROPAGATION_REQUIRED, PROPAGATION_REQUIRES_NEW, PROPAGATION_SUPPORTS, TIMEOUT_DEFAULT
构造器概要
构造器 构造器 说明 TransactionTemplate()
Construct a new TransactionTemplate for bean usage.TransactionTemplate(PlatformTransactionManager transactionManager)
Construct a new TransactionTemplate using the given transaction manager.TransactionTemplate(PlatformTransactionManager transactionManager, TransactionDefinition transactionDefinition)
Construct a new TransactionTemplate using the given transaction manager, taking its default settings from the given transaction definition.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 void
afterPropertiesSet()
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.boolean
equals(Object other)
This implementation compares thetoString()
results.<T> T
execute(TransactionCallback<T> 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.从类继承的方法 org.springframework.transaction.support.DefaultTransactionDefinition
getDefinitionDescription, getIsolationLevel, getName, getPropagationBehavior, getTimeout, hashCode, isReadOnly, setIsolationLevel, setIsolationLevelName, setName, setPropagationBehavior, setPropagationBehaviorName, setReadOnly, setTimeout, toString
从接口继承的方法 org.springframework.transaction.support.TransactionOperations
executeWithoutResult
构造器详细资料
TransactionTemplate
public TransactionTemplate()
Construct a new TransactionTemplate for bean usage.Note: The PlatformTransactionManager needs to be set before any
execute
calls.
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 usedtransactionDefinition
- the transaction definition to copy the default settings from. Local properties can still be set to change values.
方法详细资料
setTransactionManager
public void setTransactionManager(@Nullable PlatformTransactionManager transactionManager)
Set the transaction management strategy to be used.
getTransactionManager
@Nullable public PlatformTransactionManager getTransactionManager()
Return the transaction management strategy to be used.
afterPropertiesSet
public void afterPropertiesSet()
从接口复制的说明:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,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.
- 指定者:
afterPropertiesSet
在接口中InitializingBean
execute
@Nullable public <T> T execute(TransactionCallback<T> action) throws TransactionException
从接口复制的说明:TransactionOperations
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.
- 指定者:
execute
在接口中TransactionOperations
- 参数:
action
- the callback object that specifies the transactional action- 返回:
- a result object returned by the callback, or
null
if none - 抛出:
TransactionException
- in case of initialization, rollback, or system errors- 另请参阅:
TransactionOperations.executeWithoutResult(Consumer)
equals
public boolean equals(@Nullable Object other)
从类复制的说明:DefaultTransactionDefinition
This implementation compares thetoString()
results.