2019独角兽企业重金招聘Python工程师标准>>>

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]...
现只列举几个重要的参数,如下表所示:

<table border="0" cellspacing="1" cellpadding="4" width="96%" bgcolor="#b6cae1" align="center" style="color:rgb(0,0,0); font-family:Arial; font-size:14px; line-height:26px"> <tbody> <tr bgcolor="#cedee7"> <td width="85" align="middle">参数名称</td> <td width="200" align="middle">参数说明</td> <td width="50" align="middle">缺省值</td> <td width="80" align="middle">最低版本要求</td> </tr> <tr bgcolor="#ffffff"> <td>user</td> <td>数据库用户名(用于连接数据库)</td> <td>&nbsp;</td> <td>所有版本</td> </tr> <tr bgcolor="#ffffff"> <td>password</td> <td>用户密码(用于连接数据库)</td> <td>&nbsp;</td> <td>所有版本</td> </tr> <tr bgcolor="#ffffff"> <td>useUnicode</td> <td>是否使用Unicode字符集,如果参数characterEncoding设置为gb2312或gbk,本参数值必须设置为true</td> <td>false</td> <td>1.1g</td> </tr> <tr bgcolor="#ffffff"> <td>characterEncoding</td> <td>当useUnicode设置为true时,指定字符编码。比如可设置为gb2312或gbk</td> <td>false</td> <td>1.1g</td> </tr> <tr bgcolor="#ffffff"> <td>autoReconnect</td> <td>当数据库连接异常中断时,是否自动重新连接?</td> <td>false</td> <td>1.1</td> </tr> <tr bgcolor="#ffffff"> <td>autoReconnectForPools</td> <td>是否使用针对数据库连接池的重连策略</td> <td>false</td> <td>3.1.3</td> </tr> <tr bgcolor="#ffffff"> <td>failOverReadOnly</td> <td>自动重连成功后,连接是否设置为只读?</td> <td>true</td> <td>3.0.12</td> </tr> <tr bgcolor="#ffffff"> <td>maxReconnects</td> <td>autoReconnect设置为true时,重试连接的次数</td> <td>3</td> <td>1.1</td> </tr> <tr bgcolor="#ffffff"> <td>initialTimeout</td> <td>autoReconnect设置为true时,两次重连之间的时间间隔,单位:秒</td> <td>2</td> <td>1.1</td> </tr> <tr bgcolor="#ffffff"> <td>connectTimeout</td> <td>和数据库服务器建立socket连接时的超时,单位:毫秒。 0表示永不超时,适用于JDK 1.4及更高版本</td> <td>0</td> <td>3.0.1</td> </tr> <tr bgcolor="#ffffff"> <td>socketTimeout</td> <td>socket操作(读写)超时,单位:毫秒。 0表示永不超时</td> <td>0</td> <td>3.0.1</td> </tr> </tbody> </table>

对应中文环境,通常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&amp;password=&amp;useUnicode=true&amp;characterEncoding=gbk
&amp;autoReconnect=true&amp;failOverReadOnly=false

转载于:https://my.oschina.net/zhangmaoyuan/blog/1606273

连接数据库超时设置autoReconnect=true相关推荐

  1. autoreconnect mysql true_连接数据库超时设置autoReconnect=true

    mysql经典的8小时问题 链自: http://blog.csdn.net/bluesnail216/article/details/15810119 参考http://www.wang1314.c ...

  2. autoreconnect mysql true_使用’autoReconnect = true’即使MySql JDBC超时

    参见英文答案 > Why does autoReconnect=true not seem to work?                                    2个 有时,我 ...

  3. using the Connector/J connection property 'autoReconnect=true' to avoid this problem

    问题描述: com.mysql.jdbc.CommunicationsException: The last packet successfully received from the server ...

  4. mysql 禁用ssl连接_MySQL建立SSL连接问题,设置useSSL=false显式禁用SSL,或者设置useSSL=true...

    You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide tr ...

  5. Confluence 6 配置 HTTP 超时设置

    当宏,例如 RSS Macro 进行 HTTP 请求的时候,有可能因为请求的时间比较长,而导致超时.你可以通过设置系统参数来避免这个问题. 配置 HTTP 超时设置: 在屏幕的右上角单击 控制台按钮  ...

  6. MySQL 参数autoReconnect=true 解决8小时连接失效(转)

    即使在创建Mysql时url中加入了autoReconnect=true参数,一但这个连接两次访问数据库的时间超出了服务器端wait_timeout的时间限制,还是会CommunicationsExc ...

  7. 深入理解JDBC的超时设置 转

    转 http://www.admin10000.com/document/1360.html 恰当的JDBC超时设置能够有效地减少服务失效的时间.本文将对数据库的各种超时设置及其设置方法做介绍. 真实 ...

  8. Socket编程实践(9) --套接字IO超时设置方法

    引:超时设置3种方案 1. alarm超时设置方法 //代码实现: 这种方式较少用 void sigHandlerForSigAlrm(int signo) {return ; }signal(SIG ...

  9. Feign 超时设置

    feign:hystrix:enabled: true#ribbon的超时时间设置 ribbon:OkToRetryOnAllOperations: false #是否对所有请求都进行重试,默认为fa ...

最新文章

  1. SSE3 和 SSSE3 Intrinsics各函数介绍
  2. gethostbyname()函数:通过域名获取IP地址
  3. JAVA 边界布局管理器
  4. Request的getParameter和getAttribute方法的差别
  5. (转)SpringMVC学习(八)——SpringMVC中的异常处理器
  6. java 参数校验 优雅的,SpringBoot 如何优雅的进行参数校验
  7. vue3.0 word导出
  8. Spring Boot 应用上传文件时报错
  9. _itemmod_refresh
  10. 《码出高效-阿里巴巴Java开发手册-泰山版》阿里最新出品的泰山版Java开发手册,告别垃圾代码
  11. 关于Altium Designer PCB元器件的3D封装
  12. 虚拟机VM安装win7遇到的问题及解决方法
  13. 生物信息学算法之Python实现|Rosalind刷题笔记:005 GC含量计算
  14. ram与flash你知道多少?
  15. Android开发笔记——视频录制播放常见问题
  16. 命名空间“System”中不存在类型或命名空间名“Data”
  17. 一款不错的手机端视频剪辑软件
  18. 电脑麦克风没声音怎么办?3个方法快速解决
  19. 新闻推荐系统:深度知识感知网络DKN
  20. mysql slave skip_mysql运维-slave_skip_errors

热门文章

  1. linux hosts请求报错
  2. MySql中的内外联结查询
  3. 23个.net开源项目
  4. 使用了visual assist.net
  5. 【python】 邮件发送-----zmail
  6. 软件测试个人感悟之测试用例的评审重点是什么?
  7. perfdog 性能狗之Jank
  8. Selenium alert 弹窗处理
  9. js define函数_不夸张,这真的是前端圈宝藏书!360前端工程师Vue.js源码解析
  10. 线性插值改变图像尺寸_图像分割--gt;上采样的那些事