解决mysql默认的8小时自动断开连接
语言:javaEE
框架:spring mvc+spring+mybatis
数据库:mysql8
WEB服务器:tomcat8
背景:
在试运营阶段发现发生“连接超时”异常
抛出异常:
Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 353,479,051 milliseconds ago. The last packet sent successfully to the server was 353,479,078 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 353,479,051 milliseconds ago. The last packet sent successfully to the server was 353,479,078 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 353,479,051 milliseconds ago. The last packet sent successfully to the server was 353,479,078 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服务器默认的“wait_timeout”是28800秒即8小时,意味着如果一个连接的空闲时间超过8个小时,MySQL将自动断开该连接,而连接池却认为该连接还是有效的(因为并未校验连接的有效性),当应用申请使用该连接时,就会导致上面的报错。
解决方案:
- 按异常信息所说,在MYSQL的JDBC连接串上加上autoReconnect=true
未能解决,据说这是针对mysql版本是5以前的。
2. 增加 MySQL 的 wait_timeout 属性的值
方法1 修改配置文件my.ini中的wait_timeout和interactive_timeout
方法2 使用mysql命令
set global wait_timeout=2880000;
set global interactive_timeout=2880000;
未能解决,原因未知
3. 减少连接池内连接的生存周期:减少连接池内连接的生存周期,使之小于上一项中所设置的wait_timeout 的值。
方法:修改 c3p0 的配置文件,在 Spring 的配置文件中设置:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="maxIdleTime"value="1800"/>
<!--other properties -->
</bean>
未尝试。
4. 每隔一段时间往数据库发一条查询语句,这样使得数据库空闲时间不会太长,而使得其自动关闭。
方法:在SqlMapConfig.xml的dataSource进行如下配置:
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${jdbc.driverClassName}"/>
<property name="JDBC.ConnectionURL" value="${jdbc.url}"/>
<property name="JDBC.Username" value="${jdbc.username}"/>
<property name="JDBC.Password" value="${jdbc.password}"/>
<property name="Pool.PingEnabled" value="true"/>
<property name="Pool.PingQuery" value="select 1"/>
<property name="Pool.PingConnectionsOlderThan" value="0"/><br><br>
<property name="Pool.PingConnectionsNotUsedFor" value="3600000"/>
</dataSource>
开始的3行是关于数据库连接信息的,不需要说明了。
Pool.PingEnabled:是用于设置开启是否允许检测连接状态
Pool.PingQuery:是用于检测连接的查询语名,当然是越简单越好
Pool.PingConnectionOlderThan:对持续连接时间超过设定值(毫秒)的连接进行检测,我将其设置为0(不进行此项检测),否则,iBatis在超过这个时间后,执行每个sql以前检测连接,对于性能可能会有一定的影响。
Pool.PingConnectionsNotUsedFor:对空闲超过设定值(毫秒)的连接进行检测,我设置为1小时(mysql缺省的关闭时间是8小时)
成功!
参考博文:https://blog.csdn.net/u012129031/article/details/72621288
http://hunterk.iteye.com/blog/544356
https://blog.csdn.net/qq_37164847/article/details/80780984
解决mysql默认的8小时自动断开连接的更多相关文章
- dbcp基本配置和重连配置 -- mysql 8小时自动断开连接的问题
1. 引入dbcp (选择1.4) Java代码 com.alibaba.external jakarta.commons.dbcp 1.4 2. dbcp的基本配置 相关配置说明: initia ...
- mysql 连接空闲超8小时自动断开连接问题(linux)
在mysql配置文件里添加wait_timeout和interactive_timeout两个值 [mysqld] wait_timeout= interactive_timeout= 超时时间,10 ...
- MySQL5.6数据库8小时内无请求自动断开连接
问题: 最近的项目中,发现Mysql数据库在8个小时内,没有请求时,会自动断开连接,这是MySQL服务器的问题.The last packet successfully received from t ...
- 解决VirtualBox虚拟机中PM3总是自动断开的问题
一.问题 运行环境: 虚拟机软件:VirtualBox 6.0.8 r130520 宿主机:Win10 1803 客户机:Ubuntu 19.04 问题: 当插入PM3并连入虚拟机后,PM3过几十秒会 ...
- SSH自动断开连接的原因-20200323
SSH自动断开连接的原因 方法一: 用putty/SecureCRT连续3分钟左右没有输入, 就自动断开, 然后必须重新登陆, 很麻烦. 在网上查了很多资料, 发现原因有多种, 环境变量TMOUT ...
- Putty CentOS SSH 总是自动断开连接
/********************************************************************** * Putty CentOS SSH 总是自动断开连接 ...
- WebSocket断开原因、心跳机制防止自动断开连接
1.断开原因 WebSocket断开的原因有很多,最好在WebSocket断开时,将错误打印出来. ws.onclose = function (e) { console.log('websocket ...
- MySql连接空闲8小时自动断开引起的问题
一.问题描述 最近遇到了一个奇怪的MySql数据库问题,好几次前一天晚上历史数据还正常存储,第二天早上来了看实时数据存储还正常,历史数据不存储了.找了好久也没找到问题.后来仔细想了想,历史数据设置 ...
- SecureCRT自动断开连接的解决方法
方法一: 在普通用户下,输入重启sshd服务的命令:service sshd restart 这时会提示:管理系统服务或单元需要身份验证.解决的方法:先要切换为root用户,接着重启sshd服务:se ...
随机推荐
- c#: Noto Sans字体如何支持韩文
1.源起: VCU10项目,使用了Noto Sans字体,的确漂亮.但验证在win7下,其显示韩文为乱码,颇为头痛. 其界面显示如图: 度娘之,得Noto Sans又有CJK字体,顾名思义,其为支持中 ...
- 30-算法训练 最短路 spfa
http://lx.lanqiao.cn/problem.page?gpid=T15 算法训练 最短路 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个n个顶点, ...
- golang 通过fsnotify监控文件,并通过文件变化重启程序
一.下载我们需要的包 > go get github.com/fsnotify/fsnotify 二.使用fsnotify监控文件 package main; import ( "gi ...
- 【gRPC使用问题4】
1.进行gRPC服务调用出错:服务不可用 2.解决方案: linux系统部署的节点服务的确不可用,愿意是 系统是泡在虚拟机里面, 计算核数只有一核,太小,服务没有跑起来.
- 基于.net技术的 Rss 订阅开发
RSS(Really Simple Syndication,真正简单的连锁)是一种 Web 内容连锁格式.RSS 成为通过 Web 连锁新闻内容的标准格式.刚好我现在vs的环境也是.net,因为在.N ...
- 11. 标准库浏览 – Part II
第二部分包含了支持专业编程工作所需的更高级的模块,这些模块很少出现在小脚本中. 11.1. 输出格式 reprlib 模块为大型的或深度嵌套的容器缩写显示提供了 :repr() 函数的一个定制版本: ...
- iOS.AutoLayout.2.CustomView-with-AutoLayout
Custom View Which Support AutoLayout 创建支持AutoLayout的Custom View AutoLayout 通过使view更加的自组织来减轻controlle ...
- Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
最近在Tomcat上配置一个项目,在点击一个按钮,下载一个文件的时候,老是会报上面的错误.试了很多方法,如对server.xml文件中,增加MaxHttpHeaderSize的大小,改端口,改Tomc ...
- BZOJ 3007 [SDOI2012]拯救小云公主 - 对偶图 + 并查集
Solution 答案具有单调性, 显然可以二分答案. 有两个注意点 : 英雄是可以随便走的, 也就是不是网格图... 还有坐标不能小于$1$ QAQ 开始时英雄在左下角, 公主在右上角, 我们反过来 ...
- LibreOJ 2003. 「SDOI2017」新生舞会 基础01分数规划 最大权匹配
#2003. 「SDOI2017」新生舞会 内存限制:256 MiB时间限制:1500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...