spring管理事务的若干配置形式

虽说利用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 usePROPOGATION_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时才回滚事务,要使其它异常也回滚,需要在配置中作一些改变,见第一种配置中的相关信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值