I am new to use "optimistic locking" mechanism - I am using hibernate (in Jboss) and Container Managed Transaction (CMT). I want to handle the scenario when, between my entity-read and entity-update someone else updates the same entity (i.e. row) in DB. In such case I want to throw exception..
I have annotated my entity with @Version - like
@Version
private Long version;
Now, I am confused if this is enough for version management or I need to explicitly call the EntityManager.lock() api like
{
.
.
final QueryDTO queryDTO = entityManager.find(QueryDTO.class, id);
entityManager.lock(queryDTO, LockModeType.READ);
queryDTO.setStatus(updatedStatus);
entityManager.persist(queryDTO);
}
Thanks in advance,