This question already has an answer here:
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;
}
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
orSQLException: Connection timed out
.If this is true in your case as well, then all the possible causes are:
To solve the one or the either, follow the following advices:
ping
.my.cnf
of MySQL DB.--skip-networking
option.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.The escential problem is that Mysql JDBC pool connections is not used, then the Timeout from Mysql, close the Connections. You need change the pool Parameters to get restart connection when the connection has failures, on this way:
Connection Validation
: Required (Check)Validation Method:
autocommitYou can change the Validation Method if you cannot get it works!
If you use WAMP, make sure it is online. What I did was, first turned my firewall off, then it worked, so after that I allowed connection for all local ports, specially port 80. Than I got rid of this problem. For me it was the Firewall who was blocking the connection.
Ensure skip-networking is commented out in my.cnf/my.ini
check your wait timeout set on the DB server. Some times it defaults to 10 seconds. This looses the connection in 10 seconds.
update it make it something like 28800
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).