public abstract class TransactionAspectSupport extends java.lang.Object implements BeanFactoryAware, InitializingBean
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.
setTransactionManager(org.springframework.transaction.PlatformTransactionManager)
,
setTransactionAttributes(java.util.Properties)
,
setTransactionAttributeSource(org.springframework.transaction.interceptor.TransactionAttributeSource)
Modifier and Type | Class and Description |
---|---|
protected static interface |
TransactionAspectSupport.InvocationCallback
Simple callback interface for proceeding with the target invocation.
|
private static class |
TransactionAspectSupport.ThrowableHolder
Internal holder class for a Throwable, used as a return value
from a TransactionCallback (to be subsequently unwrapped again).
|
private static class |
TransactionAspectSupport.ThrowableHolderException
Internal holder class for a Throwable, used as a RuntimeException to be
thrown from a TransactionCallback (and subsequently unwrapped again).
|
protected class |
TransactionAspectSupport.TransactionInfo
Opaque object used to hold Transaction information.
|
Modifier and Type | Field and Description |
---|---|
private BeanFactory |
beanFactory |
protected Log |
logger |
private TransactionAttributeSource |
transactionAttributeSource |
private static java.lang.ThreadLocal<TransactionAspectSupport.TransactionInfo> |
transactionInfoHolder
Holder to support the
currentTransactionStatus() method,
and to support communication between different cooperating advices
(e.g. |
private PlatformTransactionManager |
transactionManager |
private java.lang.String |
transactionManagerBeanName |
Constructor and Description |
---|
TransactionAspectSupport() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Check that required properties were set.
|
protected void |
cleanupTransactionInfo(TransactionAspectSupport.TransactionInfo txInfo)
Reset the TransactionInfo ThreadLocal.
|
protected void |
commitTransactionAfterReturning(TransactionAspectSupport.TransactionInfo txInfo)
Execute after successful completion of call, but not after an exception was handled.
|
protected void |
completeTransactionAfterThrowing(TransactionAspectSupport.TransactionInfo txInfo,
java.lang.Throwable ex)
Handle a throwable, completing the transaction.
|
protected TransactionAspectSupport.TransactionInfo |
createTransactionIfNecessary(java.lang.reflect.Method method,
java.lang.Class targetClass)
Deprecated.
|
protected TransactionAspectSupport.TransactionInfo |
createTransactionIfNecessary(PlatformTransactionManager tm,
TransactionAttribute txAttr,
java.lang.String joinpointIdentification)
Create a transaction if necessary based on the given TransactionAttribute.
|
protected static TransactionAspectSupport.TransactionInfo |
currentTransactionInfo()
Subclasses can use this to return the current TransactionInfo.
|
static TransactionStatus |
currentTransactionStatus()
Return the transaction status of the current method invocation.
|
protected PlatformTransactionManager |
determineTransactionManager(TransactionAttribute txAttr)
Determine the specific transaction manager to use for the given transaction.
|
protected BeanFactory |
getBeanFactory()
Return the BeanFactory to use for retrieving PlatformTransactionManager beans.
|
TransactionAttributeSource |
getTransactionAttributeSource()
Return the transaction attribute source.
|
PlatformTransactionManager |
getTransactionManager()
Return the transaction manager, if specified.
|
protected java.lang.String |
getTransactionManagerBeanName()
Return the name of the default transaction manager bean.
|
protected java.lang.Object |
invokeWithinTransaction(java.lang.reflect.Method method,
java.lang.Class targetClass,
TransactionAspectSupport.InvocationCallback invocation)
General delegate for around-advice-based subclasses, delegating to several other template
methods on this class.
|
protected java.lang.String |
methodIdentification(java.lang.reflect.Method method)
Deprecated.
in favor of
methodIdentification(Method, Class) |
protected java.lang.String |
methodIdentification(java.lang.reflect.Method method,
java.lang.Class targetClass)
Convenience method to return a String representation of this Method
for use in logging.
|
protected TransactionAspectSupport.TransactionInfo |
prepareTransactionInfo(PlatformTransactionManager tm,
TransactionAttribute txAttr,
java.lang.String joinpointIdentification,
TransactionStatus status)
Prepare a TransactionInfo for the given attribute and status object.
|
void |
setBeanFactory(BeanFactory beanFactory)
Set the BeanFactory to use for retrieving PlatformTransactionManager beans.
|
void |
setTransactionAttributes(java.util.Properties transactionAttributes)
Set properties with method names as keys and transaction attribute
descriptors (parsed via TransactionAttributeEditor) as values:
e.g.
|
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 |
setTransactionManager(PlatformTransactionManager transactionManager)
Specify the target transaction manager.
|
void |
setTransactionManagerBeanName(java.lang.String transactionManagerBeanName)
Specify the name of the default transaction manager bean.
|
private static final java.lang.ThreadLocal<TransactionAspectSupport.TransactionInfo> transactionInfoHolder
currentTransactionStatus()
method,
and to support communication between different cooperating advices
(e.g. before and after advice) if the aspect involves more than a
single method (as will be the case for around advice).protected final Log logger
private java.lang.String transactionManagerBeanName
private PlatformTransactionManager transactionManager
private TransactionAttributeSource transactionAttributeSource
private BeanFactory beanFactory
protected static TransactionAspectSupport.TransactionInfo currentTransactionInfo() throws NoTransactionException
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.
null
if noneNoTransactionException
TransactionAspectSupport.TransactionInfo.hasTransaction()
,
TransactionSynchronizationManager.isSynchronizationActive()
,
TransactionSynchronizationManager.isActualTransactionActive()
public static TransactionStatus currentTransactionStatus() throws NoTransactionException
NoTransactionException
- if the transaction info cannot be found,
because the method was invoked outside an AOP invocation contextpublic void setTransactionManagerBeanName(java.lang.String transactionManagerBeanName)
protected final java.lang.String getTransactionManagerBeanName()
public void setTransactionManager(PlatformTransactionManager transactionManager)
public PlatformTransactionManager getTransactionManager()
public void setTransactionAttributes(java.util.Properties transactionAttributes)
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 setTransactionAttributeSources(TransactionAttributeSource[] transactionAttributeSources)
public void setTransactionAttributeSource(TransactionAttributeSource transactionAttributeSource)
public TransactionAttributeSource getTransactionAttributeSource()
public void setBeanFactory(BeanFactory beanFactory)
setBeanFactory
in interface BeanFactoryAware
beanFactory
- owning BeanFactory (never null
).
The bean can immediately call methods on the factory.BeanInitializationException
protected final BeanFactory getBeanFactory()
public void afterPropertiesSet()
afterPropertiesSet
in interface InitializingBean
protected java.lang.Object invokeWithinTransaction(java.lang.reflect.Method method, java.lang.Class targetClass, TransactionAspectSupport.InvocationCallback invocation) throws java.lang.Throwable
CallbackPreferringPlatformTransactionManager
as well as regular PlatformTransactionManager
implementations.method
- the Method being invokedtargetClass
- the target class that we're invoking the method oninvocation
- the callback to use for proceeding with the target invocationjava.lang.Throwable
- propagated from the target invocationprotected PlatformTransactionManager determineTransactionManager(TransactionAttribute txAttr)
protected java.lang.String methodIdentification(java.lang.reflect.Method method, java.lang.Class targetClass)
method
- the method we're interested intargetClass
- the class that the method is being invoked onClassUtils.getQualifiedMethodName(java.lang.reflect.Method)
@Deprecated protected java.lang.String methodIdentification(java.lang.reflect.Method method)
methodIdentification(Method, Class)
method
- the method we're interested in@Deprecated protected TransactionAspectSupport.TransactionInfo createTransactionIfNecessary(java.lang.reflect.Method method, java.lang.Class targetClass)
createTransactionIfNecessary(PlatformTransactionManager, TransactionAttribute, String)
Performs a default TransactionAttribute lookup for the given method.
method
- the method about to executetargetClass
- the class that the method is being invoked onhasTransaction()
method on TransactionInfo can be used to
tell if there was a transaction created.getTransactionAttributeSource()
protected TransactionAspectSupport.TransactionInfo createTransactionIfNecessary(PlatformTransactionManager tm, TransactionAttribute txAttr, java.lang.String joinpointIdentification)
Allows callers to perform custom TransactionAttribute lookups through the TransactionAttributeSource.
txAttr
- the TransactionAttribute (may be null
)joinpointIdentification
- the fully qualified method name
(used for monitoring and logging purposes)hasTransaction()
method on TransactionInfo can be used to
tell if there was a transaction created.getTransactionAttributeSource()
protected TransactionAspectSupport.TransactionInfo prepareTransactionInfo(PlatformTransactionManager tm, TransactionAttribute txAttr, java.lang.String joinpointIdentification, TransactionStatus status)
txAttr
- the TransactionAttribute (may be null
)joinpointIdentification
- the fully qualified method name
(used for monitoring and logging purposes)status
- the TransactionStatus for the current transactionprotected void commitTransactionAfterReturning(TransactionAspectSupport.TransactionInfo txInfo)
txInfo
- information about the current transactionprotected void completeTransactionAfterThrowing(TransactionAspectSupport.TransactionInfo txInfo, java.lang.Throwable ex)
txInfo
- information about the current transactionex
- throwable encounteredprotected void cleanupTransactionInfo(TransactionAspectSupport.TransactionInfo txInfo)
Call this in all cases: exception or normal return!
txInfo
- information about the current transaction (may be null
)