Hikari、Quill 和 Postgres 在 Play 2.6 的 conf 文件中的冲突



当我运行我的Play应用程序时,我遇到了一个奇怪的错误(这似乎是我现在的生活故事)。前几天,我遇到了这个问题,解决了这个问题,能够运行演变并创建表,然后遇到以下内容:

java.lang.RuntimeException: Property driver does not exist on target class com.zaxxer.hikari.HikariConfig

这是我当前应用程序.conf的相关部分:

db.default {
dataSourceClassName = org.postgresql.ds.PGSimpleDataSource
url = "jdbc:postgresql://localhost:5432/app-users?user=root&password=root"
driver = org.postgresql.Driver
username = root
password = root
databaseName = app-users
portNumber = 5432
serverName = localhost
connectionTimeout = 30000
}

我做了一些挖掘,发现 Hikari 期望driverClassName(以及其他一些差异)。Hikari文档和Quill文档说,类似的东西应该在application.conf中:

dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
dataSource.user=root
dataSource.password=root
dataSource.databaseName=app-users
dataSource.portNumber=5432
dataSource.serverName=localhost

但是,当我拥有它时,进化就不会运行。它们只会在我像现在这样拥有它时运行。由于进化正在运行,因此它似乎正在连接到数据库。那么为什么我会收到一条消息说它不能呢?有没有办法让我为 Hikari 和 Postgres/Quill 设置单独的数据库配置?

完整的错误消息:

play.api.UnexpectedException: Unexpected exception[IllegalStateException: Failed to load data source for config: 'Config(SimpleConfigObject({"connectionTimeout":30000,"dataSourceClassName":"org.postgresql.ds.PGSimpleDataSource","databaseName":"mack-users","driver":"org.postgresql.Driver","password":"root","portNumber":5432,"serverName":"localhost","url":"jdbc:postgresql://localhost:5432/mack-users?user=root&password=root","username":"root"}))']
at play.core.server.DevServerStart$$anon$1.reload(DevServerStart.scala:186)
at play.core.server.DevServerStart$$anon$1.get(DevServerStart.scala:124)
at play.core.server.AkkaHttpServer.modelConversion(AkkaHttpServer.scala:183)
at play.core.server.AkkaHttpServer.handleRequest(AkkaHttpServer.scala:189)
at play.core.server.AkkaHttpServer.$anonfun$createServerBinding$3(AkkaHttpServer.scala:106)
at akka.stream.impl.fusing.MapAsync$$anon$24.onPush(Ops.scala:1191)
at akka.stream.impl.fusing.GraphInterpreter.processPush(GraphInterpreter.scala:512)
at akka.stream.impl.fusing.GraphInterpreter.processEvent(GraphInterpreter.scala:475)
at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:371)
at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:584)
Caused by: java.lang.IllegalStateException: Failed to load data source for config: 'Config(SimpleConfigObject({"connectionTimeout":30000,"dataSourceClassName":"org.postgresql.ds.PGSimpleDataSource","databaseName":"mack-users","driver":"org.postgresql.Driver","password":"root","portNumber":5432,"serverName":"localhost","url":"jdbc:postgresql://localhost:5432/mack-users?user=root&password=root","username":"root"}))'
at io.getquill.JdbcContextConfig.dataSource(JdbcContextConfig.scala:24)
at io.getquill.PostgresJdbcContext.<init>(PostgresJdbcContext.scala:17)
at io.getquill.PostgresJdbcContext.<init>(PostgresJdbcContext.scala:18)
at io.getquill.PostgresJdbcContext.<init>(PostgresJdbcContext.scala:19)
at db.db.package$DBContext.<init>(package.scala:6)
at MyComponents.ctx$lzycompute(MyApplicationLoader.scala:25)
at MyComponents.ctx(MyApplicationLoader.scala:25)
at MyComponents.userService$lzycompute(MyApplicationLoader.scala:28)
at MyComponents.userService(MyApplicationLoader.scala:28)
at MyComponents.applicationController$lzycompute(MyApplicationLoader.scala:35)
Caused by: java.lang.RuntimeException: Property driver does not exist on target class com.zaxxer.hikari.HikariConfig
at com.zaxxer.hikari.util.PropertyElf.setProperty(PropertyElf.java:131)
at com.zaxxer.hikari.util.PropertyElf.lambda$setTargetFromProperties$0(PropertyElf.java:57)
at java.util.Hashtable.forEach(Hashtable.java:879)
at com.zaxxer.hikari.util.PropertyElf.setTargetFromProperties(PropertyElf.java:52)
at com.zaxxer.hikari.HikariConfig.<init>(HikariConfig.java:132)
at io.getquill.JdbcContextConfig.dataSource(JdbcContextConfig.scala:21)
at io.getquill.PostgresJdbcContext.<init>(PostgresJdbcContext.scala:17)
at io.getquill.PostgresJdbcContext.<init>(PostgresJdbcContext.scala:18)
at io.getquill.PostgresJdbcContext.<init>(PostgresJdbcContext.scala:19)
at db.db.package$DBContext.<init>(package.scala:6)

