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