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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9674165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:29:09  来源:igfitidea点击:

MySql JDBC timeout even with 'autoReconnect=true'

mysqljdbctimeout

提问by fishtoprecords

Occasionally, my Java/Tomcat6/Debian Squeeze application can't talk to the MySql server. The Tomcat application is on a front-end server and MySql is on a separate, MySql-only box. A typical error is:

有时,我的 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.

The timeout time given is only 60 seconds, which seems very short. If it was an hour or more, I would simply setup a background task to ping the DB-server every few minutes. I've added the autoReconnect parameter to the opening URL, with no obvious impact.

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

Any idea as to what the problem is here? Thanks Pat

知道这里有什么问题吗?谢谢帕特

回答by Rick James

You should code for network glitches and deal with auto-reconnect yourself.

您应该为网络故障编码并自己处理自动重新连接。

auto-reconnect is deliberately OFF because of several 'application' bugs that can silently happen when the connection goes away and comes back in a differentstate.

自动重新连接是故意关闭的,因为几个“应用程序”错误会在连接消失并以不同状态返回时悄悄发生。

Anyway, a comment shows that this is somewhat a Duplicate question.

无论如何,评论表明这有点重复。

回答by Kumar S

Configure c3p0 properties for overcome this issue. Use properties like,

配置 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

with JDBC connection URL url=jdbc:mysql://host/databasename?autoReconnect=true

带有 JDBC 连接 URL url=jdbc:mysql://host/databasename?autoReconnect=true