虽说利用Spring来实现配置式事务的基本原理都是AOP,但其配置方法也多种多样,以下从互联网摘抄了一些,希望起一个总结作用(有版权问题的话请留言作者,我将立即删除):
以下配置均忽略datasource,transactionManager,sessionFactory之类的配置,因为无论何种方式,前两者都不能少
1、 比较原始和烦琐的配置方法(每个SERVICE都来这么一下,不累死?):
<bean id="serviceMe" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<bean class="com.test.test.service.Service1">
<property name="myDao">
<ref bean="myDao" />
</property>
</bean>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED_NEW,-Exception</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
2、下面有一点小改进,但还是有一些烦
<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED, readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="userManager" parent="txProxyTemplate">
<property name="target">
<bean class="some.package.UserManagerImpl">
<property name="userDAO"><ref bean="userDAO"/></property>
</bean>
</property>
</bean>
以后,如果增加新的Service/Manager,则XML配置的增量是这一段:
<bean id="someOtherManager" parent="txProxyTemplate">
<property name="target">
<bean class="some.package.someOtherManagerImpl">
</bean>
</property>
</bean>
上面说的是老的做法,比较传统。缺点是增量比较大,配置起来copy&paste让人觉得不太爽,比较臃肿。
3、用 DefaultAdvisorAutoProxyCreator 实现自动代理,实现了对容器中所有 bean 的自动代理, 增加一个需要事务的业务 bean 时只要在 transactionInterceptor 增加一行即可, 增加别的 interceptor 也非常方便,极大减少了配置量
<beans>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributeSource">
<value>
com.skyon.user.manager.UserManager.*=PROPAGATION_REQUIRED
#Add new defines here ->
</value>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
<!--
增加新的 Interceptor
-->
</list>
</property>
</bean>
<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor" ref="transactionInterceptor"/>
</bean>
</beans>
4、改用BeanNameAutoProxyCreator,毕竟不是context下的每个bean都需要事务
<beans>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"/>?
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*Service,*Manager</value>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
<!-- 此处增加新的Interceptor -->
</list>
</property>
</bean>
<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor" ref="transactionInterceptor"/>?
</bean>
<bean id="userManager" class="some.package.UserManagerImpl" autoWire="byName"/>
</beans>
以后每次的增量是这一段:
<bean id="userManager" class="some.package.UserManagerImpl" autoWire="byName"/>
5、BeanNameAutoProxyCreator的另一种配法
<!-- Transaction Interceptor set up to do PROPOGATION_REQUIRED on all methods -->
<bean id="matchAllWithPropReq"
class="org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource">
<property name="transactionAttribute"><value>PROPAGATION_REQUIRED</value></property>
</bean>
<bean id="matchAllTxInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="transactionAttributeSource"><ref bean="matchAllWithPropReq"/></property>
</bean>
<!-- One BeanNameAutoProxyCreator handles all beans where we want all methods to use
PROPOGATION_REQUIRED -->
<bean id="autoProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<idref local="matchAllTxInterceptor"/>
<idref bean="hibInterceptor"/>
</list>
</property>
<property name="beanNames">
<list>
<idref local="core-services-applicationControllerSevice"/>
<idref local="core-services-deviceService"/>
<idref local="core-services-authenticationService"/>
<idref local="core-services-packagingMessageHandler"/>
<idref local="core-services-sendEmail"/>
<idref local="core-services-userService"/>
</list>
</property>
</bean>
其它一些注意:
1、如果你想使用配置事务,但又不想使用Spring的模板方法,要注意,获取connection时不能直接从dataSource获得:
Connection conn = DataSourceUtils.getConnection(dataSource);
2、 默认情况下SPRING只有在RuntimeException时才回滚事务,要使其它异常也回滚,需要在配置中作一些改变,见第一种配置中的相关信息。
分享到:
相关推荐
spring3.0两种事务管理配置
spring的事务管理配置详解.txt详细的描述了如何配置spring的事物。
Spring事务管理Demo
spring声明事务的配置 spring声明事务的配置 spring声明事务的配置
Spring声明式事务配置管理方法
Java高级编程 实验报告 spring 声明事务 实验目的 掌握spring 声明式事务管理配置 实验环境 本实验采用本实验采用的eclipse或者 Myeclpse开发工具。Spring 4.0以上 Jdk1.7以上、oracle/mysql。
本代码使用H2内存数据库演示spring事务使用,包括编程式事务,声明式事务@Transactional使用,自定义事务事务注解实现自定义事务管理器
spring事务配置详解 spring事务配置详解
Spring事务管理教程,详细讲解了Spring中的事务管理,包括声明式事务,注解式事务,以及事务配置等等
很好的spring+ibatis事务的配置文档.
Spring的事务配置的五种方式 Spring的事务配置的五种方式 Spring的事务配置的五种方式 Spring的事务配置的五种方式
spring几种事务配置,spring几种事务配置详解【精】
Spring AOP配置事务方法,描述了spring的事务配置,方便开发应用和数据库的连接管理。
spring学习事务源码spring学习事务源码spring学习事务源码spring学习事务源码spring学习事务源码spring学习事务源码spring学习事务源码spring学习事务源码
spring事务管理 配置文件等详解
Spring事务管理.pdf 1.资料 2.本地事务与分布式事务 3.编程式模型 4.宣告式模型
javaEE 实验三 Spring JDBC与事务管理, 一、实验目的 1、掌握Spring JDBC的配置; 2、掌握JdbcTemplae类中增删改查方法的使用; 3、了解Spring事务管理的3个核心接口; 4、了解Spring事务管理的两种方式; 5、掌握...
Spring事务管理配置
Spring 事务代理配置 Spring 事务代理配置
Spring中事务的传播属性详解,Spring中事务的传播属性详解