2013-03-02 139 views
4

我有一个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 
+0

那么在Play环境中运行时,您是如何使驱动程序可用的? – 2013-03-02 14:56:25

+0

我在application.conf文件中指定了驱动程序的名称: db.default.driver = org.postgresql.Driver 驱动程序的jar在类路径中。 – user2102276 2013-03-04 00:57:44

+1

你确定*它在类路径中吗?你怎么把它放在类路径中?我怀疑这就是问题所在。 – 2013-03-04 07:37:21

回答

1

我有同样的问题。就我而言,问题发生是因为没有足够的数据库连接。看起来这场比赛不会在封闭结束时关闭连线。这些文件告诉我们另一个故事,或许这是一个错误?

解决方案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() 
} 
+0

显式关闭连接无效。特别是在'withConnection'块中它没有任何效果。我试着明确地关闭'withTransaction'块上的连接,并且所有东西都崩溃了 – 2013-05-17 11:18:11

0

我和squeryl有同样的问题。看起来我的代码没有问题。我认为这是会发生的事情:由于单元测试我的应用程序被连续多次启动和停止。 Play在停止应用程序时关闭连接。但是,如果您的机器足够快,它可以在应用程序启动时请求新的连接,然后才能关闭上次运行中使用的连接。这只是一个时间问题。你可以通过增加数据库连接的最大数量,或者在Global's onStop结束时暂时休眠一段时间来解决它。

相关问题