Hibernate c3p0连接池没有超时空闲连接

Dou*_*kem 7 database hibernate database-connection connection-pooling c3p0

我们有一个连接到MySQL 5数据库的java服务器,使用Hibernate作为我们的持久层,它使用c3p0进行数据库连接池.

我试过跟随c3p0和hibernate文档:

我们的生产服务器上出现错误,说明:

...引起:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:连接关闭后不允许任何操作.由于基础异常/错误,连接被隐式关闭:

开始没有异常了

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException

消息:从服务器成功收到的最后一个数据包是45000秒前.成功发送到服务器的最后一个数据包是45000秒前,这比服务器配置的'wait_timeout'值长.您应该考虑在应用程序中使用之前过期和/或测试连接有效性,增加服务器配置的客户端超时值,或使用Connector/J连接属性"autoReconnect = true"来避免此问题.

堆栈跟踪:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:从服务器成功收到的最后一个数据包是45000秒前.成功发送到服务器的最后一个数据包是45000秒前,这比服务器配置的'wait_timeout'值长.您应该考虑在应用程序中使用之前过期和/或测试连接有效性,增加服务器配置的客户端超时值,或使用Connector/J连接属性"autoReconnect = true"来避免此问题.

我们的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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Sch*_*jer 2

通过检查日志确保 c3p0 确实正在启动。由于某种原因,我的类路径上有两个版本的 hibernate(hibernate-core3.3.1.jar 和 hibernate-3.2.6GA.jar)。我还使用了 hibernate 注释版本 3.4.0GA,它与 3.2.x 不兼容。(不知道这是否与原来的问题有关)。删除其中一个 hibernate jar 后(不记得我删除了哪个,可能是 hibernate-3.2.6GA.jar),c3p0 终于启动了,我摆脱了 8 小时不活动后发生的烦人的 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException 。

  • 我知道我对这个问题很迟钝,但这对于寻找答案的人来说可能很有用:似乎这个provider_class参数是在3.2中添加的,现在是必需的,但没有太多记录。请参阅http://www.agileapproach.com/blog-entry/hibernate-c3p0-connection-pool-config-changes (2认同)