zoukankan      html  css  js  c++  java
  • 分布式事务02Springtx核心

    核心类及方法

    Spring-tx-4.3.14.RELEASE

    核心: 拦截器, 和其父类 org.springframework.aop.framework.ReflectiveMethodInvocation 相关(aop的包里) 以及 org.springframework.transaction.interceptor.TransactionAspectSupport 相关

    org.springframework.transaction.interceptor.TransactionInterceptor 类中的 invoke(final MethodInvocation invocation) 方法中的 invokeWithinTransaction() 方法

    开启事务, 执行sql, 提交或回滚的核心逻辑: invokeWithinTransaction(Method method, Class<?> targetClass, final InvocationCallback invocation)

    if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
    	// Standard transaction demarcation with getTransaction and commit/rollback calls.
        // 创建事务, 如果加了 @Transaction 注解, 就会创建
    	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.
            // 执行sql, 就是自己写的语句
            retVal = invocation.proceedWithInvocation();
    	}
    	catch (Throwable ex) {
    	    // target invocation exception
            // catch异常执行回滚
    	    completeTransactionAfterThrowing(txInfo, ex);
    	    throw ex;
    	}
    	finally {
    	    cleanupTransactionInfo(txInfo);
    	}
        // 提交事务, 落库
    	commitTransactionAfterReturning(txInfo);
    	return retVal;
    }
    

    事务核心调用链

    结论: 通过一系列的调用, 先找到合适的 PlatformTransactionManager(事务管理器), 有jpa的包默认用jpa的事务管理器, 选用对应的连接池, 再执行对应的抽象父类的begin方法, 最终使用jdbc包下的api开启/提交/回滚事务

  • 相关阅读:
    C++笔试题库之编程、问答题 150~200道
    C++语言中的static关键字的作用是什么?
    C++开发工程师面试题库 1~50道
    C++笔试题库之编程、问答题 100~150道
    C++经典面试题库 附带参考答案
    常用的16个c/c++面试题
    C++经典面试题全集 50~100道 都附带有参考答案
    c++常见面试题30道
    别傻了,人家离职你也离
    西苑附近的一亩园社区
  • 原文地址:https://www.cnblogs.com/richardhaha/p/15780488.html
Copyright © 2011-2022 走看看