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.