2013-03-02 80 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結束時暫時休眠一段時間來解決它。

相關問題