共计 5002 个字符,预计需要花费 13 分钟才能阅读完成。
@Transactional 注解元数据驱动的声明式事务
基本原理是:
将对应的方法通过注解元数据,标注在业务方法或者所在的对象上,然后在业务执行期间,通过 AOP 拦截器反射读取元数据信息,最终将根据读取的业务信息构建事务管理支持。
不同的方法之间的事务传播保证在同一个事务内,是通过统一的数据源来实现的,事务开始时将数据源绑定到 ThreadLocal 中,后续加入的事务从 ThreadLocal 获取数据源来保证数据源的统一。
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Transactional {@AliasFor("transactionManager")
String value() default "";
// 事务管理器名称
@AliasFor("value")
String transactionManager() default "";
// 事务传播模式
Propagation propagation() default Propagation.REQUIRED;
// 事务隔离级别
Isolation isolation() default Isolation.DEFAULT;
// 超时时间
int timeout() default TransactionDefinition.TIMEOUT_DEFAULT;
// 是否是只读事务
boolean readOnly() default false;
// 需要回滚的异常类
Class<? extends Throwable>[] rollbackFor() default {};
// 需要回滚的异常类名称
String[] rollbackForClassName() default {};
// 排除回滚的异常类
Class<? extends Throwable>[] noRollbackFor() default {};
// 排除回滚的异常类名称
String[] noRollbackForClassName() default {};}
这里通过 SpringBoot 代码来分析实现过程,源码中删除了部分代码,只保留了一些重要部分
// 事务自动配置类 注意类里面使用的 @EnableTransactionManagement 注解
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
public class TransactionAutoConfiguration {
@Configuration
@ConditionalOnBean(PlatformTransactionManager.class)
@ConditionalOnMissingBean(AbstractTransactionManagementConfiguration.class)
public static class EnableTransactionManagementConfiguration {
// 注意这里使用的 @EnableTransactionManagement 注解
@Configuration
@EnableTransactionManagement(proxyTargetClass = false)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class",
havingValue = "false", matchIfMissing = false)
public static class JdkDynamicAutoProxyConfiguration { }
// 注意这里使用的 @EnableTransactionManagement 注解
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class",
havingValue = "true", matchIfMissing = true)
public static class CglibAutoProxyConfiguration {}}
}
这里只分析 proxy 模式
@Configuration
public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration {@Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor() {BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
advisor.setTransactionAttributeSource(transactionAttributeSource());
advisor.setAdvice(transactionInterceptor());
if (this.enableTx != null) {advisor.setOrder(this.enableTx.<Integer>getNumber("order"));
}
return advisor;
}
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TransactionAttributeSource transactionAttributeSource() {return new AnnotationTransactionAttributeSource();
}
// 这里注入了 TransactionInterceptor 拦截器 bean~~~~
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TransactionInterceptor transactionInterceptor() {TransactionInterceptor interceptor = new TransactionInterceptor();
interceptor.setTransactionAttributeSource(transactionAttributeSource());
if (this.txManager != null) {interceptor.setTransactionManager(this.txManager);
}
return interceptor;
}
}
TransactionInterceptor 拦截器通过元数据获取事务定义信息 TransactionDefinition,根据 Definition 信息获取 PlatformTransactionManager(TM),tm 接口抽象了事务的实现流程,默认的 tm 是 DataSourceTransactionManager(通过 DataSourceTransactionManagerAutoConfiguration 初始化的),tm 中的 getTransaction 根据事务的传播方式,开启、加入、挂起事务
@Override
public final TransactionStatus getTransaction(@Nullable TransactionDefinition definition) throws TransactionException {Object transaction = doGetTransaction();
boolean debugEnabled = logger.isDebugEnabled();
if (definition == null) {
// 使用默认的 Definition
definition = new DefaultTransactionDefinition();}
if (isExistingTransaction(transaction)) {
// 已经存在事务,进入单独的方法处理
return handleExistingTransaction(definition, transaction, debugEnabled);
}
// 检查 timeout 参数
if (definition.getTimeout() < TransactionDefinition.TIMEOUT_DEFAULT) {throw new InvalidTimeoutException("Invalid transaction timeout", definition.getTimeout());
}
// 当前必须存在事务,否则抛出异常
if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY) {
throw new IllegalTransactionStateException("No existing transaction found for transaction marked with propagation'mandatory'");
}
else if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED ||
definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW ||
definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) {
// 获取当前的一些事务信息,用于当前事务执行完后恢复
SuspendedResourcesHolder suspendedResources = suspend(null);
try {boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
// 构造一个新事务的 TransactionStatus(包含嵌套事务 SavePoint 的支持)DefaultTransactionStatus status = newTransactionStatus(definition, transaction, true, newSynchronization, debugEnabled, suspendedResources);
// 开启新的事务
doBegin(transaction, definition);
prepareSynchronization(status, definition);
return status;
}
catch (RuntimeException | Error ex) {
// 异常,恢复挂起的事务信息
resume(null, suspendedResources);
throw ex;
}
}
else {
// 空的事务
boolean newSynchronization = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);
return prepareTransactionStatus(definition, null, true, newSynchronization, debugEnabled, null);
}
}
正文完