Hibernate: Communication link failure: java.io.IOException
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.
Tags: hibernate






