-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: When do I have to discard the session and make a new one ?
PostPosted: Mon Jul 05, 2004 11:46 am 
Beginner
Beginner

Joined: Wed Feb 04, 2004 5:21 pm
Posts: 37
I am reviewing my Hibernate code, and am getting quite tired of writing the same sort of
close-session-get-another-one-in-case of exception.

There are several situations where I assume that the Session instance remains intact :
1) Attempting to load a record that doesnt exist
2) Attempting to save a record that already exists
3) Attempting to delete a non existent record

Is there any problem in using the session after one of these occurs ?
Is it possible to check HibernateException to see if such non-fatal issue caused it ?

And, is it possible to leave the session anyway, and check to session later to see if it needs to be replaced with a fresh one ?

Cheers,

Henrik


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 05, 2004 8:33 pm 
Beginner
Beginner

Joined: Wed Feb 04, 2004 5:21 pm
Posts: 37
in addition,

when cleaning up according to the ThreadLocal pattern under j2ee servlets. Currently I close the current session after any given request using a filter.

That will only clean up sessions of the current thread. Isn't it possible for the server to loose connection and therefore not respond, throw an exception going past the reminder of the filter, and never clean up the session in that thread. I know it would be rare, but I suppose that would be a memory leak.

fix: try { chain.doFilter.. } finally { close current session }


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 05, 2004 8:39 pm 
Regular
Regular

Joined: Tue Sep 30, 2003 11:27 am
Posts: 60
Location: Columbus, OH, USA
Good point, and I believe that Jeff Schnitzer's Servlet Filter approach is pretty consistent about this. Check out http://www.hibernate.org/43.html for more info.
Scott


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 06, 2004 7:24 am 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
I think ALL hibernate exceptions are considered fatal and the session should always be trashed after catching one. It may be that the current implementation means certain errors like the ones you mention are survivable but there is no guarantee that future implementation changes won't break the functionality you are relying on now.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 06, 2004 8:45 am 
Beginner
Beginner

Joined: Wed Feb 04, 2004 5:21 pm
Posts: 37
If so, I think there would be a good argument for either of:

1) Flagging the session object as invalid or automatically close it if it is no longer usable
2) Flagging the exception, so the catch may check
3) Having specialized versions of HibernateException for the non-fatal exceptions, so one may catch those and be less agressive in killing the session

I imagine that these are trivial improvement of Hibernate

Henrik


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 06, 2004 8:57 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
ObjectNotFoundException and StaleObjectStateException are recoverable.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: FYI regarding Christian's answer above
PostPosted: Fri Nov 17, 2006 3:34 pm 
Newbie

Joined: Tue Jul 12, 2005 11:31 am
Posts: 4
Location: Nashville, TN USA
In case anyone stumbles onto this older thread, more recent Hibernate lore says to throw away the session when any HibernateException occurs. One example:

http://forum.hibernate.org/viewtopic.php?t=967230&start=0&postdays=0&postorder=asc


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.