Com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: communication failure

My program that connects to the MySQL database works fine. Then, without changing the code used to configure the connection, I get this exception:

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. 

What happened?

The code used to get the connection:

 private static Connection getDBConnection() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException { String username = "user"; String password = "pass"; String url = "jdbc:mysql://www.domain.com:3306/dbName?connectTimeout=3000"; Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection(url, username, password); return conn; } 
+42
java mysql jdbc
Jan 23 '10 at 2:27
source share
15 answers

This is a wrapped exception and not very interesting. This is the reason for the root exception, which actually tells us something about the root cause. Take a look at stacktrace for a little more. The chance is great, and you are faced with SQLException: Connection refused or SQLException: Connection timed out .

If this is also true in your case, then all possible reasons:

  • Invalid IP or hostname in JDBC URL.
  • The host name in the JDBC URL is not recognized by the local DNS server.
  • The port number is missing or invalid in the JDBC URL.
  • The DB server is down.
  • The DB server does not accept TCP / IP connections.
  • Something between Java and DB blocks connections, for example. firewall or proxy.

To solve one or the other, follow these tips:

  • Verify and verify them using ping .
  • Update DNS or use the IP address in the JDBC URL.
  • Check it out based on my.cnf MySQL database.
  • Run it.
  • Make sure mysqld is started without the --skip-networking option.
  • Disable the firewall and / or configure the firewall / proxy to allow / forward the port.

By the way (and not related to the real problem), you don’t have to download the JDBC driver for every getConnection() call. Only once during startup is enough.

+37
Jan 23 '10 at 3:25
source share

set the wait timeout on the database server. Several times, it defaults to 10 seconds. This will lose connection in 10 seconds.

 mysql> show global variables like '%time%' ; 

update it, do something like 28800

 mysql> SET GLOBAL wait_timeout = 28800; 
+11
Apr 27 2018-12-12T00:
source share

I also have this problem for about 8-9 days. Here are some prerequisites: I am developing a simple Java application that runs in bash.

More details:

  • Spring 2.5.6
  • Hibernate3.2.3.ga
  • With maven. (The basis of the project is mkyong.com, a spring tutorial without annotations).
  • MySQL version:
  [jvazquez @ archbox ~] $ mysql --version
 mysql Ver 14.14 Distrib 5.5.9, for Linux (i686) using readline 5.1
 Linux archbox 2.6.37-ARCH # 1 SMP PREEMPT Fri Feb 18 16:58:42 UTC 2011 i686 Intel (R) Core (TM) 2 Quad CPU Q8200 @ 2.33GHz GenuineIntel GNU / Linux 

The app works great on Arch Linux, Mac OS X 10.6, and FreeBSD 7.2. When I moved the jar file to another linux arch on another host using the same mysql, similar to my.cnf and the same kernel version, the connection died and received the same error as the original poster:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communication link error

