Spring枠組み事務管理の三:Spring声明式事務


1.Spring宣言式事務とJavaEE CMTの対比
JavaEE CMTはJTA事務APIに依存し、Spring宣言式事務は複数の事務APIに依存することができる.
JavaEE CMTはEJBコンポーネントにしか使えません.Spring宣言式トランザクションは任意のクラスに使用できます.
JavaEE CMTは、トランザクションのコンテキストをサポートするためのリモート呼び出しの普及、Springトランザクションは無力です.
JavaEE CMTはjava.rmi.RemoteExceptionの異常に遭遇した時だけ、仕事に戻ります.Spring宣言式事務はデフォルトではJavaEE CMTと一致していますが、声明式の事務ロールバック規則によりロールバック業務の条件をカスタマイズすることもできます.
2.Spring宣言式事務の実現メカニズム
Spring宣言式事務はSpringのAOPエージェント機構に依存して実現される.
まず、表示またはXMLのメタデータ宣言とAOPを組み合わせてAOPエージェントを生成します.
その後、AOPプロキシはorg.springframe ebork.transactions.interceptort.Transaction Interceptorと具体的なPlatform Transation Managerによって、メソッドに呼び出されたトランザクション操作を実現します.
3.Spring宣言式事務の表示(略)
@javax.transactional.Transation al
4.Spring宣言式事務のXML配置
<!-- from the file 'applicationContext.xml' -->
<?xml version="1.0" encoding="UTF-8"?>
<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
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- this is the object that we want to be made transactional -->
    <bean id="fooService" class="x.y.service.DefaultFooService"/>

    <!-- the transactional advice -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <!-- the transactional semantics... -->
        <tx:attributes>
            <!-- all methods starting with 'get' are read-only -->
            <tx:method name="get*" read-only="true"/>
            <!-- other methods use the default transaction settings -->
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!-- ensure that the above transactional advice runs for any execution
        of an operation defined by the FooService interface -->
    <aop:config>
        <aop:pointcut id="fooServiceOperation" expression="execution(* x.y.service.FooService.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
    </aop:config>

    <!-- the DataSource -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis"/>
        <property name="username" value="scott"/>
        <property name="password" value="tiger"/>
    </bean>

    <!-- the PlatformTransactionManager -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- other <bean/> definitions here -->

</beans>
説明:
x.y.service.FooServiceは、定義された論理的方法がトランザクションの動作に参加されるインターフェースである.x.y.service.Default FooServiceはFooServiceインターフェースの一つの実現です.
論理的方法を事務操作に組み込むことは、<tx:advice>通知により実現される.tx:advice'は、tractionn-manager属性によって使用されるトランザクションマネージャを指定するだけでなく、事務動作に組み込まれる論理的方法をサブ要素によって指摘しています.
論理的な方法はいつ事務に入れられて実行されますか?を通じて(通って)提供して、そのは事務の実行に入る条件を提供して、は論理の方法と入って事務の実行する条件を結び付けます.
デフォルトでは、事務実行中に、java.rmi.RemoteException異常が発生してこそ、業務がロールバックします.
トランザクション通知<tx:advice>通知を定義する場合、トランザクションに参加する論理的方法にロールバックの異常を指定することができ、すなわち指定異常が発生したらロールバックするという論理的方法の例は以下の通りである.
<tx:method name="*" rollback-for="Throwable" no-rollback-for="InstrumentNotFoundException"/>
さらに、異なる論理的方法のために、異なるエントリトランザクションの実行条件を設定してもよい.例は以下の通りである.
    <aop:config>
        <aop:pointcut id="defaultServiceOperation" expression="execution(* x.y.service.*Service.*(..))"/>
        <aop:pointcut id="noTxServiceOperation" expression="execution(* x.y.service.ddl.DefaultDdlManager.*(..))"/>

        <aop:advisor pointcut-ref="defaultServiceOperation" advice-ref="defaultTxAdvice"/>
        <aop:advisor pointcut-ref="noTxServiceOperation" advice-ref="noTxAdvice"/>
    </aop:config>
    <tx:advice id="defaultTxAdvice">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    <tx:advice id="noTxAdvice">
        <tx:attributes>
            <tx:method name="*" propagation="NEVER"/>
        </tx:attributes>
    </tx:advice>
5.補足説明<tx:advice>の設定
デフォルトの場合に設定されているトランザクションパラメータは以下の通りです.
  • トランザクションは、読み書きが可能であり、すなわちread−only=「false」
  • である.
  • 事務分離レベルはDEFAULTであり、すなわちisolation=「DEFAULT」
  • である.
  • 事務伝播タイプはREQUIREDであり、すなわちプロパモーション=「REQUIRED」
  • である.
  • トランザクションのタイムアウトは−1であり、timeout=−1であり、トランザクションを実行するシステムを表す設定
  • である.
  • 事務のロールバックの異常はjava.rmi.RemoteException
  • です.