4

プロジェクトでは Lift + Mapper (バージョン 2.4) を使用しています。同様に、トランザクションごとのリクエスト パターンを使用していますS.addAround(DB.buildLoanWrapper())

私たちの要求の 1 つで、問題があることがわかったネストされたトランザクションが必要です。DBオブジェクトはThreadLocal現在の接続とトランザクションの状態情報を管理するために使用されるため、考えられる「ハック」の 1 つは別のスレッドでトランザクションを開始することであることがわかりました (以下の例のように) 。

以下のものよりも優れた(より安全でマルチスレッドなしの)実装はありますか?

  import net.liftweb.db.{DefaultConnectionIdentifier, DB}
  import akka.dispatch.Future

  /**
   * Will create a new transaction if none is in progress and commit it upon completion or rollback on exceptions.
   * If a transaction already exists, it has no effect, the block will execute in the context
   * of the existing transaction. The commit/rollback is handled in this case by the parent transaction block.
   */
  def inTransaction[T](f: ⇒ T): T = DB.use(DefaultConnectionIdentifier)(conn ⇒ f)

  /**
   * Causes a new transaction to begin and commit after the block’s execution,
   * or rollback if an exception occurs. Invoking a transaction always cause a new one to be created,
   * even if called in the context of an existing transaction.
   */
  def transaction[T](f: ⇒ T): T = Future(DB.use(DefaultConnectionIdentifier)(conn ⇒ f)).get
4

1 に答える 1