当我尝试运行Play应用程序(Play 2.5.4)时,出现以下错误:
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
这是我的数据库设置,有什么想法吗?我有两次定义,因为我通过Slick和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
}
更新
build.sbt文件:
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/"
答案 0 :(得分:0)
在我的情况下,application.conf没有适当的数据库参数
示例
db.default.driver = org.h2.Driver
db.default.url = "jdbc:h2:mem:play"
db.default.username = sa
db.default.password = ""
答案 1 :(得分:0)
您的application.conf应该如下所示:
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
}
}
开始播放Play时,它通过从application.conf中的db。dev_mysql
json加载参数来创建名为dev_mysql
的数据库连接池。
如何执行查询:
我们可以使用NamedDatabase注释。
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
}