连接数据库超时设置autoReconnect=true
来源:互联网 发布:拍证书的软件 编辑:程序博客网 时间:2024/06/05 17:00
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,本参数值必须设置为truefalse1.1gcharacterEncoding当useUnicode设置为true时,指定字符编码。比如可设置为gb2312或gbkfalse1.1gautoReconnect当数据库连接异常中断时,是否自动重新连接?false1.1autoReconnectForPools是否使用针对数据库连接池的重连策略false3.1.3failOverReadOnly自动重连成功后,连接是否设置为只读?true3.0.12maxReconnectsautoReconnect设置为true时,重试连接的次数31.1initialTimeoutautoReconnect设置为true时,两次重连之间的时间间隔,单位:秒21.1connectTimeout和数据库服务器建立socket连接时的超时,单位:毫秒。 0表示永不超时,适用于JDK 1.4及更高版本03.0.1socketTimeoutsocket操作(读写)超时,单位:毫秒。 0表示永不超时03.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
- 连接数据库超时设置autoReconnect=true
- 连接数据库超时设置autoReconnect=true
- 连接数据库超时设置autoReconnect=true
- Hibernate连接数据库超时设置autoReconnect=true(Mysql)
- Hibernate连接数据库超时设置autoReconnect=true
- Hibernate连接数据库超时设置autoReconnect=true
- Hibernate连接数据库超时设置autoReconnect=true
- Hibernate连接数据库超时设置autoReconnect=true
- Mysql 连接数据库超时设置autoReconnect=true
- 使用Hibernate + MYSQL数据库问题(Hibernate连接数据库超时设置autoReconnect=true)
- 连接数据库超时设置autoReconnect=true(mysql5以上的,设置autoReconnect=true 是无效的 只有4.x版本,起作用)
- Mysql 设置超时重连 autoReconnect=true
- 连接数据库地址后面加上?autoReconnect=true&useUnicode=true&characterEncoding=utf8 日志
- mysql超时设置的问题 using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
- jdbc 连接数据库设置超时
- Hibernate连接数据库超时设置
- Socket设置连接超时
- Android:Activity:启动模式
- 在fragment中使用onActivityResult
- NSTimer
- C/C++ 快速排序,插入排序
- js判断object类型
- 连接数据库超时设置autoReconnect=true
- Spark 综合应用
- Android各种屏幕分辨率(VGA、HVGA、QVGA、WQVGA、WVGA、FWVGA) 详解
- 元编程笔记
- Android:Activity:生命周期
- java中判断一个字符串是否“都为数字”和“是否包含数字”和“截取数字”
- AndroidStudio介绍,代理配置,特性,技巧及从eclipse迁移到as
- Inbox (100500)
- [转]MySQL 5.7 新特性大全和未来展望