-->
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.  [ 2 posts ] 
Author Message
 Post subject: LocalThread session problem...
PostPosted: Mon May 15, 2006 2:27 pm 
Newbie

Joined: Sat Aug 13, 2005 2:35 pm
Posts: 4
Hibernate version:3.1

Hello everybody...

I've started using hibernate in a web application that I'm developing, and I'm pretty worried about one thing:

I read in Hibernate in Action that it's recommended to instantiate a session and put it in a LocalThread in the HibernateUtil object, so when an object makes a request to HibernateUtil asking for a session, HibernateUtil verify if there's not already a session instantiate at the LocalThread: if there's, then the session that is at the LocalThread is returned; if there's not, then a new session is instantiate and allocated at the LocalThread. By doing this, my application creates a persitence context.

Well, until here it's alright. The problem happens when an object closes the session. Since the session is shared between all objects, it's possible that an object close the session, while there's another object using the session, and when the object that's using the session access any method of the session, like save(), the exception "org.hibernate.SessionException: Session is closed!" happens.

To solve this problem I've thought about creating a kind of control that does not allow an object to close a session, if there's some other object using the same session.

Althought it solves the problem, I don't think it's the right way to solve it. I'd be glad if anyone comment this solution.

Besides that, there's still another problem: since each session can have only a transaction by time, it's necessary to share the same transaction with all objects, and, by doing this, a similar problem happens: what if an object commit the shared transaction while an other object is still using it?

It can be avoided by creating a control similar to the one that I've described, but I don't think it's right that different objects share a transaction.

Well, I'd be sattisfied if someone could help me with this problem, cuz it's driving me nuts!!!

thanks forward,

Eduardo Zanoni Marques.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 15, 2006 3:20 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I'm not too keen on this ThreadLocal idea as far as webapps are concerned. I find it much better to create one SessionFactory, put it in the servlet context, and have it hand out sessions as they're needed. The sessions are closed as soon as they're done being used.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.