10.7.5. トランザクションのコミット
トランザクションは、コミットする前に開始する必要があります。トランザクションの開始方法については、「トランザクションの開始」を参照してください。
UserTransaction
のcommit()
メソッドを呼び出します。UserTransaction
のcommit()
メソッドを呼び出すと、トランザクションマネージャーがトランザクションをコミットしようとします。@Inject private UserTransaction userTransaction; public void updateTable(String key, String value) EntityManager entityManager = entityManagerFactory.createEntityManager(); try { userTransaction.begin(): <!-- Perform some data manipulation using entityManager --> ... // Commit the transaction userTransaction.commit(); } catch (Exception ex) { <!-- Log message or notify Web page --> ... try { userTransaction.rollback(); } catch (SystemException se) { throw new RuntimeException(se); } throw new RuntimeException(e); } finally { entityManager.close(); } }
Container Managed Transactions (CMT) を使用する場合は、手動でコミットする必要がありません。
Bean が Container Managed Transactions を使用するよう設定すると、コンテナはコードで設定したアノテーションに基づいてトランザクションライフサイクルを管理します。
データソースがコミットし、トランザクションが終了します。そうでない場合は、例外がスローされます。
注記