-->
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.  [ 10 posts ] 
Author Message
 Post subject: Hibernate's Pessimistic Locking
PostPosted: Fri Dec 07, 2007 1:33 am 
Newbie

Joined: Wed Sep 19, 2007 11:30 pm
Posts: 12
Location: indonesia
Hi All,

I'm having a serious concurrency problem with hibernate.
I'm writing a program which need to update a row of customer's balance everytime the customer made a transaction.

When 2 transactions come together, this will happened:
1. Transaction 1 retrieve the last state of customer balance (say it A)
2. Transaction 1 updated the balance to B
3. Transaction 2 retrieve the last state of customer balance (it's B)
4. Transaction 2 updated the balance to C

While in my code, what happened exactly is like this:
1. Transaction 1 retrieve the last state of customer balance (say it A)
2. Transaction 1 updated the balance to B = (A + difference1)
3. Transaction 2 retrieve the last state of customer balance (it should be B, but in reality it retrieved A)
4. Transaction 2 updated the balance to C = (A+ difference2)

The final result should be C = A + difference1 + difference2
But in my code, it's C = A + difference2

{code}
session.refresh(customer.getBalance(), LockMode.UPGRADE);
customer.setCashBalance(customer.getBalance() + difference);
session.update(customer.getBalance());
{code}

Thanks in Advance for any suggestion, sharing, or solution.

_________________
An incredible programmer is worthier than a common programmer because of his/her ideas, not his/her codes.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 07, 2007 2:32 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
it's a classical problem. Lost Updates.
If it's okay to change schema,
<version>
element can be used on the table that has concurrent modifications.

For more information, please refer to hibernate documentation.
Let me know if it helps.
Correct me if I am wrong.

-------------------------------------------
Rate the reply if you find it helpful


Top
 Profile  
 
 Post subject: Well...
PostPosted: Fri Dec 07, 2007 3:55 am 
Newbie

Joined: Thu Feb 01, 2007 12:57 pm
Posts: 18
The use of <version> is not pessimistic locking but optimistic according to what i've read...


I have a pb too with pessimistic locking,
I thought it was very simple and all i needed to do is something like

Code:
getHibernateTemplate().lock(object, LockMode.UPGRADE);


But it doesn't seem to lock anything to me...
If anybody can help..


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 07, 2007 4:09 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
>> The use of <version> is not pessimistic locking but optimistic according to what i've read...

Yes. It is.
All we need is a solution to Lost Updates.
Pessimistic Locking / Optimistic Locking are just means.
Correct me If I am wrong.

>>But it doesn't seem to lock anything to me...
It does.
It's like Select For Update.
This is how I have seen it behave on Oracle.
It locks the row at the database level and any other concurrent transaction ( not necessarily from inside Hibernate, even from sqlplus ) that tries to update the same row gets blocked until our transaction releases the lock either through commit or rollback.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 07, 2007 4:26 am 
Newbie

Joined: Fri Aug 31, 2007 11:58 am
Posts: 19
Code:
The use of <version> is not pessimistic locking but optimistic according to what i've read...


I have a pb too with pessimistic locking,
I thought it was very simple and all i needed to do is something like

getHibernateTemplate().lock(object, LockMode.UPGRADE);


But it doesn't seem to lock anything to me...
If anybody can help..


What you may need is LockModeType.WRITE .

_________________
Note:Don't forget to rate,if useful.

Blog:http://elope.wordpress.com/


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 07, 2007 10:03 am 
Newbie

Joined: Thu Feb 01, 2007 12:57 pm
Posts: 18
Well, i don't manage to LOCK using
Code:
getHibernateTemplate().lock(object, LockMode.UPGRADE);



However I have an Hibernate request in the Log console
Code:
Hibernate: select APPLICATION_ID from seeapp.application where APPLICATION_ID =? for update

which seems to mean that the lock is done but...
When i try this request directly in my Database, it works...It's really strange...
I read that AUTOCOMMIT must be disabled...I already check that point i think.


and about LockMode.WRITE it doesnt work with lock() function...

If anyone can help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 07, 2007 11:46 am 
Newbie

Joined: Wed Sep 19, 2007 11:30 pm
Posts: 12
Location: indonesia
Well,before I added some Sets as the properties of the object, the locking did worked. I read on the documentation that hibernate's lock won't work if there is dirty collection. Is the Sets I added have something to do with it?

_________________
An incredible programmer is worthier than a common programmer because of his/her ideas, not his/her codes.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 07, 2007 11:48 am 
Newbie

Joined: Wed Sep 19, 2007 11:30 pm
Posts: 12
Location: indonesia
Well,before I added some Sets as the properties of the object, the locking did worked. I read on the documentation that hibernate's lock won't work if there is dirty collection. Is the Sets I added have something to do with it?

_________________
An incredible programmer is worthier than a common programmer because of his/her ideas, not his/her codes.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 07, 2007 11:54 am 
Newbie

Joined: Thu Feb 01, 2007 12:57 pm
Posts: 18
Well, personnally I perform the LOCK before opening the modification PopUP, so the object is not dirty...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 10, 2007 6:00 am 
Newbie

Joined: Wed Sep 19, 2007 11:30 pm
Posts: 12
Location: indonesia
I solved this problem by changing the save and update action like this:

before:

Code:
session.save(transaction);
... some codes
sessionsession.refresh(transaction.getCustomer(), LockMode.UPGRADE);
customer.setCashBalance(customer.getBalance() + difference);
session.update(transaction.getCustomer());
...some codes
session.update(transaction);



after:

Code:
sessionsession.refresh(transaction.getCustomer(), LockMode.UPGRADE);
...some codes
customer.setCashBalance(customer.getBalance() + difference);
session.update(transaction.getCustomer());
...some codes
session.save(transaction);



Thansk for all your posts.
Seems that the lock wasn't performed because of the dirty object (transaction.customer)

_________________
An incredible programmer is worthier than a common programmer because of his/her ideas, not his/her codes.


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