public abstract class

TransactionAspectSupport

extends Object
implements BeanFactoryAware InitializingBean
java.lang.Object
   ↳ org.springframework.transaction.interceptor.TransactionAspectSupport
Known Direct Subclasses

Class Overview

Base class for transactional aspects, such as the TransactionInterceptor or an AspectJ aspect.

This enables the underlying Spring transaction infrastructure to be used easily to implement an aspect for any aspect system.

Subclasses are responsible for calling methods in this class in the correct order.

If no transaction name has been specified in the TransactionAttribute, the exposed name will be the fully-qualified class name + "." + method name (by default).

Uses the Strategy design pattern. A PlatformTransactionManager implementation will perform the actual transaction management, and a TransactionAttributeSource is used for determining transaction definitions.

A transaction aspect is serializable if its PlatformTransactionManager and TransactionAttributeSource are serializable.

Summary

Nested Classes
class TransactionAspectSupport.TransactionInfo Opaque object used to hold Transaction information. 
Fields
protected final Log logger
Public Constructors
TransactionAspectSupport()
Public Methods
void afterPropertiesSet()
Check that required properties were set.
static TransactionStatus currentTransactionStatus()
Return the transaction status of the current method invocation.
TransactionAttributeSource getTransactionAttributeSource()
Return the transaction attribute source.
PlatformTransactionManager getTransactionManager()
Return the transaction manager, if specified.
void setBeanFactory(BeanFactory beanFactory)
Set the BeanFactory to use for retrieving PlatformTransactionManager beans.
void setTransactionAttributeSource(TransactionAttributeSource transactionAttributeSource)
Set the transaction attribute source which is used to find transaction attributes.
void setTransactionAttributeSources(TransactionAttributeSource[] transactionAttributeSources)
Set multiple transaction attribute sources which are used to find transaction attributes.
void setTransactionAttributes(Properties transactionAttributes)
Set properties with method names as keys and transaction attribute descriptors (parsed via TransactionAttributeEditor) as values: e.g.
void setTransactionManager(PlatformTransactionManager transactionManager)
Specify the target transaction manager.
void setTransactionManagerBeanName(String transactionManagerBeanName)
Specify the name of the default transaction manager bean.
Protected Methods
void cleanupTransactionInfo(TransactionAspectSupport.TransactionInfo txInfo)
Reset the TransactionInfo ThreadLocal.
void commitTransactionAfterReturning(TransactionAspectSupport.TransactionInfo txInfo)
Execute after successful completion of call, but not after an exception was handled.
void completeTransactionAfterThrowing(TransactionAspectSupport.TransactionInfo txInfo, Throwable ex)
Handle a throwable, completing the transaction.
TransactionAspectSupport.TransactionInfo createTransactionIfNecessary(PlatformTransactionManager tm, TransactionAttribute txAttr, String joinpointIdentification)
Create a transaction if necessary based on the given TransactionAttribute.
TransactionAspectSupport.TransactionInfo createTransactionIfNecessary(Method method, Class targetClass)
Create a transaction if necessary, based on the given method and class.
static TransactionAspectSupport.TransactionInfo currentTransactionInfo()
Subclasses can use this to return the current TransactionInfo.
PlatformTransactionManager determineTransactionManager(TransactionAttribute txAttr)
Determine the specific transaction manager to use for the given transaction.
final BeanFactory getBeanFactory()
Return the BeanFactory to use for retrieving PlatformTransactionManager beans.
final String getTransactionManagerBeanName()
Return the name of the default transaction manager bean.
String methodIdentification(Method method, Class targetClass)
Convenience method to return a String representation of this Method for use in logging.
String methodIdentification(Method method)
This method is deprecated. in favor of methodIdentification(Method, Class)
TransactionAspectSupport.TransactionInfo prepareTransactionInfo(PlatformTransactionManager tm, TransactionAttribute txAttr, String joinpointIdentification, TransactionStatus status)
Prepare a TransactionInfo for the given attribute and status object.
[Expand]
Inherited Methods
From class java.lang.Object
From interface org.springframework.beans.factory.BeanFactoryAware
From interface org.springframework.beans.factory.InitializingBean

Fields

protected final Log logger

Public Constructors

public TransactionAspectSupport ()

Public Methods

public void afterPropertiesSet ()

Check that required properties were set.

public static TransactionStatus currentTransactionStatus ()

Return the transaction status of the current method invocation. Mainly intended for code that wants to set the current transaction rollback-only but not throw an application exception.

Throws
NoTransactionException if the transaction info cannot be found, because the method was invoked outside an AOP invocation context

public TransactionAttributeSource getTransactionAttributeSource ()

Return the transaction attribute source.

public PlatformTransactionManager getTransactionManager ()

Return the transaction manager, if specified.

public void setBeanFactory (BeanFactory beanFactory)

Set the BeanFactory to use for retrieving PlatformTransactionManager beans.

Parameters
beanFactory owning BeanFactory (never null). The bean can immediately call methods on the factory.

