(十)Spring事务处理 - IoC容器的事务处理源码分析

来源:互联网 发布:mac os x sierrad安装 编辑:程序博客网 时间:2024/06/05 16:40
Spring事务处理主要分以下三个主要的过程:

(1)读取和处理在Spring IoC容器中配置的事务处理属性,并转化为Spring事务处理所需要的内部数据结构。

(2)Spring事务处理模块实现的统一的事务处理过程。这个通用的事务处理过程包括:处理事务配置属性;事务配置属性与线程绑定等。

(3)底层事务处理实现。Spring中底层事务处理的实现交由PlatformTransactionManager的具体实现类来实现,如DataSourceTransactionManager和HibernateTransactionManager等。

一、Spring管理事务处理的IoC容器—TransactionProxyFactoryBean

TransactionProxyFactoryBean是Spring中管理事务的IoC容器,它通过Spring的AOP功能来完成事务管理配置。TransactionProxyFactoryBean为Spring的事务处理的实现做准备工作,包括配置AOP的拦截器、通知器;同时向TransactionProxyFactoryBean注入事务处理器和事务处理属性等。TransactionProxyFactoryBean源码如下:

public class TransactionProxyFactoryBean extends AbstractSingletonProxyFactoryBeanimplements BeanFactoryAware {//通过AOP发挥作用的事务拦截器private final TransactionInterceptor transactionInterceptor = new TransactionInterceptor();//事务的AOP切入点private Pointcut pointcut;//通过Spring IoC容器依赖注入的PlatformTransactionManager事务管理器public void setTransactionManager(PlatformTransactionManager transactionManager) {this.transactionInterceptor.setTransactionManager(transactionManager);}//通过依赖注入设置事务属性,以Properties形式存放的事务属性的key是方法名,//value是事务属性描述public void setTransactionAttributes(Properties transactionAttributes) {this.transactionInterceptor.setTransactionAttributes(transactionAttributes);}//通过依赖注入设置事务属性源,通过事务属性源可以找到需要使用的事务属性public void setTransactionAttributeSource(TransactionAttributeSource transactionAttributeSource) {this.transactionInterceptor.setTransactionAttributeSource(transactionAttributeSource);}//通过依赖注入设置事务切入点,事务切入点根据触发条件调用事务拦截器public void setPointcut(Pointcut pointcut) {this.pointcut = pointcut;}//为事务拦截器设置管理事务的容器public void setBeanFactory(BeanFactory beanFactory) {this.transactionInterceptor.setBeanFactory(beanFactory);}//创建Spring AOP事务处理的通知器Advisorprotected Object createMainInterceptor() {//调用事务拦截器的方法,检查必需的属性是否设置this.transactionInterceptor.afterPropertiesSet();//如果在Spring配置中设置了事务切入点if (this.pointcut != null) {//使用Spring默认的通知器封装事务切入点和事务拦截器return new DefaultPointcutAdvisor(this.pointcut, this.transactionInterceptor);}//如果在Spring配置中没有设置事务切入点else {//使用TransactionAttributeSourceAdvisor封装默认的事务切入点return new TransactionAttributeSourceAdvisor(this.transactionInterceptor);}}} 

Spring的事务处理IoC容器TransactionProxyFactoryBean中主要通过依赖注入完成事务切入点和事务拦截器的配置,最重要的功能是通过createMainInterceptor()方法为事务切入点和事务拦截器创建AOP事务通知器。

TransactionProxyFactoryBean继承了AbstractSingletonProxyFactoryBean类,作为Spring事务管理IoC容器,其getObject()方法和IoC容器初始化完成之后回调的afterPropertiesSet()方法均是在其父类AbstractSingletonProxyFactoryBean中实现的,我们接下来分析AbstractSingletonProxyFactoryBean中事务处理相关源码的实现。

二、AbstractSingletonProxyFactoryBean创建配置事务

AbstractSingletonProxyFactoryBean实现了FactoryBean和InitializingBean接口,Spring IoC容器最核心的getObject()和afterPropertiesSet()方法均是在该类中实现,AbstractSingletonProxyFactoryBean创建和配置事务相关的方法源码如下:

//获取Spring事务代理工厂(ProxyFactory)public Object getObject() {if (this.proxy == null) {throw new FactoryBeanNotInitializedException();}return this.proxy;}//InitializingBean接口的实现方法,IoC容器初始化完成之后的回调方法public void afterPropertiesSet() {//事务的目标对象不能为空if (this.target == null) {throw new IllegalArgumentException("Property 'target' is required");}//事务目标对象必须是Bean引用,不能是Bean名称if (this.target instanceof String) {throw new IllegalArgumentException("'target' needs to be a bean reference, not a bean name as value");}//如果代理类加载器为null,则使用默认的类加载器作用代理类加载器if (this.proxyClassLoader == null) {this.proxyClassLoader = ClassUtils.getDefaultClassLoader();}//创建代理工厂,Spring事务管理容器TransactionProxyFactoryBean通过//ProxyFactory完成AOP的基本功能,ProxyFactory提供事务代理对象,并将事务拦//截器设置为事务目标对象方法的拦截器ProxyFactory proxyFactory = new ProxyFactory();//如果在事务拦截器之前配置了额外的拦截器if (this.preInterceptors != null) {//将这些事务之前的额外拦截器添加到通知器中for (Object interceptor : this.preInterceptors) {proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(interceptor));}}//加入Spring AOP事务处理通知器,createMainInterceptor()方法//由子类TransactionProxyFactoryBean提供实现proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(createMainInterceptor()));//如果在事务拦截器之后配置了额外拦截器if (this.postInterceptors != null) {//将这些事务之后的额外拦截器添加到通知器中for (Object interceptor : this.postInterceptors) {proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(interceptor));}}//从当前容器中复制事务AOP相关配置到ProxyFactory中proxyFactory.copyFrom(this);//创建AOP的目标源TargetSource targetSource = createTargetSource(this.target);//为ProxyFactory设置AOP目标源proxyFactory.setTargetSource(targetSource);//如果事务配置使用了代理接口if (this.proxyInterfaces != null) {//为ProxyFactory设置代理接口proxyFactory.setInterfaces(this.proxyInterfaces);}//如果事务代理不是直接应用于目标类或者接口else if (!isProxyTargetClass()) {//将目标源的所有接口都设置为ProxyFactory的接口proxyFactory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetSource.getTargetClass(), this.proxyClassLoader));}//ProxyFactory对象根据给定的类加载器创建事务代理对象//具体的创建过程我们在Spring的AOP源码分析中已经分析过,Spring根据是否//实现接口而分别调用JDK动态代理或者CGLIB方式创建AOP代理对象this.proxy = proxyFactory.getProxy(this.proxyClassLoader);}

当Spring事务IoC容器初始化完成之后,事务容器回调afterPropertiesSet()方法创建事务AOP代理对象。

三、TransactionAttributeSourceAdvisor读取Spring事务处理配置

在创建Spring事务的核心方法createMainInterceptor()中,根据给定的事务拦截器创建事务属性源通知器TransactionAttributeSourceAdvisor,事务属性源通知器的核心源码如下:

//事务拦截器private TransactionInterceptor transactionInterceptor;//事务属性源切入点,一个实现事务属性源切入点接口的匿名内部类private final TransactionAttributeSourcePointcut pointcut = new TransactionAttributeSourcePointcut() {//实现事务属性源切入点接口的获取事务属性源方法protected TransactionAttributeSource getTransactionAttributeSource() {//通过事务拦截器获取事务的配置属性return (transactionInterceptor != null ? transactionInterceptor.getTransactionAttributeSource() : null);}};//事务属性源通知器构造方法public TransactionAttributeSourceAdvisor(TransactionInterceptor interceptor) {setTransactionInterceptor(interceptor);}//设置事务拦截器public void setTransactionInterceptor(TransactionInterceptor interceptor) {this.transactionInterceptor = interceptor;}

事务属性源通知器的核心功能是为Spring的事务IoC容器设置事务属性源切入点和事务拦截器。

四、事务切入点TransactionAttributeSourcePointcut匹配事务配置规则

事务切入点TransactionAttributeSourcePointcut在事务属性源通知器中作为匿名内部类,用于根据事务的配置匹配符合规则的方法,事务切入点的核心源码如下:

//匹配事务目标类的方法public boolean matches(Method method, Class targetClass) {//获取事务属性源,getTransactionAttributeSource是一个抽象方法,在事务属性源通知器中实现TransactionAttributeSource tas = getTransactionAttributeSource();//返回指定类指定方法是否匹配事务属性return (tas == null || tas.getTransactionAttribute(method, targetClass) != null);}

事务属性源TransactionAttributeSource在事务拦截器TransactionInterceptor的父类事务切面支持TransactionAspectSupport中配置,源码如下:

//配置事务属性源public void setTransactionAttributes(Properties transactionAttributes) {//使用名称匹配的事务属性源NameMatchTransactionAttributeSource tas = new NameMatchTransactionAttributeSource();tas.setProperties(transactionAttributes);this.transactionAttributeSource = tas;} 

Spring默认使用名称匹配事务属性源NameMatchTransactionAttributeSource读取和匹配事务属性。

五、NameMatchTransactionAttributeSource读取和匹配事务属性

名称匹配属性属性源从事务处理属性配置中读取事务的方法名和配置属性值,将得到的事务方法名和属性值保存到一个Map集合中,方法名作为key,事务属性值作为value。名称匹配事务属性源主要源码如下:

public class NameMatchTransactionAttributeSource implements TransactionAttributeSource, Serializable {……//设置方法事务属性public void setProperties(Properties transactionAttributes) {//创建事务属性解析器TransactionAttributeEditor tae = new TransactionAttributeEditor();//获取事务属性配置中所有属性名称Enumeration propNames = transactionAttributes.propertyNames();//遍历所有的事务属性while (propNames.hasMoreElements()) {//获取事务属性配置的方法名String methodName = (String) propNames.nextElement();//获取方法配置的事务属性值String value = transactionAttributes.getProperty(methodName);//解析和格式化事务属性值tae.setAsText(value);//获取解析和格式化之后的事务属性TransactionAttribute attr = (TransactionAttribute) tae.getValue();//将方法名和其对应的事务属性添加到集合中addTransactionalMethod(methodName, attr);}}//将方法名称和该方法配置的事务属性添加到Map集合中public void addTransactionalMethod(String methodName, TransactionAttribute attr) {if (logger.isDebugEnabled()) {logger.debug("Adding transactional method [" + methodName + "] with attribute [" + attr + "]");}this.nameMap.put(methodName, attr);}//获取给定类给定方法中配置的事务属性public TransactionAttribute getTransactionAttribute(Method method, Class<?> targetClass) {//获取方法名String methodName = method.getName();//从方法名—>事务属性Map集合中获取给定方法名的事务属性TransactionAttribute attr = this.nameMap.get(methodName);//如果在方法名—>事务属性Map集合中没有给定方法名的事务属性if (attr == null) {String bestNameMatch = null;//判断给定方法名称是否在方法名—>事务属性Map集合的key中for (String mappedName : this.nameMap.keySet()) {//如果给定的方法名在Map集合的key中匹配if (isMatch(methodName, mappedName) &&(bestNameMatch == null || bestNameMatch.length() <= mappedName.length())) {//获取匹配方法的事务属性attr = this.nameMap.get(mappedName);bestNameMatch = mappedName;}}}return attr;}//判断给定的方法名是否匹配,默认实现了"xxx*","*xxx"和"*xxx*"匹配检查protected boolean isMatch(String methodName, String mappedName) {return PatternMatchUtils.simpleMatch(mappedName, methodName);}……}

名称匹配事务属性源NameMatchTransactionAttributeSource主要在两个阶段发挥作用,在Spring事务IoC容器对事务配置读取解析时,事务容器调用名称匹配事务属性源的setProperties和addTransactionalMethod方法将方法名称和事务属性添加到Map集合中,当方法调用时,通过getTransactionAttribute方法获取方法所配置的事务属性。

六、事务拦截器TransactionInterceptor的实现

管理Spring事务的IoC容器TransactionProxyFactoryBean已经完成了事务配置的读取,设置好了事务拦截器和切入点。当应用调用被配置事务的方法时,首先通过getObject方法向Spring事务管理容器索取被被管理方法的事务属性,触发调用事务拦截器的拦截方法进行事务处理。

在对Spring AOP源码分析中关于AOP代理如何起作用时,我们知道Spring的AOP代理通过invoke回调方法对切入点方法进行拦截处理,这个invoke方法是AOP联盟的方法拦截器MethodInterceptor接口中定义的方法,用于对AOP代理对象的方法进行包装处理。事务拦截器TransactionInterceptor正是通过这个invoke拦截方法实现事务的拦截处理,源码如下:

//事务拦截器的拦截方法public Object invoke(final MethodInvocation invocation) throws Throwable {//通过AOP获取事务的目标类Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);//通过事务属性源TransactionAttributeSource读取事务的属性配置,即调用上面名称匹配//事务属性源NameMatchTransactionAttributeSource的方法final TransactionAttribute txAttr =getTransactionAttributeSource().getTransactionAttribute(invocation.getMethod(), targetClass);//获取Spring事务管理IoC容器配置的事务处理器final PlatformTransactionManager tm = determineTransactionManager(txAttr);//获取目标类指定方法的事务连接点final String joinpointIdentification = methodIdentification(invocation.getMethod(), targetClass);//区分不同类型的PlatformTransactionManager事务处理器,不同类型的事务处理器调用//方式不同。对CallbackPreferringPlatformTransactionManager,需要回调函数来//实现事务的创建和提交,对非CallbackPreferringPlatformTransactionManager来//说,则不需要使用回调函数来实现事务处理。//非CallbackPreferringPlatformTransactionManager类型的事务处理器if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {//创建事务,将当前事务状态和信息保存到TransactionInfo对象中TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);Object retVal = null;try {//沿着拦截器链调用处理,使得最后目标对象的方法得到调用retVal = invocation.proceed();}catch (Throwable ex) {//在调用拦截器拦过程中出现异常,则根据事务配置进行提交或回滚处理completeTransactionAfterThrowing(txInfo, ex);throw ex;}//清除与当前线程绑定的事务信息finally {cleanupTransactionInfo(txInfo);}//通过事务处理器来对事务进行提交commitTransactionAfterReturning(txInfo);return retVal;}//CallbackPreferringPlatformTransactionManager类型的事务处理器else {//通过回调函数对事务进行处理try {//执行实现TransactionCallback接口的doInTransaction回调方法Object result = ((CallbackPreferringPlatformTransactionManager) tm).execute(txAttr,new TransactionCallback<Object>() {//实现TransactionCallback接口匿名内部类的回调方法public Object doInTransaction(TransactionStatus status) {//创建和准备事务TransactionInfo txInfo = prepareTransactionInfo(tm, txAttr, joinpointIdentification, status);try {//沿着拦截器拦调用return invocation.proceed();}//拦截器链处理过程中产生异常catch (Throwable ex) {//如果事务对异常进行回滚处理if (txAttr.rollbackOn(ex)) {//如果异常时运行时异常,则事务回滚处理   if (ex instanceof RuntimeException) {throw (RuntimeException) ex;}//如果不是运行时异常,则提交处理else {throw new ThrowableHolderException(ex);}}//如果事务对异常不进行回滚处理else {//提交处理return new ThrowableHolder(ex);}}//清除当前线程绑定的事务信息finally {cleanupTransactionInfo(txInfo);}}});//对调用结果异常进行处理。//如果是ThrowableHolder类型的异常,则转换为Throwable抛出if (result instanceof ThrowableHolder) {throw ((ThrowableHolder) result).getThrowable();}//如果不是ThrowableHolder类型的异常,则异常不做处理直接抛出else {return result;}}catch (ThrowableHolderException ex) {throw ex.getCause();}}}

通过上面对事务拦截器的拦截方法源码分析,我们可以看到Spring事务处理的基本工作流程:

(1)在调用方法时首先获取方法的事务属性配置,根据方法事务属性配置创建事务,获取Spring事务容器配置的事务处理器。

(2)调用具体的事务处理器对事务进行具体的处理。

(3)根据事务处理结果和事务处理过程中产生异常等进行提交或者回滚等处理。

0 0
原创粉丝点击