Hello,
i'm trying to keep the locking state of an hibernate object synchronized across two sessions. I want to lock an object in the first session and check in the second session whether this object is locked or not. The aim is to prevent multiple users to change the same object.
Code to check / set lock:
Code:
if (HibernateAccessor.getInstance().getLockMode((Feld)getUserObject()) != LockMode.UPGRADE) {
HibernateAccessor.getInstance().setLockMode((Feld)getUserObject(), LockMode.UPGRADE);
// [object is now locked]
} else {
// [object was locked in other session]
}
class HibernateAccessor {
public LockMode getLockMode(Object objectToGetLockModeFor) {
LockMode lockMode = null;
try {
lockMode = session.getCurrentLockMode(objectToGetLockModeFor);
} catch (HibernateException e) {
e.printStackTrace();
}
return lockMode;
}
public void setLockMode(Object objectToSetLockMode, LockMode lockMode) {
try {
session.lock(objectToSetLockMode, lockMode);
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
Locking the object in the first session works. But asking, if the object is locked in the second session fails: getCurrentLockMode() always returns LockMode.READ, indicating that this object is not locked. But when trying to lock the object again in the second session the session.lock() blocks. No Error/Exception is thrown. After closing the first session, the blocking session.lock() continues.
How do I handle this concurrency issue in a correct way?
I'm using Hibernate 2.1.2 on an Oracle 8.1.7 Database