I recently launched a new site based on Struts, Hibernate and MySQL and immediately ran into a weird issue where Hibernate lost the ability to make database connections after a long period of inactivity. For the record, the stack trace is below:
java.sql.SQLException: Communication link failure: java.io.IOException
at org.gjt.mm.mysql.MysqlIO.sendCommand(Unknown Source)
at org.gjt.mm.mysql.MysqlIO.sqlQueryDirect(Unknown Source)
at org.gjt.mm.mysql.Connection.execSQL(Unknown Source)
at org.gjt.mm.mysql.PreparedStatement.executeQuery(Unknown Source)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:83)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:794)
at net.sf.hibernate.hql.QueryTranslator.iterate(QueryTranslator.java:846)
at net.sf.hibernate.impl.SessionImpl.iterate(SessionImpl.java:1540)
at net.sf.hibernate.impl.SessionImpl.iterate(SessionImpl.java:1513)
at net.sf.hibernate.impl.SessionImpl.iterate(SessionImpl.java:1505)
Looks like there are other people having the same problem, the first suggestion was to use the Thread Local Session pattern, which I already had in place. The solution was to add the following properties to my hibernate.cfg.xml:
<property name="connection.autoReconnect">true</property>
<property name="connection.autoReconnectForPools">true</property>
<property name="connection.is-connection-validation-required">true</property>
which I believe are specific to MySQL, but as far as I can tell, aren't documented anywhere on the Hibernate site (or should they be documented on the MySQL JDBC driver site?)
For what it's worth, Hibernate is a dream come true. I don't like writing create, update, and delete SQL statements and I've found that the software I've written is much easier to manage and troubleshoot. If you haven't played with it yet, check it out now.