引起:java.sql.SQLException:不支持JDBC4 Connection.isValid()方法

时间:2015-07-05 07:24:12

标签: scala heroku heroku-postgres playframework-2.4 slick-3.0

我的play-framework项目在我的本地运行良好但是当我尝试在heroku中部署它时,我遇到了以下错误。

2015-07-05T06:24:10.456657+00:00 app[web.1]:    at com.google.inject.Guice.createInjector(Guice.java:73)
2015-07-05T06:24:10.456817+00:00 app[web.1]:    at play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:93)
2015-07-05T06:24:10.456702+00:00 app[web.1]:    at com.google.inject.Guice.createInjector(Guice.java:62)
2015-07-05T06:24:10.456746+00:00 app[web.1]:    at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:126)
2015-07-05T06:24:10.456863+00:00 app[web.1]:    at play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21)
2015-07-05T06:24:10.456909+00:00 app[web.1]:    at play.core.server.ProdServerStart$.start(ProdServerStart.scala:52)
2015-07-05T06:24:10.456951+00:00 app[web.1]:    at play.core.server.ProdServerStart$.main(ProdServerStart.scala:27)
2015-07-05T06:24:10.456994+00:00 app[web.1]:    at play.core.server.ProdServerStart.main(ProdServerStart.scala)
2015-07-05T06:24:10.457407+00:00 app[web.1]:    at com.zaxxer.hikari.pool.BaseHikariPool.addConnection(BaseHikariPool.java:441)
2015-07-05T06:24:10.457496+00:00 app[web.1]:    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
2015-07-05T06:24:10.457369+00:00 app[web.1]: Caused by: java.sql.SQLException: JDBC4 Connection.isValid() method not supported, connection test query must be configured
2015-07-05T06:24:10.457602+00:00 app[web.1]:    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2015-07-05T06:24:10.457451+00:00 app[web.1]:    at com.zaxxer.hikari.pool.BaseHikariPool$1.run(BaseHikariPool.java:413)
2015-07-05T06:24:10.457681+00:00 app[web.1]:    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
2015-07-05T06:24:10.457812+00:00 app[web.1]:    at java.lang.Thread.run(Thread.java:745)
2015-07-05T06:24:10.457768+00:00 app[web.1]:    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
2015-07-05T06:24:11.231007+00:00 heroku[web.1]: Process exited with status 255
2015-07-05T06:24:11.243795+00:00 heroku[web.1]: State changed from starting to crashed
2015-07-05T06:24:11.245013+00:00 heroku[web.1]: State changed from crashed to starting
2015-07-05T06:24:16.259302+00:00 heroku[web.1]: Starting process with command `target/universal/stage/bin/sazors -Dhttp.port=10803`
2015-07-05T06:24:17.542815+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx384m -Xss512k -Dfile.encoding=UTF-8
2015-07-05T06:24:18.643391+00:00 app[web.1]: 06:24:18,391 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
2015-07-05T06:24:18.643397+00:00 app[web.1]: 06:24:18,391 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]

我使用PostgreSQL作为我的db和slick作为数据库访问库。我尝试谷歌它,并经过一些研究后发现,当配置文件中提供的url不正确或返回null时,可能会导致此错误。我试图解决它,但我无法解决此错误。

这是我的Procfile:

  

web:target / universal / stage / bin / myapp -Dhttp.port = $ {PORT} -Dconfig.resource = $ {PLAY_CONF_FILE}

和Application.conf

slick.dbs.default.driver="slick.driver.PostgresDriver$"
slick.dbs.default.db.driver="org.postgresql.Driver"
slick.dbs.default.db.url="postgres://rycmvicwkkzofh:Xt2PWggm__FQEHOeq9EoMqTOtF@ec2-54-243-132-114.compute-1.amazonaws.com:5432/d5pr4h65obtb0s"
slick.dbs.default.db.user=rycmuicwkkzofh
slick.dbs.default.db.password="copiedfromheroku"

P.S:我正在研究这些修订

play framework version : 2.4.0 slick version : 3.0

编辑:如果我在application.conf中添加slick.dbs.default.db.connectionTestQuery="SELECT 1",则会产生新类型的nullPointerException错误。

That is gone but getting new error now:
2015-07-05T12:22:49.781133+00:00 app[web.1]:    at com.google.inject.Guice.createInjector(Guice.java:73)
2015-07-05T12:22:49.781201+00:00 app[web.1]:    at com.google.inject.Guice.createInjector(Guice.java:62)
2015-07-05T12:22:49.781283+00:00 app[web.1]:    at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:126)
2015-07-05T12:22:49.781361+00:00 app[web.1]:    at play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:93)
2015-07-05T12:22:49.781507+00:00 app[web.1]:    at play.core.server.ProdServerStart$.start(ProdServerStart.scala:52)
2015-07-05T12:22:49.781425+00:00 app[web.1]:    at play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21)
2015-07-05T12:22:49.781599+00:00 app[web.1]:    at play.core.server.ProdServerStart$.main(ProdServerStart.scala:27)
2015-07-05T12:22:49.781669+00:00 app[web.1]:    at play.core.server.ProdServerStart.main(ProdServerStart.scala)
2015-07-05T12:22:49.782161+00:00 app[web.1]: Caused by: java.lang.NullPointerException
2015-07-05T12:22:49.782226+00:00 app[web.1]:    at com.zaxxer.hikari.pool.BaseHikariPool.addConnection(BaseHikariPool.java:446)
2015-07-05T12:22:49.782328+00:00 app[web.1]:    at com.zaxxer.hikari.pool.BaseHikariPool$1.run(BaseHikariPool.java:413)
2015-07-05T12:22:49.782417+00:00 app[web.1]:    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
2015-07-05T12:22:49.782515+00:00 app[web.1]:    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2015-07-05T12:22:49.783691+00:00 app[web.1]:    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
2015-07-05T12:22:49.783803+00:00 app[web.1]:    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
2015-07-05T12:22:49.783883+00:00 app[web.1]:    at java.lang.Thread.run(Thread.java:745)

5 个答案:

答案 0 :(得分:5)

Play code(查找" connectionTestQuery" config)建议您可以按如下方式设置测试查询:

slick.dbs.default.db.connectionTestQuery="/*ping*/ select 1" // or whatever

然后,堆栈跟踪中的错误理论上应该消失。

答案 1 :(得分:4)

我认为the Play mailing list here已讨论过这个问题。

原因是Slick不支持Heroku设置的DATABASE_URL格式。您必须将DATABASE_URL解析为JDBC URL。您可以使用heroku-jdbc库轻松完成此操作: https://github.com/jkutner/heroku-jdbc

只需在您的代码中添加这样的一行:

libraryDependencies += "com.heroku.sdk" % "heroku-jdbc" % "0.1.0"

然后您可以像这样解析URL:

var jdbcURL = DatabaseUrl.extract.jdbcUrl

然后将该网址传递给光面

修改

实际上,看起来大多数人都在手动解析DATABASE_URL,并设置带有硬编码值的光滑conf。这很好,但请注意,连接可能会发生变化,您必须手动更改设置。

因此,您需要将配置更改为:

slick.dbs.default.db.url="jdbc:postgresql://host:port/path"

但是您可能想破坏并重新创建数据库,因为您已经公开发布了连接详细信息。

我已经提交了一份PR来修复此问题: https://github.com/slick/slick/pull/1193

答案 2 :(得分:1)

播放配置需要jdbc url(JDBC_DATABASE_URL)而不是postgres(DATABASE URL)将数据库url转换为jdbc url这里是方法。第一行中的注意事项postgres写在下一行,以jdbc开头postgresql ql附加到单词postgres

postgres://<username>:<password>@<host>:<port>/<dbname>

以上格式为heroku postgres。但是播放期望低于格式。

jdbc:postgresql://<host>:<port>/<dbname>?user=<username>&password=<password>

注意:在上述网址中,请不要忘记在ql + jdbc:postgres成为ql后添加jdbc:postgresql

另外添加以下行来播放conf

slick.dbs.default.db.connectionTestQuery="SELECT 1" 

最后,您的配置文件application.conf变为

slick.dbs.default.driver="slick.driver.PostgresDriver$"

slick.dbs.default.db.driver="org.postgresql.Driver"

slick.dbs.default.db.url="jdbc:postgresql://rycmvicwkkzofh:Xt2PWggm__FQEHOeq9EoMqTOtF@ec2-54-243-132-114.compute-1.amazonaws.com:5432/d5pr4h65obtb0s?user=rycmuicwkkzofh&password=copiedfromheroku"

slick.dbs.default.db.user=rycmuicwkkzofh

slick.dbs.default.db.password="copiedfromheroku"

slick.dbs.default.db.connectionTestQuery="SELECT 1"

答案 3 :(得分:0)

我遇到了同样的问题,升级到HikariCP-2.4.0和Postgresql-9.4后问题得到了解决:)

答案 4 :(得分:0)

使用

slick.dbs.default.driver="slick.driver.PostgresDriver$"
slick.dbs.default.db.dataSourceClass = "slick.jdbc.DatabaseUrlDataSource"
slick.dbs.default.db.driver="org.postgresql.Driver"
slick.dbs.default.db.url=${JDBC_DATABASE_URL}