2、Spring事務執行流程

接上節內容,Spring事務執行原理經過建立一個BeanFactoryTransactionAttributeSourceAdvisor,並把TransactionInterceptor注入進去,而TransactionInterceptor實現了Advice接口。而Spring Aop在Spring中會把Advisor中的Advice轉換成攔截器鏈,而後調用。ui

執行流程this

  1. 獲取對應事務屬性,也就是獲取@Transactional註解上的屬性
  2. 獲取TransactionManager,經常使用的如DataSourceTransactionManager事務管理
  3. 在目標方法執行前獲取事務信息並建立事務
  4. 回調執行下一個調用鏈
  5. 一旦出現異常,嘗試異常處理,回滾事務
  6. 提交事務

具體分析code

  1. 獲取對應事務屬性,具體代碼執行流程以下:
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代碼實現。orm

computeTransactionAttribute方法執行的邏輯是:對象

  • 判斷是否是隻運行公共方法,在AnnotationTransactionAttributeSource構造方法中傳入true。若方法不是公共方法,則返回null。
  • 獲得具體的方法,method方法多是接口方法或者泛型方法。
  • 查看方法上是否存在事務
  • 查看方法所在類上是否存在事務
  • 查看接口的方法是否存在事務,查看接口上是否存在事務。

因此若是一個方法上用了@Transactional,類上和接口上也用了,以方法上的爲主,其次纔是類,最後纔到接口。接口

  1. 獲取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;
    }
}
@Bean
public PlatformTransactionManager txManager() {
    return new DataSourceTransactionManager(dataSource());
}
相關文章
相關標籤/搜索