关闭

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

2003人阅读 评论(0) 收藏 举报

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"/>

 

5BeanNameAutoProxyCreator的另一种配法

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

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:171594次
    • 积分:2630
    • 等级:
    • 排名:第13820名
    • 原创:84篇
    • 转载:16篇
    • 译文:0篇
    • 评论:90条
    最新评论
    blog life