我們有一個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
看到我的答案,並檢查位於hibernate.org上的兼容性矩陣(即使我錯過了該矩陣中的c3p0) – Schildmeijer 2009-09-03 19:12:17