Mysql的链接超时异常CommunicationsException
原文是在博客上的:小重合之旅
链接如下:未经过作者同意,这里注明下。
http://blog.csdn.net/bluesnail216/article/details/15810119
1,问题现象:
com.MySQL.jdbc.CommunicationsException: The last packet successfully received from the server was58129 seconds ago.The last packet sent successfully to the server was 58129 seconds ago, which 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.
解决办法:
如果连接闲置8小时 (8小时内没有进行数据库操作), mysql就会自动断开连接, 要重启tomcat.
不用hibernate的话, connection url加参数: autoReconnect=true
用hibernate的话, 加如下属性:
<property
name="connection.autoReconnect">true</property>
<property
name="connection.autoReconnectForPools">true</property>
<property
name="connection.is-connection-validation-required">true</property>
要是还用c3p0连接池:
<property
name="hibernate.c3p0.acquire_increment">1</property>
<property
name="hibernate.c3p0.idle_test_period">0</property>
<property
name="hibernate.c3p0.timeout">0</property>
<property name="hibernate.c3p0.validate">true</property>
以上转载链接地址:http://blog.aqsc.cn/article.php?type=blog&itemid=1016
2,另外:关于Mysql链接参数的说明如下:
mysql JDBC
Driver
常用的有两个,一个是gjt(Giant Java Tree)组织提供的mysql驱动,其JDBC Driver名称(Java类名)为:org.gjt.mm.mysql.Driver
详情请参见网站:http://www.gjt.org/
或在本网站下载mysql JDBC Driver(mm.jar)
另一个是mysql官方提供的JDBC Driver,其JAVA类名为:com.mysql.jdbc.Driver
驱动下载网址:http://dev.mysql.com/downloads/,进入其中的MySQL Connector/J区域下载。
mysql JDBC URL格式如下:
jdbc:mysql://[host:port],[host:port].../[database][?参数名1][=参数值1][&参数名2][=参数值2]...
现只列举几个重要的参数,如下表所示:
|
参数名称 |
参数说明 |
缺省值 |
最低版本要求 |
|
user |
数据库用户名(用于连接数据库) |
所有版本 |
|
|
password |
用户密码(用于连接数据库) |
所有版本 |
|
|
useUnicode |
是否使用Unicode字符集,如果参数characterEncoding设置为gb2312或gbk,本参数值必须设置为true |
false |
1.1g |
|
characterEncoding |
当useUnicode设置为true时,指定字符编码。比如可设置为gb2312或gbk |
false |
1.1g |
|
autoReconnect |
当数据库连接异常中断时,是否自动重新连接? |
false |
1.1 |
|
autoReconnectForPools |
是否使用针对数据库连接池的重连策略 |
false |
3.1.3 |
|
failOverReadOnly |
自动重连成功后,连接是否设置为只读? |
true |
3.0.12 |
|
maxReconnects |
autoReconnect设置为true时,重试连接的次数 |
3 |
1.1 |
|
initialTimeout |
autoReconnect设置为true时,两次重连之间的时间间隔,单位:秒 |
2 |
1.1 |
|
connectTimeout |
和数据库服务器建立socket连接时的超时,单位:毫秒。 0表示永不超时,适用于JDK 1.4及更高版本 |
0 |
3.0.1 |
|
socketTimeout |
socket操作(读写)超时,单位:毫秒。 0表示永不超时 |
0 |
3.0.1 |
对应中文环境,通常mysql连接URL可以设置为:
jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk&autoReconnect=true&failOverReadOnly=false
在使用数据库连接池的情况下,最好设置如下两个参数:
autoReconnect=true&failOverReadOnly=false
需要注意的是,在xml配置文件中,url中的&符号需要转义成&。比如在tomcat的server.xml中配置数据库连接池时,mysql jdbc url样例如下:
jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk
&autoReconnect=true&failOverReadOnly=false
其他参数请参见mysql jdbc官方文档:MySQL
Connector/J Documentation
Mysql的链接超时异常CommunicationsException的更多相关文章
- android+eclipse+mysql+servlet(Android与mysql建立链接)
原创作品,允许转载,转载时请务必以超链接形式标明文章 原创地址 .作者信息和本声明.http://www.cnblogs.com/zhu520/p/7724524.html 经过两天的时间我终于把A ...
- python之便携式mysql类和tornado mysql长链接
mymysql.py class MyMysql2(object): def __init__(self, host = '', user = '', passwd = '', db = '', po ...
- MySQL的链接,查看数据库,使用数据库,查看表
MySQL的链接,查看数据库,使用数据库,查看表 mysql> show databases; +--------------------+ | Database | +------------ ...
- Python安装第三方包(模块/工具)出现链接超时,网速慢,安装不上的问题如何解决
之前我的电脑重新装了系统以后,发现安装完Python后, 使用pip linstall 安装第三方包的时候,网速慢的一匹 有时候只有几百b/s ,而且还动不动就会出现无法安装,链接超时等问题. 今天我 ...
- mysql 的链接字符
mysql的链接字符: driver =com.mysql.cj.jdbc.Driverurl =jdbc:mysql://localhost:3306/oa?serverTimezone=Asia/ ...
- mysql 事务锁超时时间 innodb_lock_wait_timeout
mysql 事务锁超时时间 innodb_lock_wait_timeout: # 查询全局等待事务锁超时时间 SHOW GLOBAL VARIABLES LIKE 'innodb_lock_wait ...
- oracle用plsql登陆出错,提示ORA-12170:TNS:链接超时 --------关闭防火墙试试
oracle用plsql登陆出错,提示ORA-12170:TNS:链接超时 但是使用sqlplus可以连接 ping 本机127.0.0.1 显示一般故障 后关闭防火墙,问题解决. ps:登录时使用@ ...
- Navicat for MySQL打开链接时出错错误为:2005 - Unknown MySQL server host 'localhost'(0)?
问题:Navicat for MySQL打开链接时出错错误为:2005 - Unknown MySQL server host 'localhost'(0)? 在使用navicat 连接mysql数据 ...
- python中自定义超时异常的几种方法
最近在项目中调用第三方接口时候,经常会出现请求超时的情况,或者参数的问题导致调用异代码异常.针对超时异常,查询了python 相关文档,没有并发现完善的包来根据用户自定义的时间来抛出超时异常的模块.所 ...
随机推荐
- Linux基础ls命令
ls 命令是linux下最常用的命令,通过ls 命令不仅可以查看linux文件夹包含的文件而且可以查看文件权限(包括目录.文件夹.文件权限)查看目录信息等等.ls 命令在日常的linux操作中用的 ...
- Redis的订阅发布
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ServiceS ...
- [译]GLUT教程 - 初始化
Lighthouse3d.com >> GLUT Tutorial >> Basics >> Initialization 这一节开始从main函数入手.第一步是线 ...
- 【puppeteer+Node.js安装环境】之步骤
步骤一:首先,安装node.js环境,从官网下载最新的安装包. 步骤二:安装完成之后,再安装npm,通过命令行输入:npm install -g cnpm --registry=https://reg ...
- 在Ubuntu下编译FFmpeg
第一步:准备编译环境 .tar.bz2 -2245/ ./configure --enable-static--enable-shared--prefix=/usr/localmakesudomake ...
- Python鸡汤
标准库 很正确 外部库 有一些风险,可能有bug,可能文档不全,可能长时间未更新. ipython 1 pip 这应该是安装Python后第一个需要的命令 pip install -i -i, --i ...
- 批处理--执行sql(mysql数据库)
@echo off rem test.sql文件 for %%i in (test.sql) do ( echo excute %%i mysql -u用户名 -p密码 -D数据库名 < %%i ...
- java中业务接口
今天写完业务层在抽取接口的时候脑子里突然产生了一个问题:抽取接口到底有什么用呢? 在刚刚学习接口的时候知道接口是为了实现java的多继承,但是现在每一个业务类都要抽取一个接口,每当该类需要增加方法的时 ...
- Collective Mindsets (easy)(逻辑题)
Collective Mindsets (easy) Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d &am ...
- 记录-springMVC访问web-inf下文件问题+在jsp页面导入jquery插件路径不对问题
环境:spring + springMvc + mybatis + maven 关于在springMVC环境访问web-inf目录下文件,其一有在springMVC xml文件下加 <!-- 对 ...