如何强制在 Lift Mapper 中创建新事务?

And*_*yuk 5 scala transactions lift lift-mapper

我们在项目中使用 Lift + Mapper(版本 2.4)。我们也使用每个请求的事务模式S.addAround(DB.buildLoanWrapper())

\n\n

在我们的一个请求中,我们需要嵌套事务,我们发现这是有问题的。我们发现可能的“黑客”之一是在单独的线程中启动事务(如下例所示),因为DB对象用于ThreadLocal管理当前连接和事务状态信息。

\n\n

是否有任何实现比下面的更好(更安全并且没有多线程)?

\n\n
  import net.liftweb.db.{DefaultConnectionIdentifier, DB}\n  import akka.dispatch.Future\n\n  /**\n   * Will create a new transaction if none is in progress and commit it upon completion or rollback on exceptions.\n   * If a transaction already exists, it has no effect, the block will execute in the context\n   * of the existing transaction. The commit/rollback is handled in this case by the parent transaction block.\n   */\n  def inTransaction[T](f: \xe2\x87\x92 T): T = DB.use(DefaultConnectionIdentifier)(conn \xe2\x87\x92 f)\n\n  /**\n   * Causes a new transaction to begin and commit after the block\xe2\x80\x99s execution,\n   * or rollback if an exception occurs. Invoking a transaction always cause a new one to be created,\n   * even if called in the context of an existing transaction.\n   */\n  def transaction[T](f: \xe2\x87\x92 T): T = Future(DB.use(DefaultConnectionIdentifier)(conn \xe2\x87\x92 f)).get\n
Run Code Online (Sandbox Code Playgroud)\n

naf*_*afg 0

不幸的是,似乎没有现有的 API。您可以询问是否可以在 google 群组中添加一个。然而,没有什么可以阻止你做这样的事情:

DB.use(DefaultConnectionIdentidier){ sc =>
  val conn: java.sql.Connection = sc.connection
  // use regular JDBC mechanism here
}
Run Code Online (Sandbox Code Playgroud)