Spring事务的实现主要依赖于AOP和JDBC的支持。Spring通过AOP技术对事务进行了封装,使得开发者可以通过注解或编程的方式来声明事务,并且可以灵活地控制事务的传播行为和隔离级别。
下面是Spring事务的实现流程:
1. 开启事务当一个方法被声明为事务方法时,Spring会在方法执行前开启一个事务。Spring通过TransactionInterceptor拦截器来实现事务的开启。
2. 执行业务逻辑在事务开启后,Spring会执行业务逻辑。如果业务逻辑执行成功,则事务会继续执行,否则事务会回滚。
3. 提交或回滚事务当业务逻辑执行完成后,Spring会根据业务逻辑的执行结果来决定是否提交或回滚事务。如果业务逻辑执行成功,则事务会提交,否则事务会回滚。
4. 关闭事务当事务提交或回滚完成后,Spring会关闭事务。
下面是Spring事务的源码分析:
1. TransactionInterceptor拦截器TransactionInterceptor是Spring事务的核心拦截器,它实现了MethodInterceptor接口,用于拦截被@Transactional注解标记的方法。
```
public class TransactionInterceptor extends TransactionAspectSupport implements MethodInterceptor {
@Override public Object invoke(MethodInvocation invocation) throws Throwable {
Class> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);
return invokeWithinTransaction(invocation.getMethod(), targetClass, invocation::proceed);
}
}
```
2.事务管理器事务管理器是Spring事务的核心组件,它负责管理事务的开启、提交和回滚。Spring提供了多种事务管理器的实现,包括JDBC事务管理器、JTA事务管理器等。
```
public interface PlatformTransactionManager {
TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException;
void commit(TransactionStatus status) throws TransactionException;
void rollback(TransactionStatus status) throws TransactionException;
}
```
3.事务注解解析器事务注解解析器用于解析@Transactional注解,并根据注解的属性值来配置事务的传播行为和隔离级别。
```
public class AnnotationTransactionAttributeSource extends AbstractFallbackTransactionAttributeSource {
@Override protected TransactionAttribute findTransactionAttribute(Method method) {
TransactionAttribute attr = determineTransactionAttribute(method);
if (attr != null) {
return attr;
}
Class> targetClass = method.getDeclaringClass();
if (targetClass != method.getDeclaringClass()) {
attr = determineTransactionAttribute(targetClass);
if (attr != null) {
return attr;
}
}
return null;
}
protected TransactionAttribute determineTransactionAttribute(AnnotatedElement element) {
return determineTransactionAttributeFromAnnotation(element.getAnnotation(Transactional.class));
}
protected TransactionAttribute determineTransactionAttributeFromAnnotation(Transactional ann) {
if (ann != null) {
DefaultTransactionAttribute attr = new DefaultTransactionAttribute();
attr.setPropagationBehavior(ann.propagation().value());
attr.setIsolationLevel(ann.isolation().value());
attr.setTimeout(ann.timeout());
attr.setReadOnly(ann.readOnly());
return attr;
}
return null;
}
}
```
4.事务切面事务切面是将事务注解解析器、事务管理器和事务拦截器组合在一起的核心组件,它负责将事务注解解析器解析出来的事务属性传递给事务拦截器,并将事务管理器传递给事务拦截器。
```
public class TransactionInterceptor extends TransactionAspectSupport implements MethodInterceptor {
private final TransactionAttributeSource transactionAttributeSource;
private final PlatformTransactionManager transactionManager;
public TransactionInterceptor(TransactionAttributeSource transactionAttributeSource, PlatformTransactionManager transactionManager) {
this.transactionAttributeSource = transactionAttributeSource;
this.transactionManager = transactionManager;
}
@Override public Object invoke(MethodInvocation invocation) throws Throwable {
Class> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);
return invokeWithinTransaction(invocation.getMethod(), targetClass, invocation::proceed);
}
@Override protected Object doInvokeWithinTransaction(MethodInvocation invocation, TransactionAttribute txAttr) throws Throwable {
TransactionInfo txInfo = createTransactionIfNecessary(this.transactionManager, txAttr, invocation.getMethod().toString());
try {
Object retVal = invocation.proceed();
if (txInfo != null) {
this.transactionManager.commit(txInfo.getTransactionStatus());
}
return retVal;
} catch (Throwable ex) {
if (txInfo != null) {
this.transactionManager.rollback(txInfo.getTransactionStatus());
}
throw ex;
} finally {
if (txInfo != null) {
cleanupTransactionInfo(txInfo);
}
}
}
}
```