Vatsala
Vatsala

Reputation: 917

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

Upvotes: 7

Views: 22231

Answers (4)

AissaDevLab
AissaDevLab

Reputation: 784

For me i downgrade Java SE Runtime Environment to 1.8.0_281 and it work fine.

Upvotes: 0

luisacevedo
luisacevedo

Reputation: 242

I solved my communication link failure with c3p0. I came across the blog below that explained that the org.hibernate version must be equal to the c3p0 version, so this is the pom configuration that made my day

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.6.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
     <artifactId>hibernate-entitymanager</artifactId>
     <version>4.3.1.Final</version>
 </dependency>
<dependency>
 <groupId>org.hibernate</groupId>
  <artifactId>hibernate-c3p0</artifactId>
 <version>4.3.6.Final</version>
</dependency>

  <dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
   <version>0.9.1.2</version>
</dependency>

like he explains, it was enough to add only one property to hibernate.cfg.xml to get c3p0 pooling

       <property name="hibernate.c3p0.min_size">10</property>

no more "communication link error" , "expected to read 5 bytes,read 0" after that. here is the link:https://howtodoinjava.com/hibernate/hibernate-c3p0-connection-pool-configuration-tutorial/

Upvotes: 0

mac
mac

Reputation: 647

Even I faced this error, I got it solved only after enabling autoReconnect=true in c3p0 config

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

Upvotes: 1

Sheng Chien
Sheng Chien

Reputation: 519

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.

Upvotes: 5

Related Questions