宣言型トランザクション制御
1.プログラムによるトランザクション制御関連オブジェクト
1.1 PlatformTransactionManager
PlatformTransactionManagerインターフェースはSpringのトランザクションマネージャーであり、トランザクションを操作するために一般的に使用するメソッドを提供します。
注意:
PlatformTransactionManagerはインターフェースタイプであり、Daoレイヤーテクノロジーが異なれば実装クラスも異なります。たとえば、Daoレイヤーテクノロジーがjdbcまたはmybatisの場合:org.springframework.jdbc.datasource.DataSourceTransactionManager
Daoレイヤーテクノロジーが休止状態の場合:org.springframework.orm.hibernate5.HibernateTransactionManager
1.2 TransactionDefinition
TransactionDefinitionは、トランザクションの定義情報オブジェクトであり、次のメソッドがあります。
2XMLベースの宣言型トランザクション制御
2.1宣言型トランザクション制御とは
Springの宣言型トランザクションは、その名前が示すように、宣言型アプローチを使用してトランザクションを処理することです。ここで説明する宣言は、構成ファイル内の宣言を参照しており、コード処理トランザクションの代わりに、Spring構成ファイル内の宣言型トランザクション処理が使用されます。
宣言型トランザクション処理の役割
-
トランザクション管理は、開発されたコンポーネントに侵入しません。具体的には、ビジネスロジックオブジェクトは、トランザクション管理にあることを認識しません。トランザクション管理はシステムレベルのサービスであり、ビジネスロジックの一部ではないため、実際にはそうする必要があります。トランザクション管理プランを変更する場合は、定義ファイルで再構成する必要があります
-
トランザクション管理が不要な場合は、コードを変更したり再コンパイルしたりせずに設定ファイルを変更すれば、トランザクション管理サービスを削除でき、メンテナンスが非常に便利です。
注:Springの宣言型トランザクション制御の最下層はAOPです。
2.2宣言型トランザクション制御の実装
宣言型トランザクション制御の明確な問題:
-
カットポイントは誰ですか?
-
通知は誰ですか?
-
構成の側面?
-
①tx名前空間を導入
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
②構成トランザクションの強化
<!--平台事务管理器-->
<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="*"/>
</tx:attributes>
</tx:advice>
③構成トランザクションAOP織り
<!--事务的aop增强-->
<aop:config>
<aop:pointcut id="myPointcut" expression="execution(* com.itheima.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"></aop:advisor>
</aop:config>
④トランザクション制御転送ビジネスコードのテスト
@Override
public void transfer(String outMan, String inMan, double money) {
accountDao.out(outMan,money);
int i = 1/0;
accountDao.in(inMan,money);
}
3アノテーションベースの宣言型トランザクション制御
3.1注釈を使用して宣言型トランザクション制御を構成する
- AccoutDaoを書く
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void out(String outMan, double money) {
jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);
}
public void in(String inMan, double money) {
jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);
}
}
- AccoutServiceを書く
@Service("accountService")
@Transactional
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
public void transfer(String outMan, String inMan, double money) {
accountDao.out(outMan,money);
int i = 1/0;
accountDao.in(inMan,money);
}
}
- applicationContext.xml構成ファイルを作成します
<!—之前省略datsSource、jdbcTemplate、平台事务管理器的配置-->
<!--组件扫描-->
<context:component-scan base-package="com.itheima"/>
<!--事务的注解驱动-->
<tx:annotation-driven/>
3.2アノテーション構成の宣言型トランザクション制御分析
①@ Transactionalを使用して、トランザクション制御が必要なクラスまたはメソッドを変更します。アノテーションに使用できる属性は、分離レベル、伝播動作など、xml構成メソッドと同じです。
②クラスでアノテーションが使用され、クラス内のすべてのメソッドが同じアノテーションパラメータ設定のセットを使用します。
③メソッドに関しては、メソッドごとに異なるトランザクションパラメータ構成を使用できます。
④Xml設定ファイルで開くトランザクションのアノテーションドライブ<tx:annotation-driven />
3.3知識のポイント
宣言型トランザクション制御の構成の要点
-
プラットフォームトランザクションマネージャーの構成(xmlモード)
-
トランザクション通知の構成(@Transactionalアノテーション構成)
-
トランザクションアノテーション駆動型構成tx:annotation-driven /