title: Problem with Squeryl and Play! framework in scala
body: <p>I have a curious issue when using Squeryl with Play!. </p>
  
  <p>Normal usage and everything else works completely fine. However, if I use more than one transaction in the same request, I get an error.</p>
  
  <p>That's how I set up Squeryl:</p>
  
  <pre><code>def initDB() {
    import org.squeryl._
    import play.db.DB
  
    Class.forName("com.mysql.jdbc.Driver")
    SessionFactory.concreteFactory = Some(() =&gt;
      Session.create( DB.getConnection, new MySQLAdapter) )
  }
  </code></pre>
  
  <p>Sample transaction, also the one referenced in the stack trace below:</p>
  
  <pre><code>transaction {
    import models.Game
    Game.planets.insert(planetList) 
    Game.moons.insert(moonList)
  }
  </code></pre>
  
  <p>Stack trace:</p>
  
  <pre><code>Internal Server Error (500) for request GET /generate-galaxy
  
  Execution exception (In /app/Generator.scala around line 330)
  SQLException occured : You can't operate on a closed Connection!!!
  
  play.exceptions.JavaExecutionException: You can't operate on a closed Connection!!!
      at play.mvc.ActionInvoker.invoke(ActionInvoker.java:228)
      at Invocation.HTTP Request(Play!)
  Caused by: java.sql.SQLException: You can't operate on a closed Connection!!!
      at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
      at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:65)
      at org.squeryl.dsl.QueryDsl$class._executeTransactionWithin(QueryDsl.scala:95)
      at org.squeryl.dsl.QueryDsl$class.transaction(QueryDsl.scala:64)
      at org.squeryl.PrimitiveTypeMode$.transaction(PrimitiveTypeMode.scala:40)
      at generator.Generator$$anonfun$generatePlanets$2.apply(Generator.scala:330)
      at generator.Generator$$anonfun$generatePlanets$2.apply(Generator.scala:55)
      at generator.Generator$.generatePlanets(Generator.scala:55)
      at generator.Generator$.generateGalaxy(Generator.scala:36)
      at controllers.MainRouter$.generateGalaxy(MainRouter.scala:29)
      at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:543)
      at play.mvc.ActionInvoker.invoke(ActionInvoker.java:499)
      at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:493)
      at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:470)
      at play.mvc.ActionInvoker.invoke(ActionInvoker.java:158)
      ... 1 more
  Caused by: java.lang.NullPointerException
      ... 14 more
  </code></pre>
  
  <p>I know that the problem is not in my queries, because they worked fine when using scalatra as a web framework. I <em>could</em> just put everything into one transaction block, but that's not really elegant and I'm also not sure if it would work in this case - the <code>planetList</code> list has about 3 million members, which caused scala to run out of memory before I split database insertion up into smaller blocks of 50k elements.</p>