使用如下配置方式实现:
无论在方法中发生任何异常,只要方法中抛出一个Exception,包括是自己设置抛出的异常,事务都进行回滚
对比1.1与2.0中事务的配置方法
配置文件如下 1.1
- <bean id="baseTxService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
- <property name="transactionManager" ref="transactionManager"/>
- <property name="proxyTargetClass" value="true"/>
- <property name="transactionAttributes">
- <props>
- <prop key="get*">PROPAGATION_REQUIRED,readOnly </prop>
- <prop key="find*">PROPAGATION_REQUIRED,readOnly </prop>
- <prop key="save*">PROPAGATION_REQUIRED,-Exception </prop>
- <prop key="remove*">PROPAGATION_REQUIRED ,-Exception </prop>
- props>
- property>
- <property name="preInterceptors">
- <list>
- <ref bean="methodSecurityInterceptor"/>
- list>
- property>
- bean>
- <bean id="bookManager" parent="baseTxService">
- <property name="target">
- <bean class="org.springside.bookstore.admin.manager.BookManager"/>
- property>
- bean>
spring 2.0 中事务配置方法
- <aop:config proxy-target-class="true">
- <aop:advisor pointcut="execution(* *..*Manager.*(..))" advice-ref="txAdvice"/>
- <aop:advisor pointcut="execution(* *..*Manager.save(..))" advice-ref="fooAdvice"/> aop:config>
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="save*"/>
- <tx:method name="remove*"/>
- <tx:method name="*" read-only="true"/>
- tx:attributes>
- tx:advice>
- <bean id="bookManager" class="org.springside.bookstore.commons.service.BookManager"/>