"Clearly, whenever an exception happens, the database transaction has to be rolled back. Another rule in Hibernate is that the current Session has to be closed and discarded immediately, it can't be re-used. Hence, this is all you have to do to handle exceptions from a Hibernate perspective. Of course you might want to retry some unit of work if it failed, or you might want to display custom errors. All of this is outside of the scope of Hibernate and can be implemented in any way you like."
So, you're doing a web application, and you're running into a closed session. So, we can assume the session was open at one point, and somewhere along the line, it got closed.
Why did the session get closed? When a session gets closed, that might indicate a full unit of work is completed, and as such, you will need to open a new session. But was the session closed due to a HibernateException?
Since most web applications have very short request-response cycles, often a problem with the Hibernate Session can be seen as unrecoverable, at least for that particular request and response cycle. It's often sufficient to send a message back to the user to say the operation failed, and ask them to resubmit their form. Or, if there is something in particular that was on the form that triggered the error, you can ask them to make a change.
If you are closing the session yourself, and it's not an error, you might want to think about extending the life of the session. A very, very common pattern is to open a Hibernate Session transaction as soon as a request comes in. Then, when all the processing is done, from the Servlet to the final JSP, the transaction is closed. This can be achieved by opening and closing the Hibernate Session in a ServletFilter. And this doesn't tie you to Servlets and JSPs, as you can wrap a filter around a struts action or a JSF bean.
You can read more about this by digging through documentation on the "Open Session in View" design practice. Here's a few links from Hibernate.org that might help out a bit. The link about Transactions is very detailed and interesting.
I think the problem may be that I'm doing to many database hits all at the same time (from a top frame, from a bottom frame, from another servlet, etc.). As I mentioned, the code I have is now just plain ugly. I like to call it Model-Ech! I plan on eventually putting it into Model-2, but maybe I'll have to do that sooner than I had planned.
(The Meta-problem here stem from the facts that 1. I'm an old dog that is not-to-fast to learn, 2. I need to learn to do this right, and 3. time is the one thing I do not have.)
I'll be doing the reading you mentioned. The book that I have is, of course, the one you wrote "Hibernate Made Easy." It is excellent! If it had not been for your Chapter 7, I'd still be beating my head against the wall. Now I need to step back and read it cover to cover.
I'll write back here when I have pushed past this, but in the end, the answer may be just going completely around it. My predicament may be like that of the fellow in this ancient story:
A guy goes to the doctor and says "Doctor, It hurts when I do this!" He then bends his body to show what hurts. The Doctor watches him carefully and then replies "Don't do that."
Joined: Jan 05, 2001
Well I thought I had gotten around this problem by caching some information in the servlet context - which I think is a good idea anyway.
But the problem has returned. I'm just going to have to step back and try to do this thing the right way. But time is not on my side.
"Once more unto the breach, dear friends...�
Joined: Jan 05, 2001
What have I learned today?
Never, ever, ever, never under any circumstances put a hibernate persisted object into your session.
I believe I have finally pushed past this problem. Now on to new ones :-)