【发布时间】:2018-08-02 22:44:41
【问题描述】:
播放框架 2.6,Postgresql。 Jooq 作为数据库访问库。
运行测试时,我得到了
org.postgresql.util.PSQLException: FATAL: 抱歉,客户端太多 已经
这是一个帮助类,它提供了 jooq 的 dsl 上下文:
@Singleton
class Db @Inject() (val db: Database, system: ActorSystem) {
val databaseContext: ExecutionContext = system.dispatchers.lookup("contexts.database")
def query[A](block: DSLContext => A): Future[A] = Future {
db.withConnection { connection: Connection =>
val dsl = DSL.using(connection, SQLDialect.POSTGRES_9_4)
block(dsl)
}
}(databaseContext)
def withTransaction[A](block: DSLContext => A): Future[A] = Future {
db.withTransaction { connection: Connection =>
val dsl: DSLContext = DSL.using(connection, SQLDialect.POSTGRES_9_4)
block(dsl)
}
}(databaseContext)
}
我在这样的存储库中使用这个帮助类:
db.query { dsl =>
val records = dsl
.selectFrom(USERS)
.where(...)
...
}
}
application.conf
db.default.driver="org.postgresql.Driver"
db.default.url="jdbc:postgresql://localhost/postgres?user=postgres"
...
contexts {
database {
executor = "thread-pool-executor"
throughput = 1
thread-pool-executor {
fixed-pool-size = 9
}
}
}
...
build.sbt
...
libraryDependencies += jdbc
libraryDependencies += "org.jooq" % "jooq" % "3.10.5"
libraryDependencies += "org.jooq" % "jooq-codegen-maven" % "3.10.5"
libraryDependencies += "org.jooq" % "jooq-meta" % "3.10.5"
libraryDependencies += "org.postgresql" % "postgresql" % "42.1.4"
...
以及我所有测试的基本特征:
class BaseFeatureSpec extends FeatureSpec
with GivenWhenThen
with GuiceOneServerPerSuite
with Matchers
with WsScalaTestClient
with BeforeAndAfterEach
with MockitoSugar {
override def fakeApplication(): Application =
new GuiceApplicationBuilder()
.overrides(bind[EmailService].to(classOf[EmailServiceStub]))
.build()
def config: Configuration = fakeApplication().configuration
def actorSystem: ActorSystem = fakeApplication().actorSystem
val db: Db = app.injector.instanceOf[Db]
val wsClient: WSClient = app.injector.instanceOf[WSClient]
val myPublicAddress = s"localhost:$port"
private val injector = fakeApplication().injector
def truncateDbOnEachRun = true
override protected def beforeEach(): Unit = {
if (truncateDbOnEachRun) {
truncateDb
}
}
protected def truncateDb = {
await(db.withTransaction { dsl =>
... truncate all dbs...
})
}
}
我的 postgresql 实例的最大连接数是 100。
我注意到,在运行测试时,我发现池几乎在每次测试之前都被创建了多次:
[info] application - Creating Pool for datasource 'default'
[info] application - Creating Pool for datasource 'default'
[info] application - Creating Pool for datasource 'default'
[info] application - Creating Pool for datasource 'default'
在我收到 too many connections 错误之后。
请帮忙。
【问题讨论】:
-
您要测试应用的哪个部分?
-
我正在测试整个应用程序(与真实 Web 服务器的集成测试),每个测试都测试 http 端点,这些端点又将所有层调用到 db。为什么?
-
我在 jOOQ 方面看不到您的代码有任何问题,因此我认为问题与您如何配置该数据源有关...
标签: postgresql scala playframework jooq