title: How can I set a timeout on a Hibernate save() call with a MySQL backend?
body: <p>We ran into a problem in a large, Java, Hibernate-based system last week.  Our backend MySQL database (hosted on Amazon RDS) went unresponsive for 5-10 minutes (it would still accept connections, but due to hardware issues, its write throughput dropped to zero).  This piece of code:</p>
  
  <pre><code>getSession().save(entity); //session is an instance of org.hibernate.Session
  </code></pre>
  
  <p>Ended up hanging for about 8.5 minutes.  So clearly there's a need for some sort of timeout condition on this statement to make it fail in the case of my particular scenario.  I can't guarantee that I won't see a similar hardware issue in the future.</p>
  
  <p>I should mention that I'm still fairly new to Hibernate, so it's possible that I just don't understand some things like the association between using <code>save()</code> versus using Criteria, Transactions, etc.  So I've found the following:</p>
  
  <ul>
  <li><code>hibernate.c3p0.timeout</code> can be used to set connection timeouts on the C3P0 connection pool</li>
  <li><code>getSession().getTransaction().setTimeout(...)</code> can be used to timeout a transaction</li>
  <li><code>getSession().createQuery(...).setTimeout(...)</code> can be used to timeout a query</li>
  <li>I've seen the JPA 2 <code>javax.persistence.query.timeout</code>, but I'm not entirely sure it's what I want (I also don't think my Hibernate version is new enough)</li>
  </ul>
  
  <p>None of these seems like exactly what I want to do (except maybe the JPA 2 one).  This seems like it should be really simple.  Is there something I'm missing here?</p>
  
  <p>Thanks!</p>