【一】spring事务管理
(1)spring的事务管理,是基于aop动态代理实现的。对目标对象生成代理对象,加入事务管理的核心拦截器==>org.springframework.transaction.interceptor.TransactionInterceptor。
===>spring事务管理的核心拦截器
===>需要配置的数据项:事务管理机制配置属性的查找类transactionAttributeSource,事务管理的核心处理器PlatformTransactionManager(如果能配置就配置,不能配置就从beanFactory中根据接口拿)
(2)实现事务管理需要配置事务管理处理器(事务处理的支持抽象封装(获取事务状态,提交事务,回归事务),如下是spring事务管理器的基础类。
==>org.springframework.transaction.PlatformTransactionManager
==>org.springframework.transaction.support.AbstractPlatformTransactionManager
(3)获取将要执行的方法的事务策略的配置信息的查询器。
==>org.springframework.transaction.interceptor.TransactionAttributeSource
==>org.springframework.transaction.annotation.AnnotationTransactionAttributeSource(基于注解进行事务管理配置的属性获取器)
==>该类内部也做属性配置缓存。以要执行的方法的对象Method method,和要执行的bean的Class<?> targetClass组装成org.springframework.transaction.interceptor.AbstractFallbackTransactionAttributeSource.DefaultCacheKey.该类重写了equals和hashCode方法。
(4)spring事务管理的配置属性的实体类
==>org.springframework.transaction.interceptor.TransactionAttribute
==>org.springframework.transaction.interceptor.DefaultTransactionAttribute(spring的默认)
==>org.springframework.transaction.interceptor.RuleBasedTransactionAttribute(基于注解的事务管理配置属性的类)
==>其实就是,事务的传播机制,事务回滚策略等配置信息
(5)spring事务管理的一个FactoryBean
==>org.springframework.transaction.interceptor.TransactionProxyFactoryBean
==>内部初始化TransactionInterceptor,配置项:可以配置事务管理拦截器增强之外的别的拦截器,需要进行事务管理的的target, 进行事务管理的目标的接口proxyInterfaces
==>该类内部会调用afterPropertiesSet()方法,对目标target类生成一个代理对象。最终返回给业务使用。
【二】事务管理拦截器的执行过程TransactionInterceptor的invoke(final MethodInvocation invocation)方法
(1)获取要进行事务管理的业务类的class的类对象
(2)根据类对象class和要执行的方法的method对象,基于事务管理配置属性查询器获取事务机制的属性TransactionAttribute
(3)根据事务配置机制的属性获取事务管理的处理器PlatformTransactionManager
(4)根据类对象class和要执行的方法method对象获取事务管理,连接点标识joinpointIdentification(类的全路径+执行方法的名字)
(5)根据事务管理处理器PlatformTransactionManager,事务机制配置属性TransactionAttribute,事务连接点标识joinpointIdentification获取当前事务信息TransactionInfo
(6)继续执行下一个拦截器或目标业务管理bean的方法
(7)根据(6)的执行结果进行相应的事务操作
(8)如果(6)没有抛出异常,则先根据TransactionInfo进行相关资源的清理,然后根据TransactionInfo进行事务提交操作
(9)如果(6)抛出异常,则根据TransactionInfo进行事务回滚操作
【三】以xml配置方式进行事务管理的初始化原理解析
(1) 以下配置是事务管理机制属性配置
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="doReweight" propagation="REQUIRES_NEW"/> <tx:method name="doClear*" propagation="REQUIRES_NEW"/> <tx:method name="doSend*" propagation="REQUIRES_NEW"/> <tx:method name="doBatchSave*" propagation="REQUIRES_NEW"/> <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到--> <tx:method name="get*" propagation="REQUIRED" read-only="true"/> <tx:method name="count*" propagation="REQUIRED" read-only="true"/> <tx:method name="find*" propagation="REQUIRED" read-only="true"/> <tx:method name="list*" propagation="REQUIRED" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config expose-proxy="true" proxy-target-class="true"> <!-- 只对业务逻辑层实施事务 --> <aop:pointcut id="txPointcut" expression="execution(* com.mobile.thinks..service..*+.*(..))"/> <aop:advisor id="txAdvisor" advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config>