[jboss-user] [EJB 3.0] - Re: EntityManager.lock() issues

ablevine1 do-not-reply at jboss.com
Wed Oct 25 19:55:36 EDT 2006


anonymous wrote : 
  | The approach I was trying to use: 
  | 
  | AEntity a = manager.find(AEntity.class, aId); 
  | manager.lock(a, LockModeType.READ); 
  | 
  | I think now it's not the way to go, because it's not atomic: Different competing threads could adquire an entity representing that database row before one of then reaches the lock. 
  | 

I had a simlar problem and came up with this solution: Since multiple threads can get then entity before locking it, once you get the lock, refresh the entitiy, then you will have the latest copy and you can then update it without overwriting previous updates:

  | AEntity a = manager.find(AEntity.class, aId); 
  | manager.lock(a, LockModeType.READ); 
  | manager.refresh(a);
  | // now do stuff
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3980871#3980871

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3980871



More information about the jboss-user mailing list