解决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将自动断开该连接,而连接池却认为该连接还是有效的(因为并未校验连接的有效性),当应用申请使用该连接时,就会导致上面的报错。

 

解决方案:

  1. 按异常信息所说,在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

 

转载于:https://www.cnblogs.com/kuexun/p/9818468.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值