7

我們有一個java服務器連接到MySQL 5數據庫使用Hibernate作爲我們的持久層使用c3p0進行數據庫連接池。休眠c3p0連接池沒有超時空閒連接

我試過以下的C3P0和Hibernate文檔:

我們正在對我們的生產錯誤SER VERS指出:

... Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:

BEGIN NESTED EXCEPTION

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException

MESSAGE: The last packet successfully received from the server was45000 seconds ago.The last packet sent successfully to the server was 45000 seconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

STACKTRACE:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was45000 seconds ago.The last packet sent successfully to the server was 45000 seconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

我們有我們c3p0連接的屬性設置如下:

hibernate.c3p0.max_size=10 
hibernate.c3p0.min_size=1 
hibernate.c3p0.timeout=5000 
hibernate.c3p0.idle_test_period=300 
hibernate.c3p0.max_statements=100 
hibernate.c3p0.acquire_increment=2 

default MySQL wait_timetout默認值爲28800秒(8小時),報告的錯誤是說,它已經過45000秒(約12.5小時)。雖然c3p0配置表明它會「超時」空閒連接,這些連接在5000秒後還沒有被使用,並且會每隔300秒檢查一次,因此空閒連接永遠不會超過5299秒。

我已經通過設置我的開發人員MySQL(在Windows上的my.ini,在Unix上的my.cnf)wait_timeout = 60並將c3p0的空閒超時值降低到60秒以下來測試本地,並且它將正確地超時閒置連接並創建新的。我也檢查以確保我們不泄漏數據庫連接並保持連接,並且不會出現我們。

以下是我用來在開發人員環境中測試的c3p0.properties文件,以確保c3p0正確處理連接。

hibernate.properties(與MySQL測試WAIT_TIMEOUT = 60)

hibernate.c3p0.max_size=10 
hibernate.c3p0.min_size=1 
hibernate.c3p0.timeout=20 
hibernate.c3p0.max_statements=100 
hibernate.c3p0.idle_test_period=5 
hibernate.c3p0.acquire_increment=2 

c3p0.properties

com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL=ALL 
com.mchange.v2.log.MLog=com.mchange.v2.log.FallbackMLog 
c3p0.debugUnreturnedConnectionStackTraces=true 
c3p0.unreturnedConnectionTimeout=10 
+0

看到我的答案,並檢查位於hibernate.org上的兼容性矩陣(即使我錯過了該矩陣中的c3p0) – Schildmeijer 2009-09-03 19:12:17

回答

3

確保C3P0真的開始通過檢查日誌。出於某種原因,我在我的類路徑中有兩個版本的hibernate(hibernate-core3.3.1.jar和hibernate-3.2.6GA.jar)。我還使用了與3.2.x不兼容的hibernate annotatations版本3.4.0GA。 (不知道這是否與原始問題有關)。 刪除其中一個休眠jar的(不記得我刪除了,可能hibernate-3.2.6GA.jar)c3p0終於開始,我擺脫了煩人的com.mysql.jdbc.exceptions.jdbc4.CommunicationsException發生efter 8h閒置。

+0

我們使用Hibernate 3.2.6 Hibernate,使用3.4.0GA Hibernate註釋,而且我們不有任何明顯的例外(這些問題僅在8小時後處理c3p0纔會出現,否則我們會注意到)。 我們看到在我們的日誌中配置並使用了C3P0(一旦我使用c3p0.properties進行登錄),例如 「[INFO]初始化c3p0池... com.mchange.v2.c3p0。PoolBackedDataSource ... idleConnectionTestPeriod - > 20,,maxIdleTime - > 60 ...「 – Dougnukem 2009-09-03 19:32:01

+0

嘗試將hibernate升級到3.3.X,上述組合不被hibernate支持(或降級hibernate註釋) – Schildmeijer 2009-09-04 12:05:38

+0

試過什麼? – Schildmeijer 2009-09-06 08:19:22