连接数据库超时设置autoReconnect=true
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
连接数据库超时设置autoReconnect=true的更多相关文章
- Hibernate连接数据库超时设置autoReconnect=true
如果连接闲置8小时 (8小时内没有进行数据库操作), mysql就会自动断开连接, 要重启tomcat. 不用hibernate的话, connection url加参数: autoReconnect ...
- mysql 数据库连接超时的问题 autoReconnect=true
最近在使用spring-jdbc数据库连接管理时,出现一个奇怪问题,当天部署运行没问题,第二天再试就报以下异常问题 org.springframework.dao.RecoverableDataAcc ...
- jdbc autoReconnect=true 参数设置导致 slow log 爆表。
1.过程 同事按照文档上配置了下面的jdbc url: jdbc:mysql://ip:port/db?autoReconnect=true&useUnicode=true&chara ...
- springboot 连接池wait_timeout超时设置
使用springboot 线程池连接MySQL时,mysql数据库wait_timeout 为8个小时,所以程序第二天发现报错,在url配置了 autoReconnect=true 也不行,查询配置以 ...
- 日志:using the Connector/J connection property 'autoReconnect=true' to avoid this problem
com.mysql.jdbc.CommunicationsException: The last packet successfully received from the server was581 ...
- MySQL 参数autoReconnect=true 解决8小时连接失效
<!-- dataSource加参数 处理mysql 8小时自动断开连接的问题 --> <property name="testWhileIdle" va ...
- TADOConnection如何测试连接数据库超时的解决办法
TADOConnection如何测试连接数据库超时的解决办法 TADOConnection 提供了很好的数据库连接功能,但是我们在实际使用时,如果指定的远程数据库不存在或其他原因连接不上,程序往往会 ...
- Ext Grid 加载超时设置timeout: 180000
var insideGridStore = Ext.create('Ext.data.Store', { model: 'CarComponents',//这个地方CarComponents不是一个对 ...
- MySQL 参数autoReconnect=true 解决8小时连接失效(转)
即使在创建Mysql时url中加入了autoReconnect=true参数,一但这个连接两次访问数据库的时间超出了服务器端wait_timeout的时间限制,还是会CommunicationsExc ...
随机推荐
- mongodb3.2系统性学习——5、游标 模糊查询 findAndModify函数
1首先介绍查询结果 返回的过程: 进行查询的时候mongodb 并不是一次哪个返回结果集合的所有文档,而是以多条文档的形式分批返回查询的结果,返回文档到内存中. 好处: 减少了客户端与服务器端的查询负 ...
- XStream简单使用01——xml和Ojbect互转
package org.zhb.test; /** * author : zhb * data : 2014-2-14 * use packages: * xmlpull-1.1.3.1.jar * ...
- php中mysqli 处理查询结果集的几个方法
最近对php查询mysql处理结果集的几个方法不太明白的地方查阅了资料,在此整理记下 Php使用mysqli_result类处理结果集有以下几种方法 fetch_all() 抓取所有的结果行并且以关联 ...
- C语言学习总结(一) 基本语法
第一章--C语言的基本概念 丹尼斯 里奇 一.什么是C语言? 定义:是一个面向过程的计算机高级语言--不需要任何运行环境便能运行的程序语言: 发展:目前是C11 (K&R C—> ...
- gcc向待编译源文件传入参数的方法
gcc有两种方法向待编译源文件传入参数 第一种 利用–Dmacro=name 编译选项,详见gcc -D选项 第二种 利用链接脚本(*.lds)传入参数,类似于ADS的编译器参数可以被待编译源文件调用 ...
- XMLHttpRequest 使用概括
***********************************************XMLHttpRequest对象初始化:********************************* ...
- 限制sqlserver最大内存后无法连接-EXEC sp_configure max server memory
在sql server 中设置了过小的 "max server memory"最大内存后,sqlserver可启动,但是无法连接. 网络上流行的"sqlserver 内存 ...
- find+*的问题
转自find+*的问题 不久前做移植的时候想把某个目录下的C文件都找出来,然后拷贝下,结果一直报错,我用的是*.c作为pattern.今天看论坛的时候知道为什么了. $ ls test2.c tes ...
- css全局设置
/***** 全局设置 *****/ body,h1,h2,h3,h4,h5,h6,p,form,ul,ol,li,dt,dl,dd,th,td,label,bottom,input,textar ...
- 【Java基础01】Java InputStream的read方法
JDK关于InputStream中的read方法的描述: (1) read() : 从输入流中读取数据的下一个字节,返回0到255范围内的int字节值.如果因为已经到达流末尾而没有可用的字节,则返回 ...