Spring源代码阅读(2):事务

转载

https://blog.csdn.net/weixin_44366439/article/details/90381619

原理

编程式事务使用TransactionTemplate或者直接使用底层的PlatformTransactionManager。对于编程式事务管理,spring推荐使用TransactionTemplate。
声明式事务是建立在AOP之上的。其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者回滚事务。

声明式事务最大的优点就是不需要通过编程的方式管理事务,这样就不需要在业务逻辑代码中掺杂事务管理的代码,只需在配置文件中做相关的事务规则声明(或通过基于@Transactional注解的方式),便可以将事务规则应用到业务逻辑中。

一个简单的spring事务的代码如下

        transactionTemplate.execute(new TransactionCallback<Integer>() {
    
    
            @Override
            public Integer doInTransaction(TransactionStatus status) {
    
    
                return aDAO.aMethod(xxx, xxx);
            }
        });

事务的执行会被如下拦截

//TransactionInterceptor.java
	@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();
			}
		});
	}

最后,设计执行的代码如下

@Nullable
protected Object invokeWithinTransaction(Method method, @Nullable Class<?> targetClass,
		final InvocationCallback invocation) throws Throwable {
    
    

	// If the transaction attribute is null, the method is non-transactional.
	// 1. 获取事务的属性
	TransactionAttributeSource tas = getTransactionAttributeSource();
	final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null);
	// 2. 获取事务管理器
	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.
		// 3. 收集事务信息,状态,开启事务,在Threadlocal中设置相关变量
		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.
			// 4. 执行业务代码,即被@Transactional注解标注的方法
			retVal = invocation.proceedWithInvocation();
		}
		catch (Throwable ex) {
    
    
			// target invocation exception
			// 5. 如果允许出现异常,执行处理,如回滚操作
			completeTransactionAfterThrowing(txInfo, ex);
			throw ex;
		}
		finally {
    
    
		    // 6.清理事务环境,将3.中ThreadLocal中设置的变量清除
			cleanupTransactionInfo(txInfo);
		}
		// 7.提交事务
		commitTransactionAfterReturning(txInfo);
		return retVal;
	}
    // 如果是编程事务处理(省略)
	else {
    
    
		
		}
	}
}

一个事务管理器的生命一般如下

    <bean id="agdsconfigTransactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref bean="gdlDataSource"/>
        </property>
    </bean>

当然,如果你的事务已经跨库,请使用JTaTransactionManager.

存储在ThreadLocal中的信息如下
在这里插入图片描述
其中,transactionStatus表是了任务的状态。

猜你喜欢

转载自blog.csdn.net/define_us/article/details/111055071
今日推荐