在Play 2.6的conf文件中Hikari,Quill和Postgres之间的冲突
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Play 2.6的conf文件中Hikari,Quill和Postgres之间的冲突相关的知识,希望对你有一定的参考价值。
当我运行我的Play应用程序(这似乎是我现在的生活故事)时,我得到的似乎是一个奇怪的错误。前几天,我遇到了this issue,解决了这个问题,能够运行演变并创建表格,然后遇到以下问题:
java.lang.RuntimeException: Property driver does not exist on target class com.zaxxer.hikari.HikariConfig
这是我当前application.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
}
我做了some digging,发现Hikari期待driverClassName
(以及其他一些差异)。 Hikari docs和Quill docs说这样的东西应该在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 /包
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驱动程序42.2.1
- PostgreSQL 10.2
我把Quill / Hikari期望的错误信息:
! @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
类没有实现的。我遇到了类似的问题,并没有找到更优雅的方法来做到这一点。
以上是关于在Play 2.6的conf文件中Hikari,Quill和Postgres之间的冲突的主要内容,如果未能解决你的问题,请参考以下文章
Java Play Framework 2.6 不返回“Access-Control-Allow-Origin”CORS 标头
在 Play 框架 Java 2.6 中添加 Paypal 按钮
如何在 Play 2.6 控制器中使“CustomExecutionContext”可用于依赖注入?
将 Play 框架中的 WebSockets 从 2.4 版转换为 2.6 版