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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值