Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

具体错误信息

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 1,548,635,756,564 milliseconds ago.  The last packet sent successfully to the server was 0 milliseconds ago.


1、一般是由于连接的Ip地址、用户名、密码有误

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure_第1张图片

 

2、 特殊的情况

2.1、在JDBC URL中使用autoReconnect属性,实际测试时使用了autoReconnect=true&failOverReadOnly=false,网上所说的只对mysql 4之前的版本有效。

2.2、修改MySQL的参数,wait_timeout最大为31536000即1年,在my.cnf中加入:

[mysqld]

wait_timeout=31536000

interactive_timeout=31536000

重启生效,需要同时修改这两个参数


两种解决异常:“The last packet sent successfully to the server was 0 milliseconds ago. ”的办法

原因:由于数据库回收了连接,而系统的缓冲池不知道,继续使用被回收的连接所致的。

           第一种解决办法

         将mysql回收空闲连接的时间变长,mysql默认是8小时,可以在mysql目录下的my.ini中增加下面配置,将时间改为1天。

          [mysqld]

          wait_timeout=86400


           第二种解决办法

          可以通过配置,让缓冲池去测试连接是否被回收,如果被回收,则不继续使用,以dbcp为例

知识点:

          #SQL查询,用来验证从连接池取出的连接
          dbcp.validationQuery=SELECT 1
          #指明连接是否被空闲连接回收器(如果有)进行检验,如果检测失败,则连接将被从池中去除
          dbcp.testWhileIdle=true
          #在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位,一般比minEvictableIdleTimeMillis小
          dbcp.timeBetweenEvictionRunsMillis=300000
          #在每次空闲连接回收器线程(如果有)运行时检查的连接数量,最好和maxActive一致
         dbcp.numTestsPerEvictionRun=50
          #连接池中连接,在时间段内一直空闲,被逐出连接池的时间(1000*60*60),以毫秒为单位
          dbcp.minEvictableIdleTimeMillis=3600000
-------------------------------------------------------------------------------------

   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   

       

        SELECT 1

       

       

        true

       

       

        300000

       

       

        8

       

       

        3600000

       



-------------------------------------------------------------------------------------

 

 

你可能感兴趣的:(sql)