【问题标题】:How to get past SQLException: Attempting to obtain a connection from a pool that has already been shutdown如何通过 SQLException:尝试从已关闭的池中获取连接
【发布时间】:2013-10-17 11:37:35
【问题描述】:

我有一个使用 Typesafe Slick (v 1.0.1) 的 Play (v 2.2.0) 应用程序,我正在尝试编写一个测试 (specs2) 来播种 PostgreSQL 数据库,然后调用各种控制器动作来验证数据的存在。在我的测试中,我有:

 "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.

stacktrace 的相关部分是:

[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 包装器中,但仍然没有运气。

感谢您的任何指导。

【问题讨论】:

    标签: scala playframework-2.0 slick specs2


    【解决方案1】:

    您可以使用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)
        }
      }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 2014-07-07
    相关资源
    最近更新 更多