-->
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: Concurrency Problem in a web application using Hibernate
PostPosted: Wed Dec 13, 2006 9:36 am 
Beginner
Beginner

Joined: Fri Nov 18, 2005 5:49 am
Posts: 23
We have a web application built using J2EE platform. We are using Hibernate 3.0 for persisting the data in the backend which is Oracle.Our application comprises of web pages where in multiple users can edit and update simultaneously.

Scenario :
User A, takes a student information to update the address details
At the same time, User B takes the same student information to update the address details.

What is happening is , Hibernate is not throwing any error when User B tries to update. Finally whatever UserB edited information is stored in the database. UserA edited information is lost.

We are using version property in the hbm files.

Pl help .. This is very urgent....


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 9:52 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
Yes, this behaviour is typical for approach called Optimistic Locking. If there are many updates in your system, consider another approach - Pessimistic Locking, with this approach UserB must wait until UserA finishes its work and releases lock(Hibernate uses SELECT ... FOR UPDATE construction if underlying database supports this construction). You can do this in Hibernate via session.lock(LockMode.UPGRADE) call

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 3:48 pm 
Newbie

Joined: Tue Jul 25, 2006 6:40 pm
Posts: 6
Location: Plano, TX
Yes, Lester is correct. You need pesimistic locking.

(Of course, as with all concurrency issues, its best if you can design your application so that multiple users cannot update the exact same thing at the exact same time. A sound design principle is to avoid threading/concurrency issues wherever you can do so.)

I've actually been researching this for another situation. Check out the manual -- Chapter 14: Transactions and Concurrency
http://nhibernate.sourceforge.net/h2.0.3docs/reference/html/transactions.html

The SQL equivalent is select .... for update. You may want to consider the nowait option if your database supports it. Probably the easiest way to set it would be with a method call to the Query -- setLockMode(). You'd want the UPGRADE or UPGRADE_NOWAIT setting.

Though, I do note that the manual says,
Quote:
If the database does not support the requested lock mode, Hibernate will use an appropriate alternate mode (instead of throwing an exception). This ensures that applications will be portable.

Makes me wonder what it is that Hibernate does in that case -- wait until its available, I guess. Anyone know for sure?


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.