这几天在跟踪一个项目的时候,老是发现mysql连接报timeout的异常。
Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
The last packet successfully received from the server was 227,004,830
milliseconds ago. The last packet sent successfully to the server was
227,004,830 milliseconds ago. 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 last packet successfully received from the server was 227,004,830
milliseconds ago. The last packet sent successfully to the server was
227,004,830 milliseconds ago. 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.
经查证,发现是mysql自身的一个机制,若该连接在8小时只能没有任何活动,mysql就会断开该连接,但是连接还在连接池里边,再次请求数据库的时候,就会出现如上所述的异常。
初步查找解决方案,是在数据库连接语句里边加上:autoreconnect=true,加上之后,问题依然没有得到解决,只对mysql4之前的版本才有效果。
要增加wait_timeout和interractive_timeout的值,但是增加之后,依然会存在超时的风险,没有从根本上解决问题。
咨询DBA得知,autoreconnect=true这个属性不起作用;需要在配置文件里边加上如下语句:
对连接进行test,执行select 1,让连接一直或者,就不会再有超时的问题了。