org.springframework.transaction.interceptor
Class TransactionAspectSupport

java.lang.Object
  extended byorg.springframework.transaction.interceptor.TransactionAspectSupport
All Implemented Interfaces:
InitializingBean, java.io.Serializable
Direct Known Subclasses:
TransactionInterceptor

public class TransactionAspectSupport
extends java.lang.Object
implements InitializingBean, java.io.Serializable

Superclass for transactional aspects, such as the AOP Alliance-compatible 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.

Uses the Strategy design pattern. A PlatformTransactionManager implementation will perform the actual transaction management.

This class could set JTA as default transaction manager as that implementation does not need any specific configuration. JTA is not the default though to avoid unnecessary dependencies.

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

Author:
Rod Johnson, Juergen Hoeller
See Also:
Serialized Form

Nested Class Summary
protected  class TransactionAspectSupport.TransactionInfo
          Opaque object used to hold Transaction information.
 
Field Summary
protected  org.apache.commons.logging.Log logger
          Transient to avoid serialization.
protected  TransactionAttributeSource transactionAttributeSource
          Helper used to find transaction attributes
protected  PlatformTransactionManager transactionManager
          Delegate used to create, commit and rollback transactions
 
Constructor Summary
TransactionAspectSupport()
           
 
Method Summary
 void afterPropertiesSet()
          Check that required properties were set.
protected  TransactionAspectSupport.TransactionInfo createTransactionIfNecessary(java.lang.reflect.Method method, java.lang.Class targetClass)
          Create a transaction if necessary
protected static TransactionAspectSupport.TransactionInfo currentTransactionInfo()
          Subclasses can use this to return the current TransactionInfo.
static TransactionStatus currentTransactionStatus()
          Return the transaction transactionStatus of the current method invocation.
protected  void doCloseTransactionAfterThrowing(TransactionAspectSupport.TransactionInfo txInfo, java.lang.Throwable ex)
          Handle a throwable, closing out the transaction.
protected  void doCommitTransactionAfterReturning(TransactionAspectSupport.TransactionInfo txInfo)
          Execute after successful completion of call, but not after an exception was handled.
protected  void doFinally(TransactionAspectSupport.TransactionInfo txInfo)
          Call this in all cases: exception or normal return.
 TransactionAttributeSource getTransactionAttributeSource()
          Return the transaction attribute source.
 PlatformTransactionManager getTransactionManager()
          Return the transaction manager.
protected  java.lang.String methodIdentification(java.lang.reflect.Method method)
          Convenience method to return a String representation of this Method for use in logging.
 void setTransactionAttributes(java.util.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".
 void setTransactionAttributeSource(TransactionAttributeSource transactionAttributeSource)
          Set the transaction attribute source which is used to find transaction attributes.
 void setTransactionManager(PlatformTransactionManager transactionManager)
          Set the transaction manager.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected transient org.apache.commons.logging.Log logger
Transient to avoid serialization. Not static as we want it to be the correct logger for subclasses. Reconstituted in readObject().


transactionManager

protected PlatformTransactionManager transactionManager
Delegate used to create, commit and rollback transactions


transactionAttributeSource

protected TransactionAttributeSource transactionAttributeSource
Helper used to find transaction attributes

Constructor Detail

TransactionAspectSupport

public TransactionAspectSupport()
Method Detail

currentTransactionStatus

public static TransactionStatus currentTransactionStatus()
                                                  throws NoTransactionException
Return the transaction transactionStatus 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

currentTransactionInfo

protected static TransactionAspectSupport.TransactionInfo currentTransactionInfo()
                                                                          throws NoTransactionException
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.

Returns:
TransactionInfo bound to this thread
Throws:
NoTransactionException - if no transaction has been created by an aspect

setTransactionManager

public void setTransactionManager(PlatformTransactionManager transactionManager)
Set the transaction manager. This will perform actual transaction management: This class is just a way of invoking it.


getTransactionManager

public PlatformTransactionManager getTransactionManager()
Return the transaction manager.


setTransactionAttributes

public void setTransactionAttributes(java.util.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.

See Also:
setTransactionAttributeSource(org.springframework.transaction.interceptor.TransactionAttributeSource), TransactionAttributeEditor, NameMatchTransactionAttributeSource

setTransactionAttributeSource

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.

See Also:
TransactionAttributeSourceEditor, MethodMapTransactionAttributeSource, NameMatchTransactionAttributeSource

getTransactionAttributeSource

public TransactionAttributeSource getTransactionAttributeSource()
Return the transaction attribute source.


afterPropertiesSet

public void afterPropertiesSet()
Check that required properties were set.

Specified by:
afterPropertiesSet in interface InitializingBean

createTransactionIfNecessary

protected TransactionAspectSupport.TransactionInfo createTransactionIfNecessary(java.lang.reflect.Method method,
                                                                                java.lang.Class targetClass)
Create a transaction if necessary

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.

methodIdentification

protected java.lang.String methodIdentification(java.lang.reflect.Method method)
Convenience method to return a String representation of this Method for use in logging.

Parameters:
method - method we're interested in
Returns:
log message identifying this method

doCommitTransactionAfterReturning

protected void doCommitTransactionAfterReturning(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

doCloseTransactionAfterThrowing

protected void doCloseTransactionAfterThrowing(TransactionAspectSupport.TransactionInfo txInfo,
                                               java.lang.Throwable ex)
Handle a throwable, closing out the transaction. We may commit or roll back, depending on our configuration.

Parameters:
txInfo - information about the current transaction
ex - throwable encountered

doFinally

protected void doFinally(TransactionAspectSupport.TransactionInfo txInfo)
Call this in all cases: exception or normal return. Resets the TransactionInfo ThreadLocal

Parameters:
txInfo - information about the current transaction. May be null.


Copyright (C) 2003-2004 The Spring Framework Project.