How to break a Hibernate session?

Posted by Péter Török on Stack Overflow See other posts from Stack Overflow or by Péter Török
Published on 2010-04-02T09:11:53Z Indexed on 2010/04/02 9:13 UTC
Read the original article Hit count: 369

Filed under:
|
|
|

In the Hibernate reference, it is stated several times that

All exceptions thrown by Hibernate are fatal. This means you have to roll back the database transaction and close the current Session. You aren’t allowed to continue working with a Session that threw an exception.

One of our legacy apps uses a single session to update/insert many records from files into a DB table. Each recourd update/insert is done in a separate transaction, which is then duly committed (or rolled back in case an error occurred). Then for the next record a new transaction is opened etc. But the same session is used throughout the whole process, even if a HibernateException was caught in the middle. We are using Oracle 9i btw with Hibernate 3.24.sp1 on JBoss 4.2.

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 could prove that it is now requesting a new session for each record update. So far, so good.

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. Apparently Hibernate is clever enough to reopen broken connections under the hood without breaking the whole session.

So this might not be a critical issue, as our app seems to be robust enough even in its original form. However, the issue keeps bugging me. I would like to know:

  1. Under what circumstances does the Hibernate session really become unusable after a HibernateException was thrown?
  2. How to reproduce this in a test?
  3. (What's the proper term for such a test?)

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about session