使用光滑和anorm时,playframework连接关闭

时间:2014-01-02 07:19:55

标签: scala playframework slick anorm

我有一个项目使用光滑和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(_))

当我可以批量插入方法时使用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)
      }
}

报告

  

play.api.Application $$ anon $ 1:执行异常[[MySQLNonTransientConnectionException:连接关闭后不允许任何操作。]

如何避免此错误

1 个答案:

答案 0 :(得分:1)

我发现playframework的主分支禁用跟踪, datasource.setDisableConnectionTracking( conf.getBoolean("disableConnectionTracking").getOrElse(true))

所以我在Global.scala中禁用它。

还有一些问题。当sql错误发生时,可能会关闭连接。然后我转而使用阿里巴巴的数据源https://github.com/alibaba/druid。它工作正常!