即使使用'autoReconnect=true',MySql JDBC仍然超时。

7
偶尔,我的Java/Tomcat6/Debian Squeeze应用程序无法与MySql服务器通信。Tomcat应用程序位于前端服务器上,而MySql位于单独的、仅运行MySql的服务器上。一个典型的错误信息如下所示:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was56588 milliseconds ago.

The last packet sent successfully to the server was 56588 milliseconds 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.

给定的超时时间只有60秒,看起来非常短。如果是一个小时或更长时间,我将简单地设置后台任务每隔几分钟ping一次DB服务器。我已经在打开的URL中添加了autoReconnect参数,但没有明显的影响。

你有什么想法吗? 谢谢 Pat


1
请查看此帖子 https://dev59.com/KXRB5IYBdhLWcg3wSVm1 - tusar
@tusar,谢谢链接。这解释了为什么我有很多代码要写。有趣的是,他们正在废弃自动重连功能。 - fishtoprecords
服务器上的wait_timeout设置为多少? - anupam.singhal
2个回答

0

配置c3p0属性以克服此问题。使用类似以下的属性:

hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider
hibernate.c3p0.min_size=0
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=500
hibernate.c3p0.max_statements=50
hibernate.c3p0.idle_test_period=3000
hibernate.c3p0.testConnectionOnCheckout=true
hibernate.c3p0.acquire_increment=1

使用JDBC连接URL url=jdbc:mysql://主机名/数据库名称?autoReconnect=true


autoReconnect已被弃用,不建议使用(请参见Connector/J参考中的属性描述或故障排除章节的15.4节末尾的注释),自2004年以来就已经被弃用。 - vilpan

0

你应该编写代码来处理网络故障并自行处理自动重新连接。

由于连接断开并以不同状态返回时可能会发生几个“应用程序”错误,因此自动重新连接被故意关闭。

无论如何,评论显示这在某种程度上是重复的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接