-->
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.  [ 3 posts ] 
Author Message
 Post subject: Session thread safety question
PostPosted: Tue Jun 12, 2007 11:33 am 
Newbie

Joined: Tue Jun 12, 2007 11:16 am
Posts: 5
I'm fitting hibernate into an existing webapp, and I'm forced to use a long-lived hibernate session (that is stored in the http session) that I flush and disconnect at the end of each http request:

Code:
session.flush();
tx.commit();
tx = session.beginTransaction();


The thing I'm worried about is the thread safety of the hibernate session object. (ie. simultaneous http requests from the same user that come in on different threads)

I can easily put a sync block around the code that explictly references the session object, but is that enough? Is the session object also used when an object is modified, or lazy fetching occurs?

-Trevor


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 12, 2007 2:10 pm 
Newbie

Joined: Fri Oct 21, 2005 9:27 am
Posts: 13
You surely misuse Hibernate but it depends on what you want to accomplish. Maybe the "Open Session In View" pattern is what you want to use.

But for your specific question, lazily loading an object implies it's bound to a session, otherwise you'll have an exception (it's a normal behaviour : if the object is not bound to a session, it means that it's not under hibernate's control, thus you cannot fetch anything more than what has been fetched before).

The case of the "modified object" results in a misunderstanding. You can modify your object either if it's bound with a session or not. But the results will then vary. If it's bound to a session, the object is again under the control of hibernate, and hibernate will reflect the changes - depending on mapping. If it's not bound to a session, you'll lose synchronization with database due to the fact that you changed your object and didn't notice hibernate about it.

I hope it helps.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 12, 2007 2:34 pm 
Newbie

Joined: Tue Jun 12, 2007 11:16 am
Posts: 5
bdusauso wrote:
You surely misuse Hibernate but it depends on what you want to accomplish. Maybe the "Open Session In View" pattern is what you want to use.

Yes, I realize that I'm not using the best hibernate pattern, but it is one that is discussed quite a bit in the docs.

bdusauso wrote:
But for your specific question, lazily loading an object implies it's bound to a session, otherwise you'll have an exception (it's a normal behaviour : if the object is not bound to a session, it means that it's not under hibernate's control, thus you cannot fetch anything more than what has been fetched before).

I'm not seeing any information regarding the thread safety of a session in your reply. Maybe I'm dense.

I'm aware that I need to have an object bound to a session before anything can happen. I'm using session-per-conversation, where the session isn't closed between requests... the transaction is just committed and a new one is started for the next request.

From the hibernate manual, section 11.1.4:

Quote:
• A Session is not thread-safe. Things which are supposed to work concurrently, like HTTP requests, session beans, or Swing workers, will cause race conditions if a Session instance would be shared. If you keep
your Hibernate Session in your HttpSession (discussed later), you should consider synchronizing access to your Http session. Otherwise, a user that clicks reload fast enough may use the same Session in two concurrently running threads.


As in my original post, I'm wondering if the hibernate session object gets involved even when not explictly referenced, ie. by modifying an object, or accessing an object's property that causes some lazy loading to occur.

The paragraph from the manual just suggests taking a shotgun to the problem. I'm wondering if I can be a little more fine grained about it.

-Trevor


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