I currently facing a problem with locking an entity in JPA. I have a list of several entities from a database. For each element there's a edit button which loads a view for editing that entry. Every time a client tries to edit an entry, I want to check if the entity is locked preventing the client to even load the edit mask of the entity.
My method which loads the entity:
[...]
mail = (EMailKonto) query.getSingleResult();
System.out.println(getLock(mail).toString());
setLock(mail, LockModeType.PESSIMISTIC_WRITE);
System.out.println(getLock(mail).toString());
The called methods:
public void setLock(T entity, LockModeType lock)
{
getEntityManager().lock(entity, lock);
}
public LockModeType getLock(T entity)
{
return getEntityManager().getLockMode(entity);
}
What happens is that the first syso prints NONE, since there is no lock applied. Then the lock gets set and the second syso prints PESSIMISTIC_WRITE. When i refresh the page or use another tab / browser and click on the edit button of the same entity, the first syso should print PESSIMISTIC_WRITE, since I never remove the lock, but it again prints NONE. Could you guys help me to understand how to implement that kind of function?
Regards