Hibernate Communications communication error in Java Servlet application based on MySQL based Hibernate

Let me describe my question -

I have a Java application - Hibernate as an interface for interacting with a database over MySQL. I get a communication failure error in my application. The occurrence of this error is a very specific case. I get this error. When I leave the mysql server unattended for more than 6 hours (i.e. when MySQL queries are not required for more than 6 hours). I insert a top-level description of an β€œexception” below and add a pastebin link for a detailed description of stacktrace.

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

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

What I understand from these exception statements is that MySQL refuses to accept any connections after a period of inactivity activity / zero. I read about it a bit through google search and found out that one of the possible ways to overcome this is to set values ​​for c3p0 properties, since c3p0 comes bundled with Hibernate. In particular, I read from here http://www.mchange.com/projects/c3p0/index.html , which sets the two properties idleConnectionTestPeriod and preferredTestQuery will allow this for me. But these values ​​do not seem to have affected.

Is this the right approach to fix this? If not, what is the right way to overcome this?

The following are related questions about disconnecting at stackoverflow.com, but I did not find a satisfactory answer in their answers. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communication error with the channel How to handle: Communication communication error

Note 1 - I do not get this error when I use my application constantly. Note 2 - I use JPA with Hibernate and therefore my hibernate.dialect properties, etc. They are located in the persistence.xml file in the META-INF folder (does this prevent the c3p0 properties from working?)

change - c3p0 parameters that I tried are in the comments

+7
java mysql jdbc hibernate
source share
3 answers

I had a problem before. It looks like the MySQL server is disconnecting your connection. The default wait time of 28800 is 8 hours. For more information, refer to this link.

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

I hope this is useful to you.

+5
source share
+3
source share

Even if I encountered this error, I decided to solve it only after turning on autoReconnect = true in the c3p0 configuration

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ex_app?autoReconnect=true</property> 
+1
source share

All Articles