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
java mysql jdbc hibernate
Vatsala
source share