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

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、下面有一點小改進,但還是有一些煩

以後,如果增加新的Service/Manager,則XML配置的增量是這一段:

上面說的是老的做法,比較傳統。缺點是增量比較大,配置起來copy&paste讓人覺得不太爽,比較臃腫。

3、用 DefaultAdvisorAutoProxyCreator 實現自動代理,實現了對容器中所有 bean 的自動代理, 增加一個需要事務的業務 bean 時只要在 transactionInterceptor 增加一行即可, 增加別的 interceptor 也非常方便,極大減少了配置量

 

 

4、改用BeanNameAutoProxyCreator,畢竟不是context下的每個bean都需要事務

以後每次的增量是這一段:

 

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時纔回滾事務,要使其它異常也回滾,需要在配置中作一些改變,見第一種配置中的相關信息。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章