Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure [duplicate]

Tags:

java

mysql

jdbc

My program that connects to a MySQL database was working fine. Then, without changing any code used to set up 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;
}
like image 959
Nick Heiner Avatar asked Jan 23 '10 02:01

Nick Heiner


8 Answers

This is a wrapped exception and not really interesting. It is the root cause of the exception which actually tells us something about the root cause. Please look a bit further in the stacktrace. The chance is big that you'll then face a SQLException: Connection refused or SQLException: Connection timed out.

If this is true in your case as well, then all the possible causes are:

  1. IP address or hostname in JDBC URL is wrong.
  2. Hostname in JDBC URL is not recognized by local DNS server.
  3. Port number is missing or wrong in JDBC URL.
  4. DB server is down.
  5. DB server doesn't accept TCP/IP connections.
  6. Something in between Java and DB is blocking connections, e.g. a firewall or proxy.

To solve the one or the either, follow the following advices:

  1. Verify and test them with ping.
  2. Refresh DNS or use IP address in JDBC URL instead.
  3. Verify it based on my.cnf of MySQL DB.
  4. Start it.
  5. Verify if mysqld is started without the --skip-networking option.
  6. Disable firewall and/or configure firewall/proxy to allow/forward the port.

By the way (and unrelated to the actual problem), you don't necessarily need to load the JDBC driver on every getConnection() call. Just only once during startup is enough.

like image 131
BalusC Avatar answered Oct 17 '22 07:10

BalusC


check your wait timeout set on the DB server. Some times it defaults to 10 seconds. This looses the connection in 10 seconds.

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

update it make it something like 28800

mysql> SET GLOBAL wait_timeout = 28800;
like image 30
sujit sathe Avatar answered Oct 17 '22 05:10

sujit sathe


I've been having this issue also for about 8-9 days. Here's some background: I'm developing a simple Java application that runs in bash.

Details:

  • Spring 2.5.6
  • Hibernate3.2.3.ga
  • With maven. (The base of the project is from mkyong.com , the spring tutorial without anotations )
  • 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 application works fine in Arch Linux, Mac OS X 10.6, and FreeBSD 7.2. When I moved the jar file to another arch linux in a different host, using the same mysql, a similar my.cnf, and the similar kernel version, the connection died and obtained the same error as the original poster:

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

I tried every possible combination for this that I found on so and the forums (http://forums.mysql.com/read.php?39,180347,180347#msg-180347 for example, which is closed now and I can't post .. ), specifically:

  • Triple check that I wasn't using skip networking. (verified with ps aux and the my.cnf)
  • Tried enable log_warnings=1 in the my.cnf but obviously, I wasn't hitting the server so I didn't saw anything while using the app
  • SHOW ENGINE innodb STATUS didn't show anything at all; during the tests I could connect via shell, and php also connected to the mysql server
  • /etc/hosts has localhost 127.0.0.1
  • Tried the jdbc properties using localhost and 127.0.0.1 with no results
  • Tried adding c3p0 and changed the max_wait
  • Max connections in the my.cnf was changed to 900 , 2000 and still nothing my.cnf
  • Added wait_timeout = 60 my.cnf
  • Added net_wait_timeout = 360 my.cnf
  • Added the destroy-method="close" spring.xml

As it was pointed out (if you look up for the same exception , you will find several so threads about the issue Reproduce com.mysql.jdbc.exceptions.jdbc4.CommunicationsException with a setup of Spring, hibernate and C3P0 for example ).

  1. If you are using tomcat, please check the security exception (again, it is on SO, you will find it )
  2. Check that you can resolve that url that you are using
  3. Try adding c3p0.
  4. Verify that there isn't a firewall rejecting your connections
  5. Finally , if you are using GNU/Linux ( ARch linux for example and you indeed obtain this exception ) Try MySQL Forums :: JDBC and Java :: EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost

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

I know that is a bit extense, but it may help anybody using GNU/Linux and having this exception and this thread seemed the best place to post my research.

Hope it helps

like image 20
Jorge Omar Vázquez Avatar answered Oct 17 '22 07:10

Jorge Omar Vázquez


I got the same error but then I figured it out its because the Mysql server is not running at that time.

So to change the status of the server

  1. Go to Task Manager
  2. Go to Services
  3. then search for your Mysql server(eg:for my case its MYSQL56)
  4. then you will see under the status column it says its not running
  5. by right clicking and select start

Hope this will help.

like image 44
Rahal Kanishka Avatar answered Oct 17 '22 06:10

Rahal Kanishka


We have a piece of software (webapp with Tomcat) using Apache commons connection pooling, and worked great for years. In the last month I had to update the libraries due to an old bug we were encountering. The bug had been fixed in a recent version.

Shortly after deploying this, we started getting exactly these messages. Out of the thousands of connections we'd get a day, a handful (under 10, usually) would get this error message. There was no real pattern, except they would sometimes cluster in little groups of 2 to 5.

I changed the options to on the pool to validate the connection every time one is taken from or put back in the pool (if one is found bad, a new one is generated instead) and the problem went away.

Have you updated your MySQL jar lately? It seems like there may be a new setting that didn't used to be there in our (admittedly very old) jar.

I agree with BalusC to try some other options on your config, such as those you're passing to MySQL (in addition to the connection timeout).

If this failure is transient like mine was, instead of permanent, then you could use a simple try/catch and a loop to keep trying until things succeed or use a connection pool to handle that detail for you.

Other random idea: I don't know what happens why you try to use a closed connection (which exception you get). Could you be accidentally closing the connection somewhere?

like image 24
MBCook Avatar answered Oct 17 '22 05:10

MBCook


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

like image 25
PorridgeBear Avatar answered Oct 17 '22 06:10

PorridgeBear


As BalusC mentioned, it would be very useful to post the full stacktrace (always post a full stacktrace, it is useless and frustrating to have only the first lines of a stacktrace).

Anyway, you mentioned that your code was working fine and that this problem started suddenly to occur without any code change so I'm wondering if this could be related to you other question Problem with not closing db connection while debugging? Actually, if this problem started while debugging, then I think it is (you ran out of connections). In that case, restart you database server (and follow the suggestions of the other question to avoid this situation).

like image 45
Pascal Thivent Avatar answered Oct 17 '22 06:10

Pascal Thivent


I encountered same problem. I am using spring & dbcp & mysql 5.5But If I change localhost to 192.168.1.110 then everything works. What make things more weird is mysql -h localhost just works fine.

update: Finally found a solution. Changing bindaddress to localhost or 127.0.0.1 in my.conf will fix the problem.

like image 23
LiuJian Avatar answered Oct 17 '22 06:10

LiuJian