一、背景

  最近在家里捣鼓一个公司自己搭建的demo的时候,发现程序一启动就会出现CommunicationsException: Communications link failure错误,经过一番排查最后发现是数据库url写错造成的,这个过程中也对出现这个错误的解决思路有了一些自己的理解,现和大家分享。该错误的具体信息如下:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:981)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:339)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
......

二、解决问题步骤

1.检查你的数据库连接地址(配置文件中的url)是否正确.

2.有可能是由mysql5数据库的配置引起的。mysql5将其连接的等待时间(wait_timeout)缺省为8小时。在其客户程序中可以这样来查看其值:

mysql > show global variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout | 28800 |
+---------------+-------+
1 row in set (0.00 sec)

28800 seconds,也就是8小时,如果在wait_timeout秒期间内,数据库连接(java.sql.Connection)一直处于等待状态,mysql5就将该连接关闭。这时,你的Java应用的连接池仍然合法地持有该连接的引用。当用该连接来进行数据库操作时,就碰到上述错误。

三、解决方式

1.mysql5以前的版本可以直接在jdbc连接url的配置中附加上“autoReconnect=true”。

2.将mysql的全局变量wait_timeout的值修改为最大。查看mysql5的手册,发现windows和linux下wait_timeout的最大值分别是24天和365天。

 (1).在文件my.ini的最后增加一行:wait_timeout=1814400。(该文件,windows下在mysql的安装目录下,linux下位置为/etc/my.ini)

 (2).重启mysql。

3.如果经过了以上的步骤,你的问题依旧没有的到解决,则建议你修改下你程序中的mysql驱动的版本。

四、总结

  通过本问题的解决,我们认识到如果碰到问题,首先不要慌,然后从最近的位置进行排查,最后一定能够完美解决问题。

解决Java程序连接mysql数据库出现CommunicationsException: Communications link failure错误的问题的更多相关文章

  1. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 解决办法

    09:00:30.307 [http-8080-6] ERROR org.hibernate.transaction.JDBCTransaction -JDBC begin failed com.my ...

  2. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 数据库报错

    -- 查询mysql 数据库链接空闲时间持有最大空闲时间,单位为秒 SHOW VARIABLES WHERE VAriable_name = 'interactive_timeout'; -- 会出现 ...

  3. 异常解决:Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    异常描述 这个异常通常有如下信息: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failu ...

  4. mysql 5.1超过默认8小时空闲时间解决办法(错误:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure)

    报错: MySQL第二天早上第一次连接超时报错, com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications lin ...

  5. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 解决

    感谢大佬:https://blog.csdn.net/a704397849/article/details/93797529 springboot + mybatis多数据库 + druid连接池配置 ...

  6. Caused by: java.net.ConnectException: Connection refused/Caused by: java.lang.RuntimeException: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    1.使用sqoop技术将mysql的数据导入到Hive出现的错误如下所示: 第一次使用命令如下所示: [hadoop@slaver1 sqoop--cdh5.3.6]$ bin/sqoop impor ...

  7. Java连接MySQL报错:CommunicationsException: Communications link failure

    现象: 报错:Exception in thread "main" com.mysql.cj.jdbc.exceptions.CommunicationsException: Co ...

  8. 解决:com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure(真实有效)

    数据库连接失败 一.例如我在SpringBoot项目中使用了阿里的数据库连接池Driud. 有次在启动的时候,会报这样的错: Caused by: com.mysql.cj.exceptions.CJ ...

  9. Oozie时出现Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure?

    不多说,直接上干货! 问题详情 [hadoop@bigdatamaster oozie--cdh5.5.4]$ bin/ooziedb.sh create -sqlfile oozie.sql -ru ...

随机推荐

  1. 漫谈C++:良好的编程习惯与编程要点

    以良好的方式编写C++ class 假设现在我们要实现一个复数类complex,在类的实现过程中探索良好的编程习惯. ① Header(头文件)中的防卫式声明 complex.h: # ifndef ...

  2. java socket API

    java.net类 Socket java.lang.Object java.net.Socket 直接已知子类: SSLSocket public class Socketextends Objec ...

  3. mysql 错误 ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number 解决办法

    MySQL创建用户(包括密码)时,会提示ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number: 问题原因: ...

  4. Mac OS sierra app is damaged

    想升级系统,然后发生如题错误,谷歌之,解决方法如下(希望帮助大家): Step 1. Go to your applications folder. Step 2. Find the installe ...

  5. WKWebView与JavaScript交互基础

    login.html 代码 <!DOCTYPE html> <html> <head> <title>使用JavaScript</title> ...

  6. CocoaPods升级,升级以后出现bug的解决方法(升级必看!)

    命令行更新(安装)步骤[更新步骤] $ sudo gem update --system // 先更新gem,国内需要切换源 $ gem sources --remove https://rubyge ...

  7. document.documentElement.clientHeight 和 $(window).height() 无法正确获取页面可视区高度

    背景: 弹出层插件(自适应) 实现过程中突然发现在获取可视区高度时,无论document.documentElement.clientHeight 还是 $(window).height()都无法正确 ...

  8. windows 下的sleep 命令

    方法一 ping -n 3 127.0.0.1 > nul 其中3是需要sleep的秒数 方法二 timeout /t 3 /nobreak > nul 其中3是需要sleep的秒数

  9. C#中DataTable中的Compute方法使用收集

    原文: C#中DataTable中的Compute方法使用收集 Compute函数的参数就两个:Expression,和Filter. Expresstion是计算表达式,关于Expression的详 ...

  10. 在js中实现邮箱格式的验证

    在js中实现邮箱格式的验证 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><htm ...