aspose file tools*
The moose likes Object Relational Mapping and the fly likes recovery from hibernate session closed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "recovery from hibernate session closed" Watch "recovery from hibernate session closed" New topic
Author

recovery from hibernate session closed

Skip Cole
Ranch Hand

Joined: Jan 05, 2001
Posts: 175
Hi,

I have some very, very ugly code for a web application. Its going to get re-done, but in the mean while I want to be able to show people what the tool is doing. (This is like a prototype.)

Anyway, I wanted to start using Hibernate for this tool. And now that I have gotten a good book on Hibernate I have been making great progress.

But now I have run into get this error:

org.hibernate.SessionException: Session is closed!

I think I could trap this error, and re-open a session, but I'm not sure what the best way to do this is. Has anyone run up against this? Is there are good way around it?

Thanks in advance,
Skip
PS. Below is the full stack trace.


org.hibernate.SessionException: Session is closed!
org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1621)
sun.reflect.GeneratedMethodAccessor47.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
$Proxy15.createQuery(Unknown Source)
org.usip.oscw.baseobjects.SimulationSection.getBySimAndActorAndPhase(SimulationSection.java:181)
org.apache.jsp.ver1.simulation.frame_005ftop_jsp._jspService(frame_005ftop_jsp.java:180)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


If you love me, you will visit docs.opensimplatform.org
(FYI, Getting it tattooed on is a bit much.)
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

"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."


-How Do I Handle Exceptions in Hibernate and JPA Web Applications?


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.

Open Session in View Hibernate/Transaction Design Pattern/Practice

Hibernate Transactions and Custom Filter Interceptors

Post back and let's figure this thing out!

-Cameron McKenzie
[ May 12, 2008: Message edited by: Cameron Wallace McKenzie ]
Skip Cole
Ranch Hand

Joined: Jan 05, 2001
Posts: 175
Thanks Cameron!

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."

Best,
Skip
Skip Cole
Ranch Hand

Joined: Jan 05, 2001
Posts: 175
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...�
Skip Cole
Ranch Hand

Joined: Jan 05, 2001
Posts: 175
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 :-)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: recovery from hibernate session closed