How to enforce creating new transaction in Liftweb Mapper?

Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

How to enforce creating new transaction in Liftweb Mapper?

Andriy Plokhotnyuk
This post has NOT been accepted by the mailing list yet.
Is it safe implementation for Liftweb 2.4?
Is there any implementation that is better than bellow one?

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

val timeout = 60 * 1000 // 1 minute
val dispatcher = Dispatchers.newExecutorBasedEventDrivenWorkStealingDispatcher("transaction-provider-dispatcher")
  .setMaxPoolSize(25) // should be same as max number of connection in DB pool
  .build

/**
 * 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 newTransaction[T](f: ⇒ T): T = Future(DB.use(DefaultConnectionIdentifier)(conn ⇒ f), timeout)(dispatcher).get