想必大家在用MySQL
时都会遇到连接超时的问题,如下图所示:
### Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 47,795,922 milliseconds ago. The last packet sent successfully to the server was 47,795,922 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.
; SQL []; The last packet successfully received from the server was 47,795,922 milliseconds ago. The last packet sent successfully to the server was 47,795,922 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.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 47,795,922 milliseconds ago. The last packet sent successfully to the server was 47,795,922 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.
大概意思是当前的connection
所进行过的最新请求是在52,587
秒之前,这个时间是大于服务所配置的wait_timeout
时间的。
MySQL
连接时,服务器默认的“wait_timeout”
是8小时
,也就是说一个connection
空闲超过8小时
,Mysql
将自动断开该connection
。connections
如果空闲超过8小时,Mysql
将其断开,而DBCP
连接池并不知道该connection
已经失效,如果这时有Client
请求connection
,DBCP
将该失效的Connection
提供给Client
,将会造成异常。
mysql
分析:
打开MySQL
的控制台,运行:show variables like ‘%timeout%’
,查看和连接时间有关的MySQL
系统变量,得到如下结果:
其中wait_timeout
就是负责超时控制的变量,其时间为长度为28800s
,就是8个小时
,那么就是说MySQL
的服务会在操作间隔8小时后断开,需要再次重连。也有用户在URL
中使用jdbc.url=jdbc:mysql://localhost:3306/nd?autoReconnect=true
来使得连接自动恢复,当然了,这是可以的,不过是MySQL4
及其以下版本适用。MySQL5中已经无效了,必须调整系统变量来控制
。
MySQL5
手册中对两个变量有如下的说明:
interactive_timeout
:服务器关闭交互式连接前等待活动的秒数。交互式客户端定义为在mysql_real_connect()
中使用CLIENT_INTERACTIVE
选项的客户端。又见wait_timeout
wait_timeout
:服务器关闭非交互连接之前等待活动的秒数。在线程启动时,根据全局wait_timeout
值或全局interactive_timeout
值初始化会话wait_timeout
值,取决于客户端类型(由mysql_real_connect()
的连接选项CLIENT_INTERACTIVE
定义),又见interactive_timeout
如此看来,两个变量是共同控制的,那么都必须对他们进行修改了。继续深入这两个变量wait_timeout
的取值范围是1-2147483(Windows),1-31536000(linux)
,interactive_time
取值随wait_timeout
变动,它们的默认值都是28800s
。
MySQL
的系统变量由配置文件控制,当配置文件中不配置时,系统使用默认值,这个28800
就是默认值。要修改就只能在配置文件里修改。Windows
下在%MySQL HOME%/bin
下有mysql.ini
配置文件,打开后在如下位置添加两个变量,赋值。(这里修改为388000
)
MySQL
的 wait_timeout
属性的值 (不推荐
)修改mysql安装目录下的配置文件 my.ini
文件(如果没有此文件,复制“my-default.ini”
文件,生成“复件 my-default.ini”
文件。将“复件 my-default.ini”
文件重命名成“my.ini”
),在文件中设置:
wait_timeout=31536000
interactive_timeout=31536000
这两个参数的默认值是8小时(60*60*8=28800)
, 也可以使用mysql命令对这两个属性进行修改.
注意:
1.wait_timeout的最大值只允许2147483 (24天左右)
减少连接池内连接的生存周期,使之小于上一项中所设置的wait_timeout
的值。 修改 c3p0
的配置文件,在 Spring 的配置文件中设置:
定期使用连接池内的连接,使得它们不会因为闲置超时而被 MySQL
断开。
修改 c3p0
的配置文件,在 Spring
的配置文件中设置:
附上dbcp
和c3p0
的标准配置
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@localhost:1521:Test
Kay
root