Spring事务——TransacationInterceptor源码学习
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Spring事务——TransacationInterceptor源码学习,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4447字,纯文字阅读大概需要7分钟。
内容图文
![Spring事务——TransacationInterceptor源码学习](/upload/InfoBanner/zyjiaocheng/670/e7164ea251ac4a7ea6281ad34171f67e.jpg)
@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源码学习全部内容,希望文章能够帮你解决Spring事务——TransacationInterceptor源码学习所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。