-->
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.  [ 4 posts ] 
Author Message
 Post subject: how to obtain a exclusive lock?
PostPosted: Sat May 12, 2007 12:01 pm 
Newbie

Joined: Sat May 12, 2007 11:42 am
Posts: 2
Environment:
Hibernate3.0
SQLServer 2005,
SQLJDBC 2005

Statement:
Session sess=HiberanteUtil.getSession();
Transaction tx=sess.beginTransaction();
int id=1;
User user=sess.get(User.class,id,LockMode.UPGRADE);
user.set... //5th line
...

when I debugged the program,I found two running threads reach the 5th line same time,which means they both obtained the exclusive locks of the same record.Didn't the get() method obtain the exclusive lock?
furthermore,how to lock the hole db table.
any answer will be appreciated.

_________________
Never give up


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 14, 2007 2:06 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
>>> when I debugged the program,I found two running threads reach the 5th line same time,
A) which means they both obtained the exclusive locks of the same record ?
B) Didn't the get() method obtain the exclusive lock?


LockMode.UPGRADE obtains a database level lock, if that is supported.
Oracle and PostgreSQL support it. I don't know about other databases.

Further, in this scenario ( I simulated on Oracle, Kindly excuse! I don't have SQL server. )
Both the transactions obtain exclusive lock of the same record, but in turns.
NOT AT THE SAME TIME.
Here, one transaction waits for the exclusive lock to be released by the other.
Thus, we see two running threads at the same time.
This is valid and expected.

>>> how to lock the hole db table ?
Hibernate doesn't support table level locks.
We can obtain them using jdbc, if the underlying database supports them.

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 14, 2007 3:14 am 
Newbie

Joined: Sat May 12, 2007 11:42 am
Posts: 2
Thank you!
>>Both the transactions obtain exclusive lock of the same record, but in >>turns.
>>NOT AT THE SAME TIME.
>>Here, one transaction waits for the exclusive lock to be released by the >>other.
the second thread can't obtain the exclusive lock until the first thread commit the transaction or rollback it.Do I misunderstand it?

further,
according to Hibernate API Document,
LockMode.UPGRADE is An upgrade lock. Objects loaded in this lock mode are materialized using an SQL select ... for update.

_________________
Never give up


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 14, 2007 3:49 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
>>> the second thread can't obtain the exclusive lock until the first thread commit the transaction or rollback it.Do I misunderstand it?
No. You are correct.
Two transactions cannot simultaneously hold an exclusive lock on a row.

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


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