问题
I'm trying to set up a play framework server with a connection to a local postgres.
My current applications.conf
is like this:
dbplugin=disabled
db {
default {
dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
dataSource {
user="postgres"
password="postgres"
databaseName="timeseries"
serverName="localhost"
}
hikaricp {
connectionTestQuery = "SELECT 1"
}
}
}
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"
)
The error I receive is
[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]]
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.
回答1:
There are two places were you can see exactly how to configure your connection pool:
- Play docs: SettingsJDBC
- 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
...
}
}
}
}
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.
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"
回答2:
Also try:
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:
Found the answer:
You need to use this format:
dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
dataSource.user=postgres
dataSource.password=sdfsdfasd
dataSource.databaseName=timeseries
dataSource.serverName=localhost
hikaricp .connectionTestQuery = "SELECT 1"
来源:https://stackoverflow.com/questions/34925700/setting-up-play-2-4-0-with-postgres-and-hikaricp-yields-configuration-error