scala - 如何通过 SQLException : Attempting to obtain a connection from a pool that has already been shutdown

标签 scala playframework-2.0 slick specs2

我有一个使用 Typesafe Slick (v 1.0.1) 的 Play (v 2.2.0) 应用程序,我正在尝试编写一个测试 (specs2) 来为 PostgreSQL 数据库提供种子,然后调用各种 Controller 操作以验证数据的存在。在我的测试中,我有:

 "Countries" should {
      "initialize" in {
        running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
          AppDB.database.withSession {
            implicit session: Session =>

              AppDB.dal.create
              AppDB.dal.seedForTests

              AppDB.dal.Countries.findAll().size must be_>=(1)
          }
        }
      }

就其本身而言,这很好用。但是,当我添加另一个测试操作时,例如:
  "respond to Index()" in {
    val result = controllers.Countries.index()(FakeRequest())

    status(result) must equalTo(OK)
  }

我的测试失败并显示以下消息:
SQLException: Attempting to obtain a connection from a pool that has already been shutdown.

堆栈跟踪的相关部分是:
[error]     SQLException: Attempting to obtain a connection from a pool that has already been shutdown. 
[error] Stack trace of location where pool was shutdown follows:
[error]  java.lang.Thread.getStackTrace(Thread.java:1503)
[error]  com.jolbox.bonecp.BoneCP.captureStackTrace(BoneCP.java:559)
[error]  com.jolbox.bonecp.BoneCP.shutdown(BoneCP.java:161)
[error]  com.jolbox.bonecp.BoneCPDataSource.close(BoneCPDataSource.java:143)
[error]  play.api.db.BoneCPApi.shutdownPool(DB.scala:414)
[error]  play.api.db.BoneCPPlugin$$anonfun$onStop$1.apply(DB.scala:264)
[error]  play.api.db.BoneCPPlugin$$anonfun$onStop$1.apply(DB.scala:262)
[error]  scala.collection.immutable.List.foreach(List.scala:318)
[error]  play.api.db.BoneCPPlugin.onStop(DB.scala:262)
...

我试过移动 FakeApplication(...)AppDB.database.withSession代码中更高的块,以及包装 val result = controllers.Countries.index(...) AppDB.database.withSession 中的代码包装,但仍然没有运气。

谢谢你的任何方向。

最佳答案

您可以使用 AroundExample初始化您的数据库并运行您的测试:

class CountriesSpec extends mutable.Specification with AroundExample {

  def around[R : AsResult](r: =>R) = 
    running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
      AppDB.database.withSession { implicit session: Session =>
        AppDB.dal.create
        AppDB.dal.seedForTests
        AppDB.dal.Countries.findAll().size must be_>=(1)
        // just AsResult(r) with the latest 2.2.3 specs2 
        AsResult.effectively(r)
      }
    }

  "Countries" should {
    "respond to Index()" in {
      val result = controllers.Countries.index()(FakeRequest())
      status(result) must equalTo(OK)
    }
  }
}

关于scala - 如何通过 SQLException : Attempting to obtain a connection from a pool that has already been shutdown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19283421/

相关文章:

java - 获取 JFrame 在平铺窗口管理器上显示 float

Scala 类型参数边界

mysql - "Hello World"使用 MySQL 的 Slick 2.0 示例

scala - Play 框架模板没有 Html 类型

java - postgresql - 在 java 项目中定义串行数据类型

Scala Slick 表继承

scala - 更改 fs2.Stream 的效果类型

scala - 插入具有自动增量 id 的行 - Playframework Scala - Slick

postgresql - Macaddr/Inet 类型的 postgres in slick

java - 将 &lt;script&gt; 添加到 <head> from Play Framework 2 中的 scala 模板标签