AOP:聲明式事務管理流程

1. 註冊BeanFactoryTransactionAttributeSourceAdvisor

@EnableTransactionManagement --> @Import(TransactionManagementConfigurationSelector.class) --> 註冊ProxyTransactionManagementConfiguration緩存

@Configuration
public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration {
    @Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor() {
 BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();         ...
 advisor.setAdvice(transactionInterceptor());         ...return advisor;
    }
    ...
    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public TransactionInterceptor transactionInterceptor() {
 TransactionInterceptor interceptor = new TransactionInterceptor();         ...return interceptor;
    }
}

2. TransactionInterceptor

public class TransactionInterceptor extends TransactionAspectSupport implements MethodInterceptor, Serializable {
    ...
    @Override
    public Object invoke(final MethodInvocation invocation) throws Throwable {
        Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);
        return invokeWithinTransaction(invocation.getMethod(), targetClass, new InvocationCallback() {
            @Override
            public Object proceedWithInvocation() throws Throwable {
                return invocation.proceed();             }
        });
    }
    ...
}
public abstract class TransactionAspectSupport implements BeanFactoryAware, InitializingBean {
    ...
    protected Object invokeWithinTransaction(Method method, Class<?> targetClass, final InvocationCallback invocation) throws Throwable {
        final TransactionAttribute txAttr = getTransactionAttributeSource().getTransactionAttribute(method, targetClass); // @Transactional final PlatformTransactionManager tm = determineTransactionManager(txAttr); // 獲取事務管理器         ...
        if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
 TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification); // 建立事務             Object retVal = null;
            try {
 retVal = invocation.proceedWithInvocation(); // 執行切面             }
            catch (Throwable ex) {
 completeTransactionAfterThrowing(txInfo, ex); // 異常處理 throw ex;
            }
            finally {
 cleanupTransactionInfo(txInfo); // 清理事務信息(事務嵌套)             }
 commitTransactionAfterReturning(txInfo); // 提交事務 return retVal;
        }
        else {
            ...
        }
    }

    protected PlatformTransactionManager determineTransactionManager(TransactionAttribute txAttr) {
        ...
        String qualifier = txAttr.getQualifier();
        if (StringUtils.hasText(qualifier)) { // @Qualifier
            ...
        }
        else if (StringUtils.hasText(this.transactionManagerBeanName)) {
            ...
        }
        else {
            PlatformTransactionManager defaultTransactionManager = getTransactionManager();
            if (defaultTransactionManager == null) {
                defaultTransactionManager = this.transactionManagerCache.get(DEFAULT_TRANSACTION_MANAGER_KEY); // 緩存look-up
                if (defaultTransactionManager == null) {
 defaultTransactionManager = this.beanFactory.getBean(PlatformTransactionManager.class); // 容器look-up
                    this.transactionManagerCache.putIfAbsent(DEFAULT_TRANSACTION_MANAGER_KEY, defaultTransactionManager); // 緩存
                }
            }
            return defaultTransactionManager;
        }
    }
    ...
}
相關文章
相關標籤/搜索