Workaround for #950 + #999
authorTomas Zeman <tzeman@volny.cz>
Fri, 10 Feb 2012 09:53:08 +0100
changeset 14 5a2f996a5ba0
parent 13 29d4ddcd85c8
child 15 995184977e9b
Workaround for #950 + #999 #950 Connections unexpectedly closed when using DB.use with a JNDI datasource with squerylrecord https://www.assembla.com/spaces/liftweb/tickets/950 #999 DB connection issue using Menu.param https://www.assembla.com/spaces/liftweb/tickets/999
src/main/scala/bootstrap/liftweb/Boot.scala
--- a/src/main/scala/bootstrap/liftweb/Boot.scala	Fri Feb 10 09:53:08 2012 +0100
+++ b/src/main/scala/bootstrap/liftweb/Boot.scala	Fri Feb 10 09:53:08 2012 +0100
@@ -29,9 +29,13 @@
   override def boot = {
 
     /* DB stuff */
-    DB.defineConnectionManager(FisConnectionIdentifier, new FisDbVendor)
+    val dbVendor = new FisDbVendor
+    DB.defineConnectionManager(FisConnectionIdentifier, dbVendor)
     S.addAround(DB.buildLoanWrapper(List(FisConnectionIdentifier)))
     SquerylRecord.init(() => new SeqIdPostgreSqlAdapter)
+    SquerylTxMgr.initSqueryl(FisConnectionIdentifier,
+      () => new SeqIdPostgreSqlAdapter)
+    LiftRules.unloadHooks.append(dbVendor.closeAllConnections_! _)
 
     super.boot
 
@@ -54,4 +58,31 @@
   }
 }
 
+import org.squeryl._
+import org.squeryl.internals.DatabaseAdapter
+
+object SquerylTxMgr {
+  def initSqueryl(name: ConnectionIdentifier,
+    mkAdapter: () => DatabaseAdapter) = {
+    SessionFactory.externalTransactionManagementAdapter =
+    Some(new SquerylTxMgr(name, mkAdapter))
+  }
+}
+
+class SquerylTxMgr(name: ConnectionIdentifier, mkAdapter: () => DatabaseAdapter)
+  extends Loggable with Function0[Session] {
+
+  private object currentSession extends DynoVar[Session]
+
+
+  def apply = currentSession.is openOr {
+    DB.use(name) { superConn =>
+      val sess = Session.create(superConn.connection, mkAdapter())
+      sess.setLogger(s => logger.debug(s))
+      currentSession.set(sess)
+      sess
+    }
+  }
+}
+
 // vim: set ts=2 sw=2 et: