Search code examples
playframework-2.0squeryl

How to use Squeryl's externalTransactionManagementAdapter with play 2.0?


Has anyone succeeded in using Squeryl's externalTransactionManagementAdapter with play framework 2.0?:

    object Global extends GlobalSettings {
      override def onStart(app: Application) {

        SessionFactory.externalTransactionManagementAdapter = Some(() => 
            Some(new Session(
                DB.getDataSource().getConnection(), 
                dbAdapter)
            )
        )
    }

I am not able to get Squeryl to return the connections to the pool. It does work with SessionFactory.concreteFactory, but then I have to use transaction blocks instead of squeryl participating in Play's transaction management.

This question is a more specific variant of my earlier question: How to integrate the Scala Squeryl ORB with play 2.0 framework?.


Solution

  • I am currently "cheating", using SessionFactory.concreteFactory and :

    trait SquerylTransaction {
      def TransAction(f: Request[AnyContent] => Result): Action[AnyContent] = {
        Action { request =>
          transaction {
            f(request)
          }
        }
      }
    }
    

    and in the controller:

    object Application extends Controller with SquerylTransaction {
    
      def doStuff() = TransAction { 
        //squeryl query      
      }
    }
    

    but DeLonge's solution might be better.

    My Global.scala looks like this:

    object Global extends GlobalSettings {
    
      val dbAdapter = new PostgreSqlAdapter()
    
      override def onStart(app: Application): Unit = {
        SessionFactory.concreteFactory = Some(() =>
          Session.create(
            DB.getDataSource().getConnection(),
            dbAdapter))
      }
    
    }