基於XML的聲明式事務控制

基於XML的聲明式事務控制

由於事務控制基於AOP,所以要導入XML名稱空間和約束xmlns:aop

還要導入事務的名稱空間與約束xmlns:tx


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx
       https://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd">

1. 配置事務管理器DataSourceTransactionManager並注入數據源

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

2. 配置事務的通知<tx:advice>

<tx:advice id="txAdvice" transaction-manager="transactionManager"></tx:advice>

3.配置AOP

通過上面的配置,事務的通知對象已經被指定,現在,只需要將事務的通知對象與切入點聯繫起來,就構成了切面。

    <aop:config>
        <!-- 業務邏輯層的所有方法都是事務 -->
        <aop:pointcut id="txPointcut" expression="execution(* com.test.service.*.*(..))"/>
        <!-- 切入點需要與事務的通知相聯繫 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"></aop:advisor>
    </aop:config>

4. 配置事務屬性

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="transfer" isolation="" no-rollback-for="" propagation="" read-only="" rollback-for="" timeout=""/>
        </tx:attributes>
    </tx:advice>

<tx:method>屬性介紹

  • name:方法名(業務邏輯層的一個方法,就是一個事務)可以使用通配符
  • isolation:事務的隔離級別(默認值爲DEFAULT)
  • propagation:用於指定事務的傳播行爲。默認值是REQUIRED,表示一定會有事務。(增、刪、改)的選擇
  • read-only:用於指定事務是否只讀。默認爲false。只有查詢方法才能設置爲true
  • timeout:超時時間。默認值-1,表示不會超時。以秒爲單位。
  • rollback-for:用於指定一個異常,當產生該異常時,事務回滾,產生其他異常時,事務不回滾。默認情況下,任何異常都回滾
  • no-rollback-for:用於指定一個異常,表示產生該異常時,事務不回滾,產生其他異常時,事務回滾。默認情況下,任何異常都回滾

事務屬性設置示例

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" read-only="false" propagation="REQUIRED"></tx:method>
            <tx:method name="get*" read-only="true" propagation="SUPPORTS"></tx:method>
        </tx:attributes>
    </tx:advice>

上面的例子表示,對於以get開頭的方法(查詢方法),都是隻讀的,事務的傳播行爲是SUPPORTS,表示可以有事務,也可以沒有(取決於具體的實現)

對於除了查詢的任意方法(*通配優先級沒有get*),都不是隻讀的,並且一定有事務。

完整配置示例


    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" read-only="false" propagation="REQUIRED"></tx:method>
            <tx:method name="get*" read-only="true" propagation="SUPPORTS"></tx:method>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <!-- 業務邏輯層的所有方法都是事務 -->
        <aop:pointcut id="txPointcut" expression="execution(* com.test.service.*.*(..))"/>
        <!-- 切入點需要與事務的通知相聯繫 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"></aop:advisor>
    </aop:config>



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