-->
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: Regarding Hibernate Session is not threadsafe
PostPosted: Mon Feb 18, 2013 7:34 am 
Newbie

Joined: Mon Feb 18, 2013 7:25 am
Posts: 1
Hi Guys,

I am a newbie into Hibernate development and as i am progressing i came to know that Hibernate sessions are not threadsafe that means 2 different requests can modify the same data at the same time which leads to inconsistancy. Please correct me if i am wrong with my assumption.

Now suppose i am working on a record to modify column 'A' and another request modified it too to 'B' so it is a incorrect thing for first user [It can be a situation like in a session a user booked a product and again in different session different user booked the same]

is there anyway to avoid such stuff ? Any help is appreciated :)

Regards
Rahul


Top
 Profile  
 
 Post subject: Re: Regarding Hibernate Session is not threadsafe
PostPosted: Mon Feb 18, 2013 8:35 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
Have a look at different locking strategies. I'd recommend to use the @Version-Annotation and an optimistic locking mechanism. Hibernate increments a version-field or a timestamp to recognize changes made by different sessions.

update xy set field1 = ? where id = ?;

becomes

update xy set field1 = ?, version = ? where id = ? and version = ?;

If another session changed data concurrently the second session will update no rows. As hibernate knows that there must be exactly ONE row it throws an exception you can handle. E.g. inform the user that the item is reserved for another user.


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.