mysql wait_timeout太小和比较大都会导致连接异常,只不过还是有区别的,网上粘贴大军就不要答了

问题描述:

在使用c3p0连接池(在不配置maxIdleTime情况下,就使用默认值0,也就是一直保持着连接)连接远程服务器。

第一种情况:

3到5分钟没有操作后,再次操作,后台报错:

The last packet successfully received from the server was 576,776 milliseconds ago

这样的错误,百度的帖子都说是修改wait_timeout但是没软用啊,因为我查了我的timeout时间是8小时,3到5分钟完全没有达到八小时啊!!

第二种情况:

后来尝试把wait_timeout调小:SET GLOBAL wait_timeout=10;调成连接超时为10s,测试结果也是当每隔时间段超过10s确实也报相同的错误。
但是反馈很快,不像第一种情况都会卡很久再报错,后来对比两种错误的reason by,还是有所不同的,这种和网络上人们解释的倒也想通,但是谁家的项目也不可能把timeout设置为10s啊!!

大神们,我想问问第一种情况到底是为什么,因为用mysql客户端连接3到5分钟不操作也会出现相同的断开问题。为什么连接远程数据库就会出现这种情况,而本地数据库就不会出现呢?

补充一下:

第一种报错为:

java.net.SocketTimeoutException: Read timed out

第二种情况报错为:
java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
两种报错情况都在这里,两者都提示类似于:The last packet successfully received from the server was 1,672,312 milliseconds ago. The last packet sent successfully to the server was 1 milliseconds ago.; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
的错误。

mysql超时会进行自动重连,只不过你不注意例如用mysql命令客户端,重连会告诉你:
mysql> show processlist;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 373559