jilen
jilen

Reputation: 5763

playframework connection closed while using both slick and anorm

I have a project use both slick and anorm.

I define a method for slick 

  object DBCache {
    def apply(app: play.api.Application) = Cache.getOrElse[Database](app.hashCode.toString){
      Database.forDataSource(PlayDB.getDataSource("default")(app))
    }
  }

  private[persist] def inSession[T](block: Session => T) = DBCache(current).withSession(block(_))

And when I can a batch insert method use anorm

def batchInsert(customerAccounts: Seq[Customer]) = DB.withConnection { implicit conn =>
      val sql = SQL(insertSql)
      val batch = customerAccounts.foldLeft(sql.asBatch) {
        (sql, c) => sql.addbatch(xxx)
      }
}

It reports

play.api.Application$$anon$1: Execution exception[[MySQLNonTransientConnectionException: No operations allowed after connection closed.]

How to avoid this error

Upvotes: 2

Views: 630

Answers (1)

jilen
jilen

Reputation: 5763

I found the master branch of playframework disable the tracking , datasource.setDisableConnectionTracking( conf.getBoolean("disableConnectionTracking").getOrElse(true))

So I disable it in Global.scala.

There'are still some problem. When sql error happends, connection may be closed. Then I turned to use alibaba's data source https://github.com/alibaba/druid. It works fine!

Upvotes: 1

Related Questions