-->
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: Atomic read & delete
PostPosted: Fri Jul 13, 2007 4:06 am 
Newbie

Joined: Thu Jul 12, 2007 9:09 am
Posts: 7
Hi,

I want to implement a mechanism where I can read a row from a table and lock it, then delete the row so that no other reads will see this row again, and each read is guaranteed to get a unique row.

Here's what I've tried:

Code:
      Criteria criteria = getCurrentSession().createCriteria(SomeClass.class);
      criteria.add(Restrictions.in("fieldName", value));.setLockMode(LockMode.UPGRADE).setMaxResults(1);

      List<SomeClass> result = criteria.list();
      SomeClass nextObject = null;
      if (!result .isEmpty()) {
         nextObject = result .get(0);
         delete(SomeClass.class, nextObject.getId());
      }
      return nextObject;


LockMode.UPGRADE seems to have no effect in HSQLDB (Multiple threads can get the same row), and when I tried MySQL, the first select just hangs, so I guess I'm not doing the locking correctly, but I'm not sure what the correct way of doing this is.

This 'fetch' operation is done in a transaction with other data access code, so I don't want to change the isolation level of the entire transaction.

If there was a way to tell hibernate to generate the table with exclusive row read locks, that would be fine too, but I'm not sure how to do this either.

Any help is greatly appreciated!!!

Thanks,
John.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 9:05 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Hi John

I ran a couple of tests and can confirm that LockMode.UPGRADE doesn't work in HSQLDB. The documentation for HSQLDB states it doesn't support "select for update" which is probably why it fails.

However, against MYSQL (Ver 14.12 Distrib 5.0.41, for Win32) it works fine.

As an alternative you could lock the rows by updating them e.g.

session.createQuery("update SomeClass set id = id").executeUpdate();

...although this might be expensive if the table is large.

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 10:56 am 
Newbie

Joined: Thu Jul 12, 2007 9:09 am
Posts: 7
thatmikewilliams wrote:
Hi John

I ran a couple of tests and can confirm that LockMode.UPGRADE doesn't work in HSQLDB. The documentation for HSQLDB states it doesn't support "select for update" which is probably why it fails.

However, against MYSQL (Ver 14.12 Distrib 5.0.41, for Win32) it works fine.

As an alternative you could lock the rows by updating them e.g.

session.createQuery("update SomeClass set id = id").executeUpdate();

...although this might be expensive if the table is large.

Mike


Thanks Mike,

There was a problem in my test code where the transaction writing the rows into the table was still active when I was trying to perform the "select .. for update" in a different transaction; hence the hang. I eliminated this problem, and it worked fine on MySQL. I also tried H2 instead of HSQLDB, since it supports this type of locking, and it worked there too.

A happy ending to this one :)

John.


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.