Created
August 20, 2012 05:16
-
-
Save komiya-atsushi/3401163 to your computer and use it in GitHub Desktop.
Play framework 向けの、トランザクションを一時的に切り替える処理をお手伝いする機能。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package helpers; | |
import play.db.jpa.JPA; | |
import play.db.jpa.JPAPlugin; | |
/** | |
* トランザクションを一時的に切り替える機能を提供します。 | |
* <p> | |
* トランザクションの切り替え・切り戻しは内部で自動的に行われます。 | |
* </p> | |
* | |
* @author KOMIYA Atsushi | |
* | |
* @param <T> | |
*/ | |
public abstract class AdHocTx<T> { | |
/** | |
* サブクラスにおいて、実際の処理をこの execute() メソッドに記述します。 | |
* <p> | |
* doIt() メソッドより、トランザクションを差し替えた状態で呼び出されます。 | |
* </p> | |
* | |
* @return 任意の値を返却してください。 | |
*/ | |
public abstract T execute(); | |
/** | |
* 現行のトランザクションとは別のトランザクションを生成・差し替えて execute() メソッドを呼び出します。 | |
* <p> | |
* 正常終了・異常終了問わず、内部で生成したトランザクションは、このメソッドから抜ける前に自動的にクローズされます。 | |
* </p> | |
* | |
* @return execute() メソッドの戻り値 | |
*/ | |
public T doIt() { | |
JPA currentContext = JPA.local.get(); | |
JPA.local.remove(); | |
JPAPlugin.startTx(false); | |
T result = null; | |
boolean rollback = true; | |
try { | |
result = execute(); | |
rollback = false; | |
} finally { | |
JPAPlugin.closeTx(rollback); | |
JPA.local.set(currentContext); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment