【问题标题】:No suitable driver found by connection pool连接池找不到合适的驱动程序
【发布时间】:2013-03-02 14:55:03
【问题描述】:

我有一个 play 2.1 应用程序,我正在使用 junit 进行单元测试。我的测试运行良好并且能够执行他们的数据库操作。显然驱动程序 (org.postgresql.Driver) 已加载。

但是,在两次测试之间,连接池似乎无法访问驱动程序。以下是我日志中典型序列的摘录。有谁知道为什么当应用程序正常时该连接池可能无法访问驱动程序?

[info] application - QuickWitness Server shutdown...
[error] c.j.b.h.AbstractConnectionHook - Failed to acquire connection Sleeping for 1000ms and trying again. Attempts left: 10. Exception: null
[error] c.j.b.ConnectionHandle - Database access problem. Killing off all remaining connections in the connection pool. SQL State = 08001
[error] c.j.b.PoolWatchThread - Error in trying to obtain a connection. Retrying in 1000ms
java.sql.SQLException: No suitable driver found for jdbc:postgresql:qw
        at java.sql.DriverManager.getConnection(DriverManager.java:602) ~[na:1.6.0_26]
        at java.sql.DriverManager.getConnection(DriverManager.java:185) ~[na:1.6.0_26]
        at com.jolbox.bonecp.BoneCP.obtainRawInternalConnection(BoneCP.java:256) ~[bonecp.jar:0.7.1.RELEASE]
        at com.jolbox.bonecp.ConnectionHandle.obtainInternalConnection(ConnectionHandle.java:211) ~[bonecp.jar:0.7.1.RELEASE]
        at com.jolbox.bonecp.ConnectionHandle.<init>(ConnectionHandle.java:170) ~[bonecp.jar:0.7.1.RELEASE]
        at com.jolbox.bonecp.PoolWatchThread.fillConnections(PoolWatchThread.java:101) [bonecp.jar:0.7.1.RELEASE]
[info] application - QuickWitness Server has started
[debug] application - entering ensureTriggersAndStoredProceduresAreInstalled()
[debug] application - exiting ensureTriggersAndStoredProceduresAreInstalled()
[info] application - logging initialized
[info] application - Register user request from localhost:12345
[info] application - QuickWitness Server shutdown...
[error] c.j.b.h.AbstractConnectionHook - Failed to acquire connection Sleeping for 1000ms and trying again. Attempts left: 10. Exception: null
[error] c.j.b.ConnectionHandle - Database access problem. Killing off all remaining connections in the connection pool. SQL State = 08001
[error] c.j.b.PoolWatchThread - Error in trying to obtain a connection. Retrying in 1000ms
java.sql.SQLException: No suitable driver found for jdbc:postgresql:qw
        at java.sql.DriverManager.getConnection(DriverManager.java:602) ~[na:1.6.0_26]
        at java.sql.DriverManager.getConnection(DriverManager.java:185) ~[na:1.6.0_26]
        at com.jolbox.bonecp.BoneCP.obtainRawInternalConnection(BoneCP.java:256) ~[bonecp.jar:0.7.1.RELEASE]
        at com.jolbox.bonecp.ConnectionHandle.obtainInternalConnection(ConnectionHandle.java:211) ~[bonecp.jar:0.7.1.RELEASE]
        at com.jolbox.bonecp.ConnectionHandle.<init>(ConnectionHandle.java:170) ~[bonecp.jar:0.7.1.RELEASE]
        at com.jolbox.bonecp.PoolWatchThread.fillConnections(PoolWatchThread.java:101) [bonecp.jar:0.7.1.RELEASE]
[info] application - QuickWitness Server has started

【问题讨论】:

  • 那么在 Play 环境中运行时,您是如何使驱动程序可用的?
  • 我已经在application.conf文件中指定了驱动的名称:db.default.driver=org.postgresql.Driver 驱动的jar在classpath中。
  • 确定它在类路径中吗?你是如何把它放在类路径中的?我怀疑这就是问题所在。
  • 我对 mysql 也有同样的问题:db.default.driver=com.mysql.jdbc.Driverdb.default.url="mysql://root:XXXXXXXXXXXXX@127.0.0.1/myxer_searcher_server?characterEncoding=UTF-8&amp;useUnicode=true" 奇怪的是,尽管有错误,但代码中的一切都正常

标签: playframework playframework-2.1


【解决方案1】:

我遇到了同样的问题。就我而言,问题发生是因为那里没有足够的数据库连接。似乎 play 不会关闭块末尾的连接。文档讲述的是另一个故事,所以也许这是一个错误?

解决方案 1:您可以通过更改来增加 application.conf 中的连接数 (http://www.playframework.com/documentation/2.1.0/SettingsJDBC)

db.default.partitionCount=2
db.default.maxConnectionsPerPartition=5
db.default.minConnectionsPerPartition=5

解决方案 2:您可以在使用后关闭连接 (http://www.playframework.com/documentation/2.0/ScalaDatabase)

DB.withConnection { conn =>   
  // do whatever you need with the connection   
  conn.close()
}

【讨论】:

  • 明确关闭连接不起作用。尤其是在 withConnection 块上它没有效果。我尝试明确关闭 withTransaction 块上的连接,一切都崩溃了
【解决方案2】:

我假设您已在 /Project/lib 文件夹中正确放置了所需的驱动程序

1) 检查application.conf 中的数据库url 配置。

2)在application.conf中添加“evolutionplugin=disabled”

3) 参考http://digitalsanctum.com/2012/05/30/play-framework-2-tutorial-database-access/,你在build.scala中添加依赖了吗?

谢谢,希望能帮上忙~~

【讨论】:

    【解决方案3】:

    我在使用 squeryl 时遇到了同样的问题。我的代码似乎没有任何问题。我认为会发生这种情况:由于单元测试,我的应用程序连续多次启动和停止。 Play 在停止应用程序时关闭连接。但是,如果您的机器足够快,它可以在应用程序启动时请求新连接,然后再关闭上次运行中使用的连接。这只是时间问题。您可以通过增加最大数据库连接数或在 Global 的 onStop 结束时简单地休眠一会儿来解决它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-26
      • 2018-08-04
      • 2012-01-20
      • 2020-05-20
      • 2014-04-20
      • 1970-01-01
      • 2012-12-09
      相关资源
      最近更新 更多