线上的JAVA项目在进行刷新的时候,发现第一次刷新时会报错com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was xxx milliseconds ago。详细报错如下:本文地址:http://www.04007.cn/article/910.html,未经许可,不得转载.
### Cause: com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was 104,365,096 milliseconds ago. The last packet sent successfully to the server was 104,365,097 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 104,365,096 milliseconds ago. The last packet sent successfully to the server was 104,365,097 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.; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsException: #acquireRetryAttempts设置成几,后面就会重复报多少次。 The last packet successfully received from the server was 104,365,096 milliseconds ago...本文地址:http://www.04007.cn/article/910.html,未经许可,不得转载.
这里使用的是C3P0连接池,报错:The last packet successfully received from the server 会重复,且其重复次数即是C3P0中的#acquireRetryAttempts配置,原因是MySQL服务器的默认的wait_timeout是28800秒即8小时,一个连接的空闲时间超过8小时,MySQL将自动断开连接,而连接池并不知道,会一直认为该连接还是有效的(未校验有效性),当应用申请使用该连接时,就会导致上面的报错。本文地址:http://www.04007.cn/article/910.html,未经许可,不得转载.
解决方法:其实报错中已经详细提示了解决方法如下:
1. You should consider either expiring and/or testing connection validity before use in your application,
配置如c3p0在应用使用前测试连接。如下:本文地址:http://www.04007.cn/article/910.html,未经许可,不得转载.
#添加estConnection配置
<property name="maxIdleTime" value="900" />
<property name="testConnectionOnCheckin" value="true" />
<property name="idleConnectionTestPeriod" value="60" />本文地址:http://www.04007.cn/article/910.html,未经许可,不得转载.
2. increasing the server configured values for client timeouts,
配置数据库服务端的timeout参数本文地址:http://www.04007.cn/article/910.html,未经许可,不得转载.
3. or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
在url=jdbc:mysql://连接属性上增加autoReconnect=true选项实现自动连接。本文地址:http://www.04007.cn/article/910.html,未经许可,不得转载.
本文地址:http://www.04007.cn/article/910.html 未经许可,不得转载. 手机访问本页请扫描右下方二维码.
![]() |
![]() |
手机扫码直接打开本页面 |