In the onMessage() method of a Message-Driven Bean there is code that uses a HibernateUtil class to get a reference to a SessionFactory. The code for creating the SessionFacotry is implemented in a static initialization block in the HibernateUtil class.
This static initialization block executes when the class is loaded in the application server's JVM. And the class is loaded when the code is deployed. Is this correct?
The problem we are facing is that when a series of messages are put on the queue, we are gettting a stale connection to the database and one record fails to be processed. In other words, a series of MDBs are called by the container and they all use this HibernateUtil class to write to the database. It is usually the first record processed, but sometimes the second or third that fails.
How to make sure that this HibernateUtil will always have fresh connections or a SessionFactory with a live database connection is the issue, I think?