使用Postgres和HikariCP设置Play 2.4.0会产生配置错误 [英] Setting up Play 2.4.0 with Postgres and HikariCP yields configuration error

查看:797
本文介绍了使用Postgres和HikariCP设置Play 2.4.0会产生配置错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个与本地postgres连接的播放框架服务器。

I'm trying to set up a play framework server with a connection to a local postgres.

我当前的 applications.conf 是这样的:

dbplugin=disabled
db {
  default {
    dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
    dataSource {
      user="postgres"
      password="postgres"
      databaseName="timeseries"
      serverName="localhost"
    }
    hikaricp {
      connectionTestQuery = "SELECT 1"
    }
  }
}

我的build.sbt只添加了最新的postgres jdbc:

My build.sbt has only the newest jdbc for postgres added:

lazy val root = (project in file(".")).enablePlugins(PlayJava)

scalaVersion := "2.11.6"

libraryDependencies ++= Seq(
  javaJdbc
  , cache
  , javaWs
  , "org.postgresql" % "postgresql" % "9.4.1207.jre7"
)

我收到的错误是

[error] - application - 
[info] 
[info] ! @6ookeg34l - Internal server error, for (GET) [/] ->
[info]  
[info] play.api.UnexpectedException: Unexpected exception[CreationException: Unable to create injector, see the following errors:
[info] 
[info] 1) Error in custom provider, Configuration error: Configuration error[Cannot connect to database [default]]
[info]   while locating play.api.db.DBApiProvider
[info]   while locating play.api.db.DBApi
[info]     for parameter 0 at play.db.DefaultDBApi.<init>(DefaultDBApi.java:28)
[info]   at play.db.DefaultDBApi.class(DefaultDBApi.java:28)
[info]   while locating play.db.DefaultDBApi
[info]   while locating play.db.DBApi
[info]     for field at play.db.DBModule$NamedDatabaseProvider.dbApi(DBModule.java:61)
[info]   while locating play.db.DBModule$NamedDatabaseProvider
[info]   at com.google.inject.util.Providers$GuicifiedProviderWithDependencies.initialize(Providers.java:149)
[info]   at play.db.DBModule.bindings(DBModule.java:40):
[info] Binding(interface play.db.Database qualified with QualifierInstance(@play.db.NamedDatabase(value=default)) to ProviderTarget(play.db.DBModule$NamedDatabaseProvider@3782a5cb)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
[info] Caused by: Configuration error: Configuration error[Cannot connect to database [default]]

我发现,我的游戏2.4不需要hikari作为明确的依赖。附加密码和用户名都是正确的以及databsename。

I have found out, that my play 2.4 does not require hikari as an explicit dependecy. Additonally passwords and username are correct aswell as databsename.

我实际上并不知道除了框架的网站之外还要查找更多信息,我已经广泛检查过。多人似乎也有类似的问题,虽然他们的解决方案对我没有帮助,或者只是我目前的一步。

I don't actually know where to look for more info besides the frameworks' websites and I've checked both extensively. Multiple people seem to have had similar problems, though their solutions did not help me or were just a step in my path this far.

推荐答案

有两个地方可以确切地看到如何配置连接池:

There are two places were you can see exactly how to configure your connection pool:


  1. 播放文档:SettingsJDBC

  2. play-jdbc reference.conf 文件

  1. Play docs: SettingsJDBC
  2. play-jdbc reference.conf file

从那里,您可以看到您的游泳池必须配置为:

From there, you will can see that your pool must be configured like:

db {
  default {
    driver=org.postgresql.Driver
    url="jdbc:postgresql://localhost/timeseries"
    user=postgres
    password=postgres

    hikaricp {
      dataSourceClassName = org.postgresql.ds.PGSimpleDataSource
      connectionTestQuery = "SELECT 1"
      # Data source configuration options. Must be INSIDE
      # the hikaricp "node" here
      dataSource {
        # anything you need to configure here
        ...
      }
    }
  }
}

注意配置节点是如何嵌套的: db - > 默认 - > hikaricp - > dataSource 。这是因为 dataSource 是特定于HikariCP的配置。正如您在 reference.conf 文件中所看到的,BoneCP不提供此配置节点。

Notice how the configuration nodes are nested: db -> default -> hikaricp -> dataSource. That is because dataSource is a configuration specific to HikariCP. As you can see at the reference.conf file, BoneCP does not offer this configuration node.

此外,< a href =https://github.com/typesafehub/config =nofollow> Typesafe配置库支持上述配置,或者更加明白地编写如下:

Also, Typesafe Configuration library supports both this the configuration above or writing more "plainly" like below:

db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost/timeseries"
db.default.user=postgres
db.default.password=postgres
db.default.hikaricp.dataSourceClassName = org.postgresql.ds.PGSimpleDataSource
db.default.hikaricp.connectionTestQuery = "SELECT 1"

这篇关于使用Postgres和HikariCP设置Play 2.4.0会产生配置错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