I tried all possible combinations for this that I found on this and on the forums ( http://forums.mysql.com/read.php?39,180347,180347#msg-180347 , for example, which is closed now and I can’t publish ..), in particular:

  • Triple check that I did not use skip networks. (checked using ps aux and my.cnf)
  • I tried to include log_warnings = 1 in my.cnf, but obviously I didn’t hit the server, so I didn’t see anything when using the application
  • SHOW ENGINE innodb STATUS did not show anything; during the tests, I could connect through the shell, and php also connected to the mysql server
  • / etc / hosts has localhost 127.0.0.1
  • Tried jdbc properties using localhost and 127.0.0.1 without any results.
  • Tried to add c3p0 and changed max_wait
  • The maximum number of connections in my.cnf has been changed to 900, 2000 and still nothing my.cnf
  • Added wait_timeout = 60 my.cnf
  • Added net_wait_timeout = 360 my.cnf
  • Destroy-method = "close" spring.xml added

As indicated (if you are looking for the same exception, you will find several similar topics about the issue Play com.mysql.jdbc.exceptions.jdbc4.CommunicationsException with Spring setting, sleep mode and C3P0 for example).

If the link is removed, just add mysqld: ALL to /etc/hosts.allow

I know this is a little advanced , but it can help anyone who uses GNU / Linux and has this exception, and this thread seemed to be the best place to post my research.

Hope this helps

+4
Feb 27 '11 at 6:42
source share

I got the same error but then I realized, because the Mysql server was not working at that time.

So, to change the server status

  • Go to Task Manager
  • Go to Services
  • then search your Mysql server (for example: for my case MYSQL56)
  • then in the status column you will see that it is not working.
  • right click and select start

Hope this helps.

+4
Oct. 16 '15 at 4:24
source share

We have a piece of software (webapp with Tomcat) that uses the Apache commons connection pool and has worked great for years. Last month I had to update the libraries due to an old error that we encountered. The bug was fixed in the latest version.

Shortly after deployment, we began to receive these messages. Of the thousands of connections, we would receive one day, several (up to 10, usually) would receive this error message. There was no real picture, except that sometimes they were grouped into small groups of 2-5.

I changed the settings in the pool to check the connection every time it was taken or put back into the pool (if it is found poorly, a new one is created instead), and the problem disappeared.

Have you updated your MySQL drum recently? It seems that there may be a new setting that was not used in our (albeit very old) bank.

I agree with BalusC to try other parameters in your config, for example, those that you switch to MySQL (in addition to the connection timeout).

If this failure is temporary, like mine, and not permanent, then you can use a simple try / catch and loop to continue trying until everything succeeds or uses the connection pool to handle this part for you.

Another random idea: I do not know what is happening, why you are trying to use a closed connection (which exception you get). Could you accidentally close the connection somewhere?

+3
Jan 23
source share

Make sure skip-networking is commented out in my.cnf / my.ini

+3
May 04 '12 at 11:34 p.m.
source share

As BalusC noted, it would be very useful to publish a stack with a full stacktrace (always popping a full stack is useless and frustrating the presence of only the first lines of stacktrace).

In any case, you mentioned that your code worked fine, and this problem suddenly occurred without changing the code, so I wonder if this could be related to you by another question. Problem with not closing the db connection during debugging? In fact, if this problem started during debugging, then I think it is (you have run out of connections). In this case, restart the database server (and follow the recommendations of another question to avoid this situation).

+2
Jan 23 '10 at 6:16
source share

I ran into the same problem. I am using spring and dbcp and mysql 5.5But. If I change localhost to 192.168.1.110 then everything will work. Which makes things weirder mysql -h localhost just works fine.

update: Finally found a solution. Changing the binding to localhost or 127.0.0.1 in my.conf fixes the problem.

+1
Apr 25 '14 at 8:49
source share

In my case, the local loopback interface did not start, so "localhost" could not be resolved. You can verify this by running ifconfig and you will see the lo interface. If it is not inserted, you can activate it by running "ifup lo" or "ifconfig lo up".

0
Jan 29 '13 at 6:27
source share

In my case, the loaded mysql.com Connector / J 5.1.29.jar had this error , whereas 5.1.29.jar loaded from the MvnRepository did not .

This happened when creating the Google appengine application in Android Studio (gradle, Windows x64), communicating with the Linux MySQL server on the local network / local virtual machine.

0
Mar 26 '14 at 5:35
source share

I see that you are connecting to a remote host. Now the question is, what type of network are you using to connect to the Internet?

WINDOWS

If it is a mobile broadband device, then get the IP address of your machine and add it to your hosting server so that your host server can allow connections coming from your computer. [your host could have disabled this for security reasons]. Please note that each time you use a different network device, your IP address changes.

If you are using a local area network, set a static IP address on your computer, and then add it to your computer.

Hope this helps! :)

0
Aug 6 '14 at 8:20
source share

I got a communication failure error while using java.sql.PreparedStatement with a specific statement.

This was done against MySQL 5.6, Tomcat 7.0.29, and JDK 1.7.0_67 on a Windows 7 x64 computer.

The reason was for setting an integer to a string parameter and a string to an integer parameter, and then trying to execute executeQuery in the prepared statement. After I corrected the order of setting the parameters, the operator did the right job.

This had nothing to do with network problems as the wording of the error message was suggested.

0
Nov 20 '14 at 20:07
source share

The problem is that the Mysql JDBC pool connections are not used, then Timeout from Mysql closes the connections. You need to change the pool settings to get the connection to restart when the connection fails, as follows:

Connection Validation : Required (Check)
Validation Method: autocommit

You can change the verification method if you cannot make it work!

0
Mar 19 '15 at 17:56
source share

If you are using WAMP, make sure it is connected to the network. What I did was first turn off the firewall, then it worked, so after that I allowed the connection for all local ports, especially to port 80. How did I get rid of this problem. For me, it was a firewall that blocked the connection.

0
Jul 13 '15 at 12:19
source share

I had the same problem and I used most of the parameters (autoreconnect, etc.), but did not try (test_on_idle or test_on_connect), I am going to make them further.

However, I had this hack that made me go through this:

I have a cron job called Healthcheck, it wakes up every 10 minutes and calls a REST API call to the server. The web / app server selects this, connects to db, makes a small change and returns with “yes, everything is quiet on the western front” or “shitshappening”. When the latter, he sends a pager / email to the right people.

This has a side effect, always supporting the db connection pool. While this cron is working, I have no problem with the db connection timeout. otherwise they arise.

0
Jul 13 '16 at 17:43
source share



All Articles