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