-->
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: About multiple users ans db synchro
PostPosted: Tue Feb 14, 2012 4:28 am 
Newbie

Joined: Tue Feb 14, 2012 4:18 am
Posts: 2
Hi,

I am currently developping a Jee web platform. I am using Hibernate as ORM.

But, got a question about how hibernate works in the followung case (see scheme).

In database :
Got a Library table
Containing Book1 and Book2

Got two simultaneous users.
User1 and User2

User1 and 2 are both loading Library with a Set<Book> containing Book1 and Book2

User1 deletes Book1, and commit.
So, Book1 is deleted from database.

User2 edit Book1 and 2 and commit :

Exception executing batch:
org.hibernate.StaleStateException:
Batch update returned unexpected row count from update [0];
actual row count: 0; expected: 1

How should I write my app to manage with that kind of problems ? Do you guys have some nice doc about it ?

Following the scheme of te problem :

Image


Top
 Profile  
 
 Post subject: Re: About multiple users ans db synchro
PostPosted: Tue Feb 14, 2012 5:53 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
There are mainly to approaches to solve such problems:

1. Optimistic locking (Optimistic concurrency control)
2. Pessimistic locking

Quote:
How should I write my app to manage with that kind of problems ?


If you choose for Optimistic locking (most popular approach in hibernate), then your app should catch the org.hibernate.StaleStateException,
rollback the transaction, if it still active and inform the user that a concurrency conflict happened,
and that he has to redo the transaction from scratch with updated situation.
The appplication should manage the StaleStateException in a way that the user can redo his work without to much effort.

If you choose for Pessimistic locking, the your appplication must try to lock each Book instance before editing it.
If the lock action fails (because concerning book is already locked by another user), then your app should handle this by
advicing the user that concerning book is currently locked by another user.
N.B.: In this Pessimistic locking context it is of interest to establish whether your hibernate.dialect supports lock LockMode.UPGRADE_NOWAIT or not. If NOWAIT option is not properly supported, you have to wait until the other user releases the lock.
If the other user interacts with a GUI-interface, it could take long time until he terminates the transaction.


Find more informations about here
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/transactions.html#transactions-optimistic


Last edited by pb00067 on Tue Feb 14, 2012 9:36 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: About multiple users ans db synchro
PostPosted: Tue Feb 14, 2012 7:41 am 
Newbie

Joined: Tue Feb 14, 2012 4:18 am
Posts: 2
Thank you very much for your help.

Tortue 974.


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.