问题
When I attempt to run the Play application (Play 2.5.4) I get the following error:
ProvisionException: Unable to provision, see the following errors:
1) No implementation for play.api.db.Database was bound.
while locating play.api.db.Database
for parameter 0 at ds.qb.manage.ManageQueryBuilder.<init>(ManageQueryBuilder.scala:30)
while locating ds.qb.manage.ManageQueryBuilder
for parameter 16 at router.Routes.<init>(Routes.scala:107)
while locating router.Routes
while locating play.api.inject.RoutesProvider
while locating play.api.routing.Router
for parameter 0 at play.api.http.JavaCompatibleHttpRequestHandler.<init>(HttpRequestHandler.scala:200)
while locating play.api.http.JavaCompatibleHttpRequestHandler
while locating play.api.http.HttpRequestHandler
for parameter 4 at play.api.DefaultApplication.<init>(Application.scala:221)
at play.api.DefaultApplication.class(Application.scala:221)
while locating play.api.DefaultApplication
while locating play.api.Application
This is my database setup, any ideas? I have the definition twice because I access the database both through Slick and JDBC.
play.db {
# The combination of these two settings results in "db.default" as the
# default JDBC pool:
config = "db"
default = "default"
# Play uses HikariCP as the default connection pool. You can override
# settings by changing the prototype:
#prototype {
# Sets a fixed JDBC connection pool size of 50
#hikaricp.minimumIdle = 50
#hikaricp.maximumPoolSize = 50
#}
}
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost:3306/db2"
db.default.username=root
db.default.password=xxxxx
db2 = {
url = "jdbc:mysql://localhost:3306/db2"
driver = com.mysql.jdbc.Driver
connectionPool = disabled
keepAliveConnection = true
user=root
password=xxxxxx
}
UPDATE
The build.sbt file:
name := """myapp"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
jdbc,
cache,
ws,
"org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test
)
libraryDependencies += "com.typesafe.slick" %% "slick" % "3.1.1"
libraryDependencies += "com.typesafe.play" %% "play-slick" % "2.0.0"
libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.18"
libraryDependencies += "org.pivot4j" % "pivot4j-core" % "0.9"
libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.1.0"
// properties file
libraryDependencies += "com.typesafe" % "config" % "1.3.0"
libraryDependencies += "org.slf4j" % "slf4j-nop" % "1.6.4"
libraryDependencies += "log4j" % "log4j" % "1.2.14"
resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"
// mondrian
resolvers += "Pentaho Releases" at "http://repository.pentaho.org/artifactory/repo/"
回答1:
In my case application.conf didn't have proper db parameters
example
db.default.driver = org.h2.Driver
db.default.url = "jdbc:h2:mem:play"
db.default.username = sa
db.default.password = ""
回答2:
Your application.conf should look like below:
db.dev_mysql {
driver = "org.mariadb.jdbc.Driver"
# JDBC connection string. "useAffectedRows" must be set to true.
url = "jdbc:mysql://dev01:3306/dev_db?autoReconnect=true&characterEncoding=utf-8&connectionCollation=utf8_unicode_ci&useSSL=false&useAffectedRows=true"
url = ${?DATABASE_URL}
hikaricp {
# Whether autocommit should be used
autoCommit = true
# The connection timeout
connectionTimeout = 10 seconds
# The idle timeout
idleTimeout = 5 minutes
# The max lifetime of a connection
maxLifetime = 10 minutes
# If non null, the query that should be used to test connections
connectionTestQuery = "SELECT 1"
# Minimum number of idle connections maintained in the pool.
minimumIdle = 10
# The maximum number of connections to make.
maximumPoolSize = 20
# If non null, sets the name of the connection pool. Primarily used for stats reporting.
poolName = "mysql"
# A SQL statement that will be executed after every new connection creation before adding it to the pool
connectionInitSql = "SELECT 1"
# If non null, sets the transaction isolation level
transactionIsolation = TRANSACTION_READ_COMMITTED
# The validation timeout to use
validationTimeout = 5 seconds
}
}
When Play starts, it creates a database connection-pool with the name of dev_mysql
by loading the params from db.dev_mysql
json in application.conf
How to execute queries :
We can use a NamedDatabase annotation.
class CustomController @Inject() (
val config: Configuration,
@play.db.NamedDatabase("dev_mysql") db : Database,
controllerComponents: ControllerComponents,
) extends ApiController(controllerComponents) {
def createReportDefinition = Action { implicit ctx ⇒
val conn = db.getConnection(false)
val sqlQuery = "select 1"
val prepStmt: PreparedStatement = conn.prepareStatement(sqlQuery)
prepStmt.execute()
conn.commit()
conn.close()
Ok
}
来源:https://stackoverflow.com/questions/38825728/play-error-on-startup-no-implementation-for-play-api-db-database-was-bound