美文网首页
Spring声明式事务

Spring声明式事务

作者: 田文健 | 来源:发表于2017-08-18 17:16 被阅读0次

在org.springframework.transaction.interceptor包下面找到TransactionInterceptor类,这个类实现了Spring声明式事务的过程。找到它的invoke方法。

   @Override
    public Object invoke(final MethodInvocation invocation) throws Throwable {
        // Work out the target class: may be {@code null}.
        // The TransactionAttributeSource should be passed the target class
        // as well as the method, which may be from an interface.
        Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);

        // Adapt to TransactionAspectSupport's invokeWithinTransaction...
        return invokeWithinTransaction(invocation.getMethod(), targetClass, new InvocationCallback() {
            @Override
            public Object proceedWithInvocation() throws Throwable {
                return invocation.proceed();
            }
        });
    }

第一行代码是获取调用的目标类,第二行进行带事务的调用,进入第二行代码查看细节。进入invokeWithinTransaction方法:

// If the transaction attribute is null, the method is non-transactional.
        final TransactionAttribute txAttr = getTransactionAttributeSource().getTransactionAttribute(method, targetClass);
        final PlatformTransactionManager tm = determineTransactionManager(txAttr);
        final String joinpointIdentification = methodIdentification(method, targetClass);

这里获取了事务属性,事务管理器和切入点的标志符(一个字符串)。

接下来是:

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

当txAttr(事务属性)为空或者tm(事务管理器)为空时,是没有事务的,这一点在createTransactionIfNecessary中有更具体的实现。

相关文章

  • spring04

    Spring JdbcTemplate学习 Spring 声明式事务 xml配置实现 Spring 声明式事务 注...

  • Spring的事务机制解析一

    一Spring事务的种类 1.声明式事务 2.编程式事务 二Spring事务的具体描述 (一)声明式事务 1.声明...

  • 手写源码(一):自己实现Spring事务

    手写Spring事务 Spring事务分为声明式事务(注解或包扫描)和编程式(在代码里提交或回滚)事务,声明式事务...

  • spring事务(二) 声明式事务

    spring事务(二) 声明式事务 知识导读 声明式事务是对编程式事务的包装 声明式事务通过使用AOP来实现,注册...

  • Java Spring-声明式事务

    Spring-声明式事务

  • Spring 事务机制详解

    Spring事务机制主要包括声明式事务和编程式事务 Spring声明式事务时,有一个非常重要的概念就是事务属性。事...

  • spring事务管理 TransactionProxyFacto

    J2EE,当然离不开事务,事务又当然少不了Spring声明式事务。spring声明式事务,很多码农门,应该和笔者一...

  • Spring事务总结

    1. 编程式事务和声明式事务 spring支持编程式事务管理和声明式事务管理两种方式。 编程式事务Spring推荐...

  • Spring Boot开启声明式事务

    Spring Boot开启声明式事务 在以前早期的Spring使用xml方式的时候,配置声明式事务通常用xml方式...

  • spring 声明式事务管理

    本节阐述在事务相关的问题上,Spring框架的声明式事务管理的内部工作原理。 关于Spring框架的声明式事务支持...

网友评论

      本文标题:Spring声明式事务

      本文链接:https://www.haomeiwen.com/subject/adyarxtx.html