目次
1.PlatformTransactionManagerインターフェース
2. TransactionDefinition インターフェース
1. 経営計画
宣言型トランザクションの最下層には AOP テクノロジーが採用されており、サービス層にトランザクションを手動で追加することで、前の記事で述べた問題を解決できます。
1. ビジネス層コードを変更する
SqlSessionTemplate オブジェクトを追加し、ビジネス メソッドをキャッチして、例外がなければ送信し、例外がキャッチされた場合はロールバックしてみましょう。
package com.example.service;
import com.example.dao.AccountDao;
import com.example.pojo.Account;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
public class AccountService {
@Autowired
private AccountDao accountDao;
@Autowired
private SqlSessionTemplate sessionTemplate;
/**
*
* @param id1 转出人id
* @param id2 转入人id
* @param price 金额
*/
// 作用方法上时,该方法都将具有该类型事务的事务属性
public void transfer(int id1,int id2, double price){
try {
// 转出人减少余额
Account account1 = accountDao.findById(id1);
account1.setBalance(account1.getBalance() - price);
accountDao.update(account1);
// 模拟程序出错
int i = 1 / 0;
// 转入人增加余额
Account account2 = accountDao.findById(id2);
account2.setBalance(account2.getBalance() + price);
accountDao.update(account2);
sessionTemplate.commit();
}
catch (Exception e){
e.printStackTrace();
sessionTemplate.rollback();
}
}
}
2. テスト
OK、プログラムが異常中断していることがわかります。次に、データベース内で何が起こっているかを観察してください。
ここでは、Zhang San からお金が差し引かれていないことがわかります。そのため、トランザクションを手動で送信することも可能ですが、この方法ではトライ キャッチが多すぎます。したがって、トランザクションマネージャーです。
2 番目に、トランザクション マネージャー
1 はじめに
Spring はトランザクション管理をトランザクション マネージャーに依存しています。トランザクション マネージャーは通知クラスであり、サービス層のメソッドとして通知クラスのカット ポイントを設定し、自動トランザクション管理を完了します。データベースを操作するテクノロジが異なるため、トランザクション操作を実行する方法も異なります。例: JDBC コミット トランザクションは connection.commit()、MyBatis コミット トランザクションは sqlSession.commit() であるため、Spring は複数のトランザクション マネージャーを提供します。
トランザクションマネージャー名 | 効果 |
org.springframework.jdbc.datasource.DataSourceTransactionManager | JDBC テクノロジーのトランザクション マネージャー。JDBC および MyBatis で動作します。 |
org.springframework.orm.hibernate3.HibernateTransactionManager | Hibernate フレームワークによって提供されるトランザクション マネージャー用。Hibernate フレームワークで動作します。 |
org.springframework.orm.jpa.JpaTransactionManager | JPA テクノロジーに提供されるトランザクション マネージャー。JPAテクノロジーに適用されます。 |
org.springframework.transaction.jta.JtaTransactionManager | 複数のトランザクション管理ソースにまたがります。2 つ以上の異なるデータ ソースでトランザクション制御を実装するのに適しています。 |
データベースの操作にはMyBatisを使用し、トランザクション管理にはDataSourceTransactionManagerを使用します。
2. 構成ファイルに制約を導入します。
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
3. トランザクション設定を実行します。
applicationContext.xml ファイルに構成を追加します。
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 进行事务相关配置 -->
<tx:advice id = "txAdvice">
<tx:attributes>
<!-- 代表所有方法 -->
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- 配置切面 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut id="pointcut" expression="execution(* com.example.service..*(..))"/>
<!-- 配置通知 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
3. トランザクション制御API
すべてのトランザクション マネージャーは PlatformTransactionManager インターフェイスを実装します。Spring のトランザクション制御機能は 3 つのインターフェイスによって提供されます。これら 3 つのインターフェイスは Spring によって実装されます。開発ではほとんど使用されません。これらの機能を理解するだけで済みます。
1.PlatformTransactionManagerインターフェース
PlatformTransactionManager は Spring が提供するトランザクション マネージャー インターフェイスであり、すべてのトランザクション マネージャーはこのインターフェイスを実装しています。このインターフェイスは、次の 3 つのトランザクション操作メソッドを提供します。
- TransactionStatus getTransaction (TransactionDefinition 定義): トランザクションのステータス情報を取得します。
- void commit (TransactionStatus ステータス): トランザクションのコミット
- void rollback(TransactionStatus status): トランザクションのロールバック
2. TransactionDefinition インターフェース
TransactionDefinition はトランザクションの定義情報オブジェクトであり、次のメソッドがあります。
- String getName(): トランザクションオブジェクト名を取得します。
- int getIsolationLevel(): トランザクションの分離レベルを取得します。
- int getPropagationBehavior(): トランザクションの伝播動作を取得します。
- int getTimeout(): トランザクションのタイムアウト時間を取得します。
- boolean isReadOnly(): トランザクションが読み取り専用かどうかを取得します。
3. TransactionStatusインターフェース
TransactionStatus はトランザクションのステータス インターフェイスであり、特定の時点でのトランザクションのステータス情報を記述します。次のようなメソッドがあります。
- void flash() リフレッシュトランザクション
- boolean hasSavepoint() セーブポイントがあるかどうかを取得します
- boolean isCompleted() トランザクションが完了したかどうかを取得します
- boolean isNewTransaction() 新規トランザクションかどうかを取得する
- boolean isRollbackOnly() ロールバックするかどうかを取得する
- void setRollbackOnly() トランザクションのロールバックを設定します
過去のコラムや記事の関連記事
今回の内容が分からない方は、過去号もご覧ください。以下は、Maven や Mybatis など過去にブロガーが丁寧に書いたコラム記事です。あなたは通り過ぎます!役に立った場合は、「いいね」やブックマークをお願いします。このうち、Springコラムは一部更新中のため閲覧できませんが、ブロガーが全ての更新を完了すると閲覧できるようになります。
1. Maven シリーズの列
Maven シリーズのコラム | Maven プロジェクト開発 |
Maven 集約開発 [例の詳細な説明 --- 5555 ワード] |