码迷,mamicode.com
首页 > 其他好文 > 详细

AOP:声明式事务管理流程

时间:2019-02-25 18:43:08      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:注册   hint   异常处理   attribute   信息   roc   util   bsp   tor   

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;
        }
    }
    ...
}

 

AOP:声明式事务管理流程

标签:注册   hint   异常处理   attribute   信息   roc   util   bsp   tor   

原文地址:https://www.cnblogs.com/bjorney/p/10432207.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!