一、简介
spring事务管理是基于spring Aop基础上实现的,也是通过增加一个advisor通知器封装pointCut和advice。在advice中事务进行事务。根据Spring Aop原理,因这个方法增加了@Transactional,所以事务的advisor会对这个类生效,在生成bean对象时,检测到有advisor就会生成代理对象。
spring事务几个重要的组件:
BeanFactoryTransactionAttributeSourceAdvisor:事务处理的advisor
TransactionInterceptor: 事务处理的advice
DataSourceTransactionManager: 事务处理管理器,实现PlatformTransactionManager统一接口,处理事务的创建,提交,回滚。
TransactionInfo:事务信息,包含了事务处理管理transactionManager,事务的配置信息,,事务状态信息。如下图
当创建事务后,transactionInfo信息会被保存在ThreadLocal中。
protected final class TransactionInfo {
private final PlatformTransactionManager transactionManager;
private final TransactionAttribute transactionAttribute;
private final String joinpointIdentification;
private TransactionStatus transactionStatus;
private TransactionInfo oldTransactionInfo;
public TransactionInfo(PlatformTransactionManager transactionManager,
TransactionAttribute transactionAttribute, String joinpointIdentification) {
this.transactionManager = transactionManager;
this.transactionAttribute = transactionAttribute;
this.joinpointIdentification = joinpointIdentification;
}
public PlatformTransactionManager getTransactionManager() {
return this.transactionManager;
}
public TransactionAttribute getTransactionAttribute() {
return this.transactionAttribute;
}
/**
* Return a String representation of this joinpoint (usually a Method call)
* for use in logging.
*/
public String getJoinpointIdentification() {
return this.joinpointIdentification;
}
public void newTransactionStatus(TransactionStatus status) {
this.transactionStatus = status;
}
public TransactionStatus getTransactionStatus() {
return this.transactionStatus;
}
/**
* Return whether a transaction was created by this aspect,
* or whether we just have a placeholder to keep ThreadLocal stack integrity.
*/
public boolean hasTransaction() {
return (this.transactionStatus != null);
}
}
二、初始加载配置:
在spring-tx.jar包中,有一个加载事务的advisor类ProxyTransactionManagementConfiguration ,如下图。
/**
* {@code @Configuration} class that registers the Spring infrastructure beans
* necessary to enable proxy-based annotation-driven transaction management.
*
* @author Chris Beams
* @since 3.1
* @see EnableTransactionManagement
* @see TransactionManagementConfigurationSelector
*/
@Configuration
public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration {
@Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor() {
//这个就是处理事务的advisor
BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
//事务的一些配置信息
advisor.setTransactionAttributeSource(transactionAttributeSource());
advisor.setAdvice(transactionInterceptor());
advisor.setOrder(this.enableTx.<Integer>getNumber("order"));
return advisor;
}
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TransactionAttributeSource transactionAttributeSource() {
return new AnnotationTransactionAttributeSource();
}
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TransactionInterceptor transactionInterceptor() {
//这个就是处理事务的advice
TransactionInterceptor interceptor = new TransactionInterceptor();
interceptor.setTransactionAttributeSource(transactionAttributeSource());
if (this.txManager != null) {
interceptor.setTransactionManager(this.txManager);
}
return interceptor;
}
}
如上,初始化了spring 事务管理的advisor,注意上面代码中的@Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME) 其中public static final String TRANSACTION_ADVISOR_BEAN_NAME = "org.springframework.transaction.config.internalTransactionAdvisor";这个advisor会被标记为系统beanpostProcessor,存到统一的BeanPostProcessor集合中,待生成bean对象遍历这些BeanPostProcessor并执行后置处理器时,在Spring Aop文章中有详细介绍这里流程。
三、生成代理对象。
四、业务调用触发代理对象的调用:如下使用的是CglibAopProxy类里面的DynamicAdvisedInterceptor类#intercept方法被执行。
这里通过cglib生成的代理对象,在生成时设置了DynamicAdvisedInterceptor为回调类,所以会被执行。
然后取出所有的advice业务链进行调用。这里是执行的TransactionInterceptor
未完续