首页 > 编程语言 > 详细

Spring事务——TransacationInterceptor源码学习

时间:2019-11-07 20:44:31      阅读:85      评论:0      收藏:0      [点我收藏+]

 

@Nullable
protected Object invokeWithinTransaction(Method method, @Nullable Class<?> targetClass,
final InvocationCallback invocation) throws Throwable {

// If the transaction attribute is null, the method is non-transactional.
TransactionAttributeSource tas = getTransactionAttributeSource();
final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null);  
// 1
final PlatformTransactionManager tm = determineTransactionManager(txAttr);  // 2
final String joinpointIdentification = methodIdentification(method, targetClass, txAttr);  // 3

if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
// Standard transaction demarcation with getTransaction and commit/rollback calls.
TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);  // 4

Object retVal;
try {
// This is an around advice: Invoke the next interceptor in the chain.
// This will normally result in a target object being invoked.
retVal = invocation.proceedWithInvocation();
}
catch (Throwable ex) {
// target invocation exception
completeTransactionAfterThrowing(txInfo, ex);
throw ex;
}
finally {
cleanupTransactionInfo(txInfo);
}
commitTransactionAfterReturning(txInfo);
return retVal;
}

else {
final ThrowableHolder throwableHolder = new ThrowableHolder();

// It‘s a CallbackPreferringPlatformTransactionManager: pass a TransactionCallback in.
try {
Object result = ((CallbackPreferringPlatformTransactionManager) tm).execute(txAttr, status -> {
TransactionInfo txInfo = prepareTransactionInfo(tm, txAttr, joinpointIdentification, status);
try {
return invocation.proceedWithInvocation();
}
catch (Throwable ex) {
if (txAttr.rollbackOn(ex)) {
// A RuntimeException: will lead to a rollback.
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
}
else {
throw new ThrowableHolderException(ex);
}
}
else {
// A normal return value: will lead to a commit.
throwableHolder.throwable = ex;
return null;
}
}
finally {
cleanupTransactionInfo(txInfo);
}
});

// Check result state: It might indicate a Throwable to rethrow.
if (throwableHolder.throwable != null) {
throw throwableHolder.throwable;
}
return result;
}
catch (ThrowableHolderException ex) {
throw ex.getCause();
}
catch (TransactionSystemException ex2) {
if (throwableHolder.throwable != null) {
logger.error("Application exception overridden by commit exception", throwableHolder.throwable);
ex2.initApplicationException(throwableHolder.throwable);
}
throw ex2;
}
catch (Throwable ex2) {
if (throwableHolder.throwable != null) {
logger.error("Application exception overridden by commit exception", throwableHolder.throwable);
}
throw ex2;
}
}
}


TransactionAttributeSource tas = getTransactionAttributeSource();
final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null);
final PlatformTransactionManager tm = determineTransactionManager(txAttr);
final String joinpointIdentification = methodIdentification(method, targetClass, txAttr);
1.TransactionAttribute txAttr由TransactionAttributeSource tas获得,TransationDefinition是TransactionAttribute的基类txAttr记录了事务的执行方法、事务的传播行为、事务的隔离等级、事务是否只读、事务TimeOut属性和事务名。

    技术分享图片

2.final PlatformTransactionManager。DatasourceTransactionManager实现了PlatformTransactionManager接口,获取的是SpringBean容器中的PlatformTransactionManager(DataSourceTransactionManager)。
  技术分享图片

 

前面的步骤返回的全部是null。

3.final String joinpointIdentification,获取@Transactional注解的方法的全限定名。

3.5

4.TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification); txInfo持有TransactionManager、TransactionAttribute、joinpointIdentification、transactionStatus。

 



               

 

 

 

 

 



 

Spring事务——TransacationInterceptor源码学习

原文:https://www.cnblogs.com/ybonfire/p/11815181.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!