目录
  1. 入口
  2. 执行流程
spring事务(2) spring事务入口及整体流程

入口

事务的xml解析部分,我们有说到对于代理方式实现的事务,我们最终是通过AOP来做的。

那么回想一下AOP的部分,AOP的调用最终都是通过MethodInterceptor的invoke方法。

在xml解析部分,AnnotationDrivenBeanDefinitionParser类的内部类AopAutoProxyConfigurer的configureAutoProxyCreator方法中,spring做了这么一项工作,设置事务拦截器为TransactionInterceptor。

那么此处便很明朗了,我们实现事务的AOP拦截器正是TransactionInterceptor。

打开TransactionInterceptor类,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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();
}
});
}

整个方法只有两步,第一步中为获取targetClass,这个就是我们需要代理的类,比如我此处就是代理的study.cayun.login.service.impl.UserServiceImpl这个类。

在第二步中我们可以看到这个方法又做了一个调用,不过里面有一个回调参数,可以发现回调方法就是spring AOP中的继续调用拦截器链的方法。具体可见spring AOP(4) advice,可以在@Before、@After等advice中发现均调用了此方法。

执行流程

进入invokeWithinTransaction方法,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 该方法在TransactionAspectSupport类中

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

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

else {
// It's a CallbackPreferringPlatformTransactionManager: pass a TransactionCallback in.
try {
Object result = ((CallbackPreferringPlatformTransactionManager) tm).execute(txAttr,
new TransactionCallback<Object>() {
@Override
public Object doInTransaction(TransactionStatus status) {
TransactionInfo txInfo = prepareTransactionInfo(tm, txAttr, joinpointIdentification, status);
try {
return invocation.proceedWithInvocation();
}
catch (Throwable ex) {
if (txAttr.rollbackOn(ex)) {
// A RuntimeException: will lead to a rollback.
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
}
else {
throw new ThrowableHolderException(ex);
}
}
else {
// A normal return value: will lead to a commit.
return new ThrowableHolder(ex);
}
}
finally {
cleanupTransactionInfo(txInfo);
}
}
});

// Check result: It might indicate a Throwable to rethrow.
if (result instanceof ThrowableHolder) {
throw ((ThrowableHolder) result).getThrowable();
}
else {
return result;
}
}
catch (ThrowableHolderException ex) {
throw ex.getCause();
}
}
}

我们此处只探讨事务管理器使用DataSourceTransactionManager的情况,所以我们也只关注上面的if部分,对于else部分则不研究了。

那么我们可以很清晰地梳理出整个流程:

  • 获取事务的属性,包括传播行为、隔离级别、回滚异常(rollbackFor)等
  • 获取事务管理器,此处我们使用的是DataSourceTransactionManager(可以在spring的xml配置中找到,比如我的配置在spring-mybatis.xml中)
  • 获取连接点标记。其实就是将包名+类名+方法名拼接了一下,我的此处为study.cayun.login.service.impl.UserServiceImpl.insertUser
  • 创建事务
  • 执行AOP链
  • 若发生异常,则执行completeTransactionAfterThrowing
  • 清理事务信息
  • 若正常执行,则提交事务

那么核心的几个部分就是

  • 创建事务
  • 异常回滚
  • 清理事务信息
  • 提交事务

感觉写在一起量有些大,那么重开章节来写好了。

文章作者: 谷河
文章链接: https://www.lyytaw.com/spring/spring_tx_entrance/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 谷河|BLOG