What's the conventional way to handle a transaction in Play 2 Scala? What's the conventional way to handle a transaction in Play 2 Scala? database database

What's the conventional way to handle a transaction in Play 2 Scala?


I don't if it is a better alternative, but you could also use Action Composition instead of creating a subclass via inheritance.

Basically, you could write something like this:

def TransactionalAction(f: Request[AnyContent] => Result): Action[AnyContent] = {  Action { request =>    startTransaction    try {        f(request)        commit    } catch {        case e: Exception => rollback    }  }}

and then use:

def index = TransactionalAction { request =>  val something = someQueriesInDB  Ok(something)}


I'm not sure that there is a best practice for this yet. I look forward to reading other people's answers.

This page should show you how to do what you want to do. Basically in Global you either extend WithFilters or override doFilter. You're still just wrapping the Action, but you're doing it from a central place.

Now, whether or not this is a better idea than doing the action compsoition as suggested here, I don't know.