public void setTransactionAttributeSource (TransactionAttributeSource transactionAttributeSource)

Set the transaction attribute source which is used to find transaction attributes. If specifying a String property value, a PropertyEditor will create a MethodMapTransactionAttributeSource from the value.

public void setTransactionAttributeSources (TransactionAttributeSource[] transactionAttributeSources)

Set multiple transaction attribute sources which are used to find transaction attributes. Will build a CompositeTransactionAttributeSource for the given sources.

public void setTransactionAttributes (Properties transactionAttributes)

Set properties with method names as keys and transaction attribute descriptors (parsed via TransactionAttributeEditor) as values: e.g. key = "myMethod", value = "PROPAGATION_REQUIRED,readOnly".

Note: Method names are always applied to the target class, no matter if defined in an interface or the class itself.

Internally, a NameMatchTransactionAttributeSource will be created from the given properties.

public void setTransactionManager (PlatformTransactionManager transactionManager)

Specify the target transaction manager.

public void setTransactionManagerBeanName (String transactionManagerBeanName)

Specify the name of the default transaction manager bean.

Protected Methods

protected void cleanupTransactionInfo (TransactionAspectSupport.TransactionInfo txInfo)

Reset the TransactionInfo ThreadLocal.

Call this in all cases: exception or normal return!

Parameters
txInfo information about the current transaction (may be null)

protected void commitTransactionAfterReturning (TransactionAspectSupport.TransactionInfo txInfo)

Execute after successful completion of call, but not after an exception was handled. Do nothing if we didn't create a transaction.

Parameters
txInfo information about the current transaction

protected void completeTransactionAfterThrowing (TransactionAspectSupport.TransactionInfo txInfo, Throwable ex)

Handle a throwable, completing the transaction. We may commit or roll back, depending on the configuration.

Parameters
txInfo information about the current transaction
ex throwable encountered

protected TransactionAspectSupport.TransactionInfo createTransactionIfNecessary (PlatformTransactionManager tm, TransactionAttribute txAttr, String joinpointIdentification)

Create a transaction if necessary based on the given TransactionAttribute.

Allows callers to perform custom TransactionAttribute lookups through the TransactionAttributeSource.

Parameters
txAttr the TransactionAttribute (may be null)
joinpointIdentification the fully qualified method name (used for monitoring and logging purposes)
Returns
  • a TransactionInfo object, whether or not a transaction was created. The hasTransaction() method on TransactionInfo can be used to tell if there was a transaction created.

protected TransactionAspectSupport.TransactionInfo createTransactionIfNecessary (Method method, Class targetClass)

Create a transaction if necessary, based on the given method and class.

Performs a default TransactionAttribute lookup for the given method.

Parameters
method method about to execute
targetClass class the method is on
Returns
  • a TransactionInfo object, whether or not a transaction was created. The hasTransaction() method on TransactionInfo can be used to tell if there was a transaction created.

protected static TransactionAspectSupport.TransactionInfo currentTransactionInfo ()

Subclasses can use this to return the current TransactionInfo. Only subclasses that cannot handle all operations in one method, such as an AspectJ aspect involving distinct before and after advice, need to use this mechanism to get at the current TransactionInfo. An around advice such as an AOP Alliance MethodInterceptor can hold a reference to the TransactionInfo throughout the aspect method.

A TransactionInfo will be returned even if no transaction was created. The TransactionInfo.hasTransaction() method can be used to query this.

To find out about specific transaction characteristics, consider using TransactionSynchronizationManager's isSynchronizationActive() and/or isActualTransactionActive() methods.

Returns
  • TransactionInfo bound to this thread, or null if none

protected PlatformTransactionManager determineTransactionManager (TransactionAttribute txAttr)

Determine the specific transaction manager to use for the given transaction.

protected final BeanFactory getBeanFactory ()

Return the BeanFactory to use for retrieving PlatformTransactionManager beans.

protected final String getTransactionManagerBeanName ()

Return the name of the default transaction manager bean.

protected String methodIdentification (Method method, Class targetClass)

Convenience method to return a String representation of this Method for use in logging. Can be overridden in subclasses to provide a different identifier for the given method.

Parameters
method the method we're interested in
targetClass class the method is on
Returns
  • log message identifying this method

protected String methodIdentification (Method method)

This method is deprecated.
in favor of methodIdentification(Method, Class)

Convenience method to return a String representation of this Method for use in logging. Can be overridden in subclasses to provide a different identifier for the given method.

Parameters
method the method we're interested in
Returns
  • log message identifying this method

protected TransactionAspectSupport.TransactionInfo prepareTransactionInfo (PlatformTransactionManager tm, TransactionAttribute txAttr, String joinpointIdentification, TransactionStatus status)

Prepare a TransactionInfo for the given attribute and status object.

Parameters
txAttr the TransactionAttribute (may be null)
joinpointIdentification the fully qualified method name (used for monitoring and logging purposes)
status the TransactionStatus for the current transaction
Returns
  • the prepared TransactionInfo object