[hibernate-users] How do I have a session.get lock and block other reads

Sean Porter sean.porter at searchamerica.com
Mon Jan 17 12:19:32 EST 2011


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 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/hibernate-users/attachments/20110117/7a7a0f3e/attachment.html 


More information about the hibernate-users mailing list