net.java.ao.Transactionの概要(javadocの日本語訳)

https://activeobjects.dev.java.net/api/net/java/ao/Transaction.htmlのクラスの概要を翻訳しました。

本文

ActiveObjects APIでは、簡単な構文でデータベーストランザクションを使用できます。このクラスの構文は、RailsActiveRecord ORMが提供する「トランザクション do ... end」構文を基にして作られています。トランザクション機能を可能な限り簡単になるようにカプセル化して提供することを意図しています。

Allows for the syntactically simple use of database transactions within the ActiveObjects API. This class's syntax is modeled after the transaction do ... end syntax provided by Rails's ActiveRecord ORM. The intention is to provide the simplest possible encapsulation around the transaction functionality. As such, AO transactions lack some of the power of the underlying database transaction function (such as arbitrary save-points).

トランザクションの背後には、以下のコード片に基づく設計があります。

The design behind Transaction is modeled after the following code snippet:

new Transaction(manager) {
     public Object run() {
         Account a = getEntityManager().get(Account.class, 1);
         Account b = getEntityManager().get(Account.class, 2);
         
         a.setBalance(a.getBalance() - 1000);
         a.save();
         
         b.setBalance(b.getBalance() + 1000);
         b.save();
         
         return null;
     }
 }.execute();

トランザクションは、run()メソッドが終了して初めて、コミットされます。したがって、a.save()がすぐにデータベースの値を変更することはなく、トランザクションのコミット時になって変更します。もしコンフリクトが起こった場合、JDBCは自動的にSQL例外を投げます。トランザクションはこの例外をキャッチし、データの整合性を保つためにトランザクションロールバックします。トランザクションが一度ロールバックされると、execute()メソッドにより例外が投げなおされます。

The transaction will be committed only after the run() method returns. Thus, a.save() doesn't immediately modify the database values, only upon the committal of the transaction. If any conflicts are detected, JDBC will automatically throw an SQLException. Transaction catches this exception and rolls back the transaction, ensuring data integrity. Once the transaction is rolled back, the exception is rethrown from the execute() method.

トランザクションからの戻り値の返却は、runメソッドでパラメータ化された型を返すことで実現されます。例えば、もしアカウントを作成するためにトランザクションを使用するとすれば、

In cases where the transaction generates data which must be returned, this can be accomplished by returning from the run() method against the parameterized type. Thus if a transaction to create an account is utilized:

Account result = new Transaction(manager) {
     public Account run() throws SQLException {
         Account back = getEntityManager().create(Account.class);
         
         back.setBalance(0);
         back.save():
         
         return back;
     }
 }.execute();

run()から返された値は、コールスタックの上位のexecute()渡され、execute()が呼び出し元へ値を返します、例えばこの例では、トランザクション内のbakインスタンスがそのまま戻り値になります。この機能によりトランザクションのスコープの外にデータを取り出せるため、とても使いやすくなっています。

The value returned from run() will be passed back up the call stack to execute(), which will return the value to the caller. Thus in this example, result will be precisely the back instance from within the transaction. This feature allows data to escape the scope of the transaction, thereby achieving a greater usefulness.

JDBCのトランザクションの種別としては、Connection.TRANSACTION_SERIALIZABLEが使用されます。。

The JDBC transaction type used is Connection.TRANSACTION_SERIALIZABLE.