接上节内容,Spring事务执行原理通过创立一个BeanFactoryTransactionAttributeSourceAdvisor,并把TransactionInterceptor注入进去,而TransactionInterceptor实现了Advice接口。而Spring Aop在Spring中会把Advisor中的Advice转换成拦截器链,而后调用。
执行流程
- 获取对应事务属性,也就是获取@Transactional注解上的属性
- 获取TransactionManager,罕用的如DataSourceTransactionManager事务管理
- 在指标办法执行前获取事务信息并创立事务
- 回调执行下一个调用链
- 一旦出现异常,尝试异样解决,回滚事务
- 提交事务前的事务信息清理
- 提交事务
具体分析
- 获取对应事务属性,具体代码执行流程如下:
final TransactionAttribute txAttr = getTransactionAttributeSource().getTransactionAttribute(method, targetClass);
protected TransactionAttribute computeTransactionAttribute(Method method, Class<?> targetClass) { // Don't allow no-public methods as required. //1. allowPublicMethodsOnly()返回true,只能是公共办法 if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) { return null; } // Ignore CGLIB subclasses - introspect the actual user class. Class<?> userClass = ClassUtils.getUserClass(targetClass); // The method may be on an interface, but we need attributes from the target class. // If the target class is null, the method will be unchanged. //method代表接口中的办法、specificMethod代表实现类的办法 Method specificMethod = ClassUtils.getMostSpecificMethod(method, userClass); // If we are dealing with method with generic parameters, find the original method. //解决泛型 specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod); // First try is the method in the target class. //查看办法中是否存在事务 TransactionAttribute txAttr = findTransactionAttribute(specificMethod); if (txAttr != null) { return txAttr; } // Second try is the transaction attribute on the target class. //查看办法所在类是否存在事务申明 txAttr = findTransactionAttribute(specificMethod.getDeclaringClass()); if (txAttr != null && ClassUtils.isUserLevelMethod(method)) { return txAttr; } //如果存在接口,则在接口中查找 if (specificMethod != method) { // Fallback is to look at the original method. //查找接口办法 txAttr = findTransactionAttribute(method); if (txAttr != null) { return txAttr; } // Last fallback is the class of the original method. //到接口类中寻找 txAttr = findTransactionAttribute(method.getDeclaringClass()); if (txAttr != null && ClassUtils.isUserLevelMethod(method)) { return txAttr; } } return null;}
getTransactionAttributeSource()取得的对象是在ProxyTransactionManagementConfiguration创立bean时注入的AnnotationTransactionAttributeSource对象。 AnnotationTransactionAttributeSource中getTransactionAttributeSource办法次要逻辑交给了computeTransactionAttribute办法,所以咱们间接看computeTransactionAttribute代码实现。
computeTransactionAttribute办法执行的逻辑是:
- 判断是不是只运行公共办法,在AnnotationTransactionAttributeSource构造方法中传入true。若办法不是公共办法,则返回null。
- 失去具体的办法,method办法可能是接口办法或者泛型办法。
- 查看办法上是否存在事务
- 查看办法所在类上是否存在事务
- 查看接口的办法是否存在事务,查看接口上是否存在事务。
所以如果一个办法上用了@Transactional,类上和接口上也用了,以办法上的为主,其次才是类,最初才到接口。
- 获取TransactionManager,具体代码执行流程如下:
protected PlatformTransactionManager determineTransactionManager(TransactionAttribute txAttr) { // Do not attempt to lookup tx manager if no tx attributes are set if (txAttr == null || this.beanFactory == null) { return getTransactionManager(); } String qualifier = txAttr.getQualifier(); if (StringUtils.hasText(qualifier)) { return determineQualifiedTransactionManager(qualifier); } else if (StringUtils.hasText(this.transactionManagerBeanName)) { return determineQualifiedTransactionManager(this.transactionManagerBeanName); } else { //罕用的会走到这里 PlatformTransactionManager defaultTransactionManager = getTransactionManager(); if (defaultTransactionManager == null) { defaultTransactionManager = this.transactionManagerCache.get(DEFAULT_TRANSACTION_MANAGER_KEY); if (defaultTransactionManager == null) { //从beanFactory获取PlatformTransactionManager类型的bean defaultTransactionManager = this.beanFactory.getBean(PlatformTransactionManager.class); this.transactionManagerCache.putIfAbsent( DEFAULT_TRANSACTION_MANAGER_KEY, defaultTransactionManager); } } return defaultTransactionManager; }}
@Beanpublic PlatformTransactionManager txManager() { return new DataSourceTransactionManager(dataSource());}