# HG changeset patch # User Tomas Zeman # Date 1328863988 -3600 # Node ID 5a2f996a5ba00f1387534178bd49bb50608c46a2 # Parent 29d4ddcd85c8b3fac0e49e69cb3e19f688e51522 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 diff -r 29d4ddcd85c8 -r 5a2f996a5ba0 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: