title: How to break a Hibernate session?
body: <p>In the Hibernate reference, it is stated several times that</p>
  
  <blockquote>
    <p>All exceptions thrown by Hibernate are fatal. This means you have to roll back
    the database transaction and close the current <code>Session</code>. You aren’t allowed to continue
    working with a <code>Session</code> that threw an exception.</p>
  </blockquote>
  
  <p>One of our legacy apps uses a single session to update/insert many records from files into a DB table. Each record update/insert is done in a separate transaction, which is then committed (or rolled back in case an error occurred). Then for the next record a new transaction is opened, and so on. But the same session is used throughout the whole process, even if a <code>HibernateException</code> was caught in the middle of processing. We are using Oracle 9i btw with Hibernate 3.24.sp1 on JBoss 4.2.</p>
  
  <p>Reading the above in the book, I realized that this design may fail. So I refactored the app to use a separate session for each record update. In a unit test with a mock session factory, I can verify that it is now requesting a new session for each record update. So far, so good.</p>
  
  <p>However, we found no way to reproduce the session failure while testing the whole app (would this be a stress test btw, or ...?). We thought of shutting down the listener of the DB but we realized that the app is keeping a bunch of connections open to the DB, and the listener would not affect those connections. (This is a web app, activated once every night by a scheduler, but it can also be activated via the browser.) Then we tried to kill some of those connections in the DB while the app was processing updates - this resulted in some failed updates, but then the app happily continued updating the rest of the records. Apparently Hibernate is clever enough to reopen broken connections under the hood without breaking the whole session.</p>
  
  <p>So this might not be a critical issue after all, as our app seems to be robust enough even in its original form. However, the issue keeps bugging me. I would like to know:</p>
  
  <ol>
  <li>Under what circumstances does the Hibernate session really become unusable after a <code>HibernateException</code> was thrown (<strong>Update:</strong> and what are the symptoms)?</li>
  <li>How to reproduce this in a test (<strong>Update:</strong> preferably in integration, rather than unit test)?</li>
  <li>(What's the proper term for such a test?)</li>
  </ol>