Class TransactionInterceptor
- All Implemented Interfaces:
MethodInterceptor
This interceptor processes @Transactional annotations and manages transaction boundaries accordingly.
- Since:
- 6.2.0
- Author:
- wisdomme
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Loggerprivate final AnnotationLookupCache<Transactional> Instance-scoped on purpose - see AnnotationLookupCache's class javadoc.private final TransactionManager -
Constructor Summary
ConstructorsConstructorDescriptionTransactionInterceptor(TransactionManager transactionManager) Creates a new transaction interceptor. -
Method Summary
Modifier and TypeMethodDescriptionprivate ObjectexecuteInNewTransaction(MethodInvocation invocation, Transactional tx) Executes the invocation in a new transaction.private ObjectexecuteWithTransaction(MethodInvocation invocation, Transactional tx) Executes the method invocation within a transaction context.private intinheritanceDepth(Class<?> thrownClass, Class<?> ruleClass) WalksthrownClass's superclass chain to find the depth at which it matchesruleClass.invoke(MethodInvocation invocation) Intercept the given method invocation.private IntegershallowestMatchDepth(Throwable thrown, Class<? extends Throwable>[] rules) Returns the shallowest inheritance depth at whichthrown's runtime class matches any rule inrules, ornullif none match.private booleanshouldRollback(Throwable e, Transactional tx) Determines if the transaction should be rolled back for the given exception.
-
Field Details
-
lookupCache
Instance-scoped on purpose - see AnnotationLookupCache's class javadoc. -
LOGGER
-
transactionManager
-
-
Constructor Details
-
TransactionInterceptor
Creates a new transaction interceptor.- Parameters:
transactionManager- the transaction manager to use
-
-
Method Details
-
invoke
Description copied from interface:MethodInterceptorIntercept the given method invocation.Implementations should call
MethodInvocation.proceed()to continue the interceptor chain or invoke the target method.- Specified by:
invokein interfaceMethodInterceptor- Parameters:
invocation- the method invocation context- Returns:
- the result of the method invocation
- Throws:
Throwable- if the interceptor or target method throws an exception
-
executeWithTransaction
private Object executeWithTransaction(MethodInvocation invocation, Transactional tx) throws Throwable Executes the method invocation within a transaction context.- Throws:
Throwable
-
executeInNewTransaction
private Object executeInNewTransaction(MethodInvocation invocation, Transactional tx) throws Throwable Executes the invocation in a new transaction.- Throws:
Throwable
-
shouldRollback
Determines if the transaction should be rolled back for the given exception.Additive, depth-ordered combination of
rollbackForandnoRollbackFor(D-06/D-07 - confirmed by the maintainer during 02-06's checkpoint, matching Spring'sRuleBasedTransactionAttribute): each array is checked for its shallowest matching inheritance depth viashallowestMatchDepth(Throwable, Class[]). When both arrays match, the shallower depth wins; an exact-depth tie - including the same exception class listed in both arrays - favours rollback. When only one array matches, that one decides. When neither matches (including when both arrays are empty), this falls through to the unchangedRuntimeException/Errordefault - a non-emptyrollbackForwith no match no longer short-circuits to "commit" the way it did before this fix. -
shallowestMatchDepth
Returns the shallowest inheritance depth at whichthrown's runtime class matches any rule inrules, ornullif none match. Depth 0 is an exact class match; each step upthrown's superclass chain toward a matching rule adds 1. -
inheritanceDepth
WalksthrownClass's superclass chain to find the depth at which it matchesruleClass. Callers only reach this after confirmingruleClass.isInstance(...)of an instance ofthrownClassalready holds, so the walk is guaranteed to terminate atruleClassrather than atnull.
-