• Spring事务失效场景


    1 非public方法

    AbstractFallbackTransactionAttributeSource # 

    @Nullable
        protected TransactionAttribute computeTransactionAttribute(Method method, @Nullable Class<?> targetClass) {
            // Don't allow no-public methods as required.
            if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
                return null;
            }

    TransactionAspectSupport #

    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);//txAttr=null
            final PlatformTransactionManager tm = determineTransactionManager(txAttr);
            final String joinpointIdentification = methodIdentification(method, targetClass, txAttr);
    
            if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
                // Standard transaction demarcation with getTransaction and commit/rollback calls.
                TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);
                Object retVal = null;
                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;
            }

    2 查看@Transactional注解所在的类,是否添加了@Service 或 @Component注解;

    3 MethodA 调用MethodB,MethodA和MethodB都有@Transactional注解。如果MethodA调用MethodB此时MethodB只是普通的方法

    4 final方法,因为final方法无法进行动态代理

    5 多线程执行,因为数据库的链接是放在threadlocal里的,多线程执行会导致不在一个conection里,也就不会在一个事务里

  • 相关阅读:
    使用Java实现对MySql数据库的导入与导出
    【转】揭开J2EE集群的神秘面纱
    Memcached深度分析
    HSQL入门及使用指南
    系统架构基础篇(高性能基础建设说明与选型条件)
    架构之美 摘抄
    JMS规范及相关实现
    spring3中使用@value注解获取属性值
    Thread Dump 分析综述
    什么中间件及中间件服务器?
  • 原文地址:https://www.cnblogs.com/juniorMa/p/15228644.html
Copyright © 2020-2023  润新知