「Spring」トランザクションを実装するサンプル
説明
1.DefaultTransactionDefinition
TransactionDefinition インターフェースのデフォルト実装。
Bean スタイルの構成と適切なデフォルト値(PROPAGATION_REQUIRED、ISOLATION_DEFAULT、TIMEOUT_DEFAULT、readOnly = false)を提供します。
2.PlatformTransactionManager
アプリケーションは TransactionTemplate または AOP による宣言型トランザクション境界のいずれかで動作します。
サンプルコード
@Autowired
PlatformTransactionManager txManager;
public void insertData(Locale locale, String empInfo) {
// トランザクション管理の開始
DefaultTransactionDefinition dtd = null;
TransactionStatus ttStatus = null;
try {
dtd = new DefaultTransactionDefinition();
dtd.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
ttStatus = txManager.getTransaction(dtd);
// DB操作 somecode
// コミット操作
txManager.commit(ttStatus);
} catch (Exception e) {
// ロールバック
txManager.rollback(ttStatus);
}
}