How do I have a session.get lock and block other reads
by Sean Porter
Currently trying Pessimistic LockMode/Lock Options but this results in a
LockAcquisitionException. Ideally this would be blocking the second
thread trying to access the same record. Then when the first thread
releases lock the second would do its read and update.
EX:
We have two threads that do a read. The first thread T1 appears to
obtain the lock.
T1(l)----> t = Session().get(this.getType(), id, new
LockOptions(LockMode.PESSIMISTIC_READ));
T2(1)----> t2 = Session().get(this.getType(), id, new
LockOptions(LockMode.PESSIMISTIC_READ));
T2(2)----> dao.update(t2);
T2(3)----> t2.commit()
org.hibernate.exception.LockAcquisitionException: could not update:
We want T2(1) to wait until T1 is commited (versus blowing up). Here
T2(1) appears to do a dirty read of the object, and then throw an
exception on commit T2(3).
Here is our workaround, but we don't want to have to do this extra IO.
Any ideas on what to try?
public T read(PK id) {
synchronized (LockingDao.class) {
t = session().get(class, id);
MyObject myObj = (MyObject) t;
myObj.setLastModDate(new Date());
update(t);
getSession().flush();
return t;
}
}
Any insight is much appreciated,
Sean
13 years, 10 months