Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Communications Link Failure in Hibernate Based Java Servlet application powered by MySQL

Let me describe my question -

I have a Java application - Hibernate as the DB interfacing layer over MySQL. I get the communications link failure error in my application. The occurence of this error is a very specific case. I get this error , When I leave mysql server unattended for more than approximately 6 hours (i.e. when there are no queries issued to MySQL for more than approximately 6 hours). I am pasting a top 'exception' level description below, and adding a pastebin link for a detailed stacktrace description.

javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Cannot open connection - Caused by: org.hibernate.exception.JDBCConnectionException: Cannot open connection - Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure - The last packet successfully received from the server was 1,274,868,181,212 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago. - Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure - The last packet successfully received from the server was 1,274,868,181,212 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago. - Caused by: java.net.ConnectException: Connection refused: connect

the link to the pastebin for further investigation - http://pastebin.com/4KujAmgD

What I understand from these exception statements is that MySQL is refusing to take in any connections after a period of idle/nil activity. I have been reading up a bit about this via google search, and came to know that one of the possible ways to overcome this is to set values for c3p0 properties as c3p0 comes bundled with Hibernate. Specifically, I read from here http://www.mchange.com/projects/c3p0/index.html that setting two properties idleConnectionTestPeriod and preferredTestQuery will solve this for me. But these values dont seem to have had an effect.

Is this the correct approach to fixing this? If not, what is the right way to get over this?

The following are related Communications Link Failure questions at stackoverflow.com, but I've not found a satisfactory answer in their answers. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure How to handle: Communication link failure

Note 1 - i dont get this error when I am using my application continuosly. Note 2 - I use JPA with Hibernate and hence my hibernate.dialect,etc hibernate properties reside within the persistence.xml in the META-INF folder (does that prevent the c3p0 properties from working?)

edit - the c3p0 parameters I tried out are in the comments

like image 299
Vatsala Avatar asked May 26 '10 11:05

Vatsala


1 Answers

I had issue before. It looks like MySQL server times out your connection. Timeout is 28800 by default which is 8 hours. Please refer to this link for more details.

http://shengchien.blogspot.com/2009/10/hibernate-c3p0-and-mysql.html

Hope it is useful to you.

like image 170
Sheng Chien Avatar answered Oct 27 '22 15:10

Sheng Chien