-->
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.  [ 5 posts ] 
Author Message
 Post subject: select ... for update (LockMode.UPGRADE not working)
PostPosted: Sun May 17, 2009 6:11 pm 
Newbie

Joined: Sun May 17, 2009 5:46 pm
Posts: 8
I'm using Hibernate 3 with a MySQL 5.1 server.

Hibernate seems to be ignoring my request to use pessimistic locking. I'm testing this by having two threads run the below code with different delays before and after reading from the db (thread 1: 1 sec then 4 secs; thread 2: 2 secs then 1 sec):

Session session = HibernateUtil.openSession();
Transaction transaction = session.beginTransaction();
Thread.sleep(getDelay1InSecs()*1000);
//this line should obtain a pessimistic lock
UnconfirmedUser user = (UnconfirmedUser)session.get(UnconfirmedUser.class, (long)1, LockMode.UPGRADE);
Thread.sleep(getDelay2InSecs()*1000);
user.setPassword(user.getPassword()+getThreadId());
transaction.commit();
session_.close();

I would expect this code to append the thread id's from both threads to the end of the existing password. This is not happening; the password only has the id of the last thread to commit appended to it (i.e. I have a lost-update problem).

It's definitely not a MySQL problem: All my db tables are using the InnoDb engine (which supports transactions and row-level locking) and I've confirmed that select ... for update does block as appropriate by manually opening two mysql consoles and doing the equivalent test in sql.

Do I need to do more than set the LockMode to UPGRADE?

I've searched forums all over the place. I really hope somebody can help.

P.S. This is the sysout:
Hibernate:
select
unconfirme0_.id as id12_0_,
unconfirme0_.displayName as displayN2_12_0_,
unconfirme0_.emailAddy as emailAddy12_0_,
unconfirme0_.confirmationCode as confirma4_12_0_,
unconfirme0_.password as password12_0_
from
UnconfirmedUsers unconfirme0_
where
unconfirme0_.id=?

Isn't it meant to say "for update" at the end?


Last edited by tomg on Wed May 20, 2009 2:45 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: select ... for update (LockMode.UPGRADE not working)
PostPosted: Mon May 18, 2009 7:14 am 
Newbie

Joined: Wed May 06, 2009 6:10 am
Posts: 4
try locking the object instead of class. for example,

session.lock(object, LockMode.UPGRADE);


Top
 Profile  
 
 Post subject: Re: select ... for update (LockMode.UPGRADE not working)
PostPosted: Mon May 18, 2009 10:26 am 
Newbie

Joined: Sun May 17, 2009 5:46 pm
Posts: 8
I've tried that too - but to no avail.

I've also tried
System.out.println(session.getCurrentLockMode(user));
to confirm that the object is being placed in the correct lock mode (LockMode.UPGRADE) and it is.

Thanks though .

Anybody else have any ideas?


Top
 Profile  
 
 Post subject: Re: select ... for update (LockMode.UPGRADE not working)
PostPosted: Wed May 20, 2009 2:43 am 
Newbie

Joined: Sun May 17, 2009 5:46 pm
Posts: 8
ping


Top
 Profile  
 
 Post subject: Re: select ... for update (LockMode.UPGRADE not working)
PostPosted: Mon Jul 26, 2010 12:16 pm 
Newbie

Joined: Mon Jul 26, 2010 12:12 pm
Posts: 1
I was wondering about this myself, but in terms of a db that does not support row locking ie ISAM. The hibernate documentation says that it will
Quote:
If the requested lock mode is not supported by the database, Hibernate uses an appropriate alternate mode instead of throwing an exception. This ensures that applications are portable.
Does anyone have any experience with this?


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