【问题标题】:Setting up Play 2.4.0 with Postgres and HikariCP yields configuration error使用 Postgres 和 HikariCP 设置 Play 2.4.0 会产生配置错误
【发布时间】:2016-01-21 13:53:45
【问题描述】:

我正在尝试设置一个连接到本地 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:

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]]

我发现,我的 play 2.4 不需要 hikari 作为明确的依赖。另外密码和用户名以及数据库名称都是正确的。

除了框架的网站之外,我实际上不知道在哪里可以找到更多信息,我已经广泛检查了这两个信息。很多人似乎都遇到过类似的问题,尽管他们的解决方案对我没有帮助,或者只是我到目前为止的一步。

【问题讨论】:

    标签: java postgresql playframework datasource hikaricp


    【解决方案1】:

    您可以在两个地方准确了解如何配置连接池:

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

    从那里,您将看到您的池必须配置为:

    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 -> default -> hikaricp -> dataSource。那是因为dataSource 是一个特定于 HikariCP 的配置。正如您在reference.conf 文件中看到的,BoneCP 不提供此配置节点。

    另外,Typesafe Configuration library 既支持上面的配置,也支持下面的更“简单”的写法:

    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"
    

    【讨论】:

    • 非常感谢!我真的很感激它(即使我已经找到了答案)。我花了很长时间才掌握了所有的游戏资源。 IRC 中的人自己并没有那么乐于助人,只是被动攻击。所以我真的很感谢你!
    【解决方案2】:

    也试试:

    db.default.hikaricp.dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
    db.default.hikaricp.dataSource.user=username
    db.default.hikaricp.dataSource.password=password
    db.default.hikaricp.dataSource.databaseName=mydb
    db.default.hikaricp.dataSource.serverName=localhost
    

    【讨论】:

      【解决方案3】:

      找到答案:

      你需要使用这种格式:

      dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
      dataSource.user=postgres
      dataSource.password=sdfsdfasd
      dataSource.databaseName=timeseries
      dataSource.serverName=localhost
      hikaricp .connectionTestQuery = "SELECT 1"
      

      【讨论】:

        猜你喜欢
        • 2016-03-21
        • 2016-04-13
        • 2021-08-07
        • 1970-01-01
        • 2016-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-02
        相关资源
        最近更新 更多