db/package.scala

import io.getquill.{PostgresJdbcContext, SnakeCase}
package object db {
class DBContext(config: String) extends PostgresJdbcContext(SnakeCase, config)
trait Repository {
val ctx: DBContext
}
}

关于可能导致这种情况的任何想法?或者我错过了什么?

用:

  • 斯卡拉 2.12.4
  • 羽毛笔 2.3.2
  • 玩 2.6.6
  • Postgres JDBC Driver 42.2.1
  • PostgreSQL 10.2

当我输入奎尔/光期望时出现错误消息:

! @776d39n6c - Internal server error, for (GET) [/] ->
play.api.Configuration$$anon$1: Configuration error[Cannot connect to database [default]]
at play.api.Configuration$.configError(Configuration.scala:156)
at play.api.Configuration.reportError(Configuration.scala:990)
at play.api.db.DefaultDBApi.$anonfun$connect$1(DefaultDBApi.scala:48)
at play.api.db.DefaultDBApi.$anonfun$connect$1$adapted(DefaultDBApi.scala:42)
at scala.collection.immutable.List.foreach(List.scala:389)
at play.api.db.DefaultDBApi.connect(DefaultDBApi.scala:42)
at play.api.db.DBApiProvider.get$lzycompute(DBModule.scala:86)
at play.api.db.DBApiProvider.get(DBModule.scala:75)
at play.api.db.DBComponents.dbApi(DBModule.scala:49)
at play.api.db.DBComponents.dbApi$(DBModule.scala:49)
Caused by: play.api.Configuration$$anon$1: Configuration error[dataSource or dataSourceClassName or jdbcUrl is required.]
at play.api.Configuration$.configError(Configuration.scala:156)
at play.api.Configuration.reportError(Configuration.scala:990)
at play.api.db.HikariCPConnectionPool.create(HikariCPModule.scala:63)
at play.api.db.PooledDatabase.createDataSource(Databases.scala:199)
at play.api.db.DefaultDatabase.dataSource$lzycompute(Databases.scala:123)
at play.api.db.DefaultDatabase.dataSource(Databases.scala:121)
at play.api.db.DefaultDatabase.getConnection(Databases.scala:142)
at play.api.db.DefaultDatabase.getConnection(Databases.scala:138)
at play.api.db.DefaultDBApi.$anonfun$connect$1(DefaultDBApi.scala:44)
at play.api.db.DefaultDBApi.$anonfun$connect$1$adapted(DefaultDBApi.scala:42)
Caused by: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:997)
at play.api.db.HikariCPConfig.toHikariConfig(HikariCPModule.scala:136)
at play.api.db.HikariCPConnectionPool.$anonfun$create$1(HikariCPModule.scala:50)
at scala.util.Try$.apply(Try.scala:209)
at play.api.db.HikariCPConnectionPool.create(HikariCPModule.scala:47)
at play.api.db.PooledDatabase.createDataSource(Databases.scala:199)
at play.api.db.DefaultDatabase.dataSource$lzycompute(Databases.scala:123)
at play.api.db.DefaultDatabase.dataSource(Databases.scala:121)
at play.api.db.DefaultDatabase.getConnection(Databases.scala:142)
at play.api.db.DefaultDatabase.getConnection(Databases.scala:138)

不幸的是,Hikari和Play没有使用完全相同的命名约定来设置他们的数据源,例如Quill需要一个裸dataSourceClassName但Play希望它采用db.default.dataSourceClassName这样的格式。 据我所知,没有办法重新设计您的配置,以便它同时满足它们,您可能不想这样做,原因如下:

Play实际上使用Hikari作为池化数据源,因此当您通过db.default创建数据库时,您实际上已经在创建池数据源。 当您使用裸配置调用PostgresJdbcContext时,您实际上是在要求 Quill 创建一个完全独立的连接池。不确定这是否是您想要的,但我假设您只需要一个连接池。

Quill实际上公开了一个接受DataSource的构造函数,所以你可以做的是使用Play创建的Database并将其直接传递给Quill:

import io.getquill.{PostgresJdbcContext, SnakeCase}
import com.zaxxer.hikari.HikariDataSource
import play.api.db.Database
package object db {
class DBContext(db: Database) extends PostgresJdbcContext(SnakeCase, db.dataSource.asInstanceOf[HikariDataSource])
}

这应该可以解决您的问题,并且只会创建一个连接池。您可以通过依赖注入获取Database对象。

你必须在这里做一个丑陋的asInstanceOf,因为PostgresJdbcContext需要dataSource来实现Closeable,而裸DataSource类没有实现。 我遇到了类似的问题,但没有找到更优雅的方法。

相关内容

  • 没有找到相关文章

最新更新