How to use Squeryl externalTransactionManagementAdapter with game 2.0?

Has anyone succeeded in using Squeryl 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 cannot get Squeryl to return connections to the pool. It works with SessionFactory.concreteFactory , but then I have to use transaction blocks instead of squeryl involved in Play transaction management.

This question is a more specific version of my previous question: How to integrate Scala Squeryl ORB with 2.0 play card? .

+4
source share
2 answers

I am currently β€œtricking” 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 a DeLonge solution could 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)) } } 
0
source

This has haunted me for the past two days, so I grabbed coffee and wasted an hour of my life, but I would like to show you how I earned:

In Global.scala put this:

  override def onStart(app: Application) { SessionFactory.externalTransactionManagementAdapter = Some(() => { if(org.squeryl.Session.hasCurrentSession) { org.squeryl.Session.currentSessionOption.get } else { val s = new org.squeryl.Session(DB.getDataSource().getConnection(), new PostgreSqlAdapter){ override def cleanup = { super.cleanup unbindFromCurrentThread } } s.bindToCurrentThread s } }) } 

And then you will need to clean up a bit so that your application does not distort (in the same global):

  /** * cleans up Squeryl thread to each request */ override def onRouteRequest(request: RequestHeader): Option[Handler] = { org.squeryl.Session.currentSessionOption.foreach(_.unbindFromCurrentThread) super.onRouteRequest(request) } 

I will update this if I find any reservations, etc. Turning purification kindly http://lunajs.blogspot.ca/2011/06/squeryl-with-java-experiment.html

+3
source

Source: https://habr.com/ru/post/1402197/


All Articles