This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Hibernate, MySQL and the EOFException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate, MySQL and the EOFException" Watch "Hibernate, MySQL and the EOFException" New topic

Hibernate, MySQL and the EOFException

Oliver Hanka

Joined: May 08, 2007
Posts: 6
Hi folks,

I am encountering the old problem with MySQL and Hibernate. Whenever I deploy an application, it works fine in the beginning. After a while of inactivity, however, I get the infamous EOFException. Here is the part of the stack trace:

root cause

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:


at com.mysql.jdbc.MysqlIO.readFully(
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(
at com.mysql.jdbc.MysqlIO.checkErrorPacket(
at com.mysql.jdbc.MysqlIO.sendCommand(
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(
at com.mysql.jdbc.Connection.execSQL(

I know, this problem has been discussed quite often in different message-boards and on various maillinglist. Unfortunatly I wasn't able the figure out a solution which worked for me. Quite often, tipps given are contrary or do not help at all.

What I figured out so far: It seems that the problem occures because MySQL drops inactive connections while the connection pool doesn't. It is said, that the default value for MySQL dropping these is around 8 hours. I tried to address this issue by telling the connection pool to close inactive connections after 60 minutes. This, however, didn't solve the problem either.

I am using the c3p0 connection pool instead of Hibernates own one. Here is my hibernate.cfg.xml:

This is the file within the root of my classpath:

Did someone encounter the same problem? Did someone even solve it? (actually I guess so, since there must be people out there using Hibernate with MySQL)

Any suggestions what to change/add?

These are the software versions I am using:
Hibernate: 3.2.2
MySQL: 5.0.32
JVM Version: 1.5.0_10-b03
[ May 09, 2007: Message edited by: Oliver Hanka ]
Roger Diller

Joined: May 18, 2007
Posts: 1
I had this problem with my application as well. What I did was setup a timer (javax.swing.Timer) that about every 7 hours regains the connection. All you gotta do is setup up the timer and then add a listener to the timer that makes the call to reset the connection. I have my listener code below.

private class TimerHandler implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
dbConnection = getDBConnection();
System.out.println("Regained connection...");

I think the MySQL default timeout period is exactly 8 hours so if you simply make a call to regain the connection every 7 hours or so it should take care of the problem. You can change the MySQL timeout period in the configuration file to however long you want to but you can't disable timing out to my knowledge.
I hope this helps... I'm sure there other solutions to this problem as well.
Oliver Hanka

Joined: May 08, 2007
Posts: 6
Thanks's Roger!

What I have figured so far is, there also might be a problem with the way the application opens and closes connections. I added:


into (requires c3p0 0.9.1 pre12) and that's what I found in the logs:

12:50:26,719 INFO RendererUtils:789 - Unable to find component 'msg' (calling findComponent on component '_idJsp0:_idJsp9'). We'll try to return a guessed client-id anyways - this will be a problem if you put the referenced component into a different naming-container. If this is the case, you can always use the full client-id.
13:02:28,888 INFO BasicResourcePool:1392 - A checked-out resource is overdue, and will be destroyed: com.mchange.v2.c3p0.impl.NewPooledConnection@1b158db
13:03:48,210 INFO BasicResourcePool:1392 - A checked-out resource is overdue, and will be destroyed: com.mchange.v2.c3p0.impl.NewPooledConnection@1e779a7
16:39:14,614 WARN JDBCExceptionReporter:77 - SQL Error: 0, SQLState: 08003

(Note: There was no request between 12:50 and 4:39)

I started the application and requested it to do a query. No update or insert. Here is my code:

I also tried to add a "session.getTransaction().commit()" to close the session. With this, however, I wasn't able to retrieve values from a one-to-many association.
I agree. Here's the link:
subject: Hibernate, MySQL and the EOFException
Similar Threads
Hibernate and MySQL connection issues
com.mysql.jdbc.CommunicationsException MESSAGE: Communications link failure due to underlying except
c3p0 configuration
Problems to connect to mysql with hibernate
Cannot get Hibernate 3.1 to work with c3p0