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