title: How to set timeout on a SQL statement with Hibernate
body: <p>What I have tried...</p>
  
  <p>Using Criteria:</p>
  
  <pre><code>criteria.setTimeout(1);
  </code></pre>
  
  <p>=> Zero effect</p>
  
  <p>Using HQL:</p>
  
  <pre><code>return getSession().createQuery("from User").setTimeout(1).list();
  </code></pre>
  
  <p>=> Zero effect</p>
  
  <p>Even tried it on transaction level:</p>
  
  <pre><code>@Transactional(timeout = 1, readOnly = true, propagation = Propagation.REQUIRES_NEW)
  </code></pre>
  
  <p>Some effect; after the query is executed an exception is thrown but the transaction is not aborted after 1 second.</p>
  
  <p>This is running on a Jetty server with Atomikos TM.</p>
  
  <p>How can I specify a timeout on a specific query so that the call is aborted after that timeout (which will not be 1 second, this is just to test quicker)?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>When running the same test but bypassing Atomikos/Hibernate/Spring (so using plain JDBC) the timeout does work (approximately at least; it will not be exactly 1 second, on H2 it came really close, on Oracle this was sometimes over 2 seconds). The result was as expected: <em>SQLException: ORA-01013: user requested cancel of current operation</em>. </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>When debugging hibernate I see that the timeout is set on the PreparedStatement but it seems to have no effect.</p>