5

My Java application, which uses Hibernate and it's hosted by Tomcat 6.0, gets the following exception after a long time of inactivity when it tries to access the DB:

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

** BEGIN NESTED EXCEPTION ** 

java.io.EOFException

STACKTRACE:

java.io.EOFException
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1963)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2375)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2874)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3249)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1403)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1812)
    at org.hibernate.loader.Loader.doQuery(Loader.java:697)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    at org.hibernate.loader.Loader.doList(Loader.java:2232)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
    at org.hibernate.loader.Loader.list(Loader.java:2124)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)

Any tips?

Thanks

3
  • well judging by the error, what this page has to say java.sun.com/j2se/1.4.2/docs/api/java/io/EOFException.html and the lengthy time-out it would seem that for whatever reason your app / hibernate is unable to connect to your db. Check the db url, port, and login details. Commented May 29, 2010 at 11:21
  • It seems that Hibernate is not able to reconnect to the db when the connection expires. Because first it works, then after a long inactivity the exception is thrown. Commented May 29, 2010 at 11:26
  • ah ok this is a configuration option. you can get jdbc to reconnect but off top head don't know answer, try a google google.com/search?hl=en&q=jdbc+hibernate+reconnect Commented May 29, 2010 at 11:50

1 Answer 1

3

MySQL will close idle connections after 8 hours by default (the wait_timeout). This is a "famous problem" and you'll find many references of it on the internet, for example this one. So either:

  • configure tomcat to test connections on borrow using a validationQuery in the datasource configuration:

    <parameter>
      <name>validationQuery</name>
      <value>select 1</value>
    </parameter>
    
  • increase MySQL's wait_timeout via my.cnf/my.ini, or by connecting with a command-line SQL client and entering SET GLOBAL wait_timeout=86400, or some other suitable number of seconds.

Note that I'm not aware of all consequences of the second option. If you decide to go the second option way, I'd suggest posting another question to get feedback from MySQL experts.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.