I am new to JPA 2.0 locking, so it might be I am missing something.
Using NetBeans, I tried to debug a Stateless Session Bean. I tried to switch between two threads to examine the concept:
em.lock(entity, LockModeType.PESSIMISTIC_WRITE);
em.persist(entity);
try {
em.flush();
} catch (Exception e) {
System.out.println("Already Locked!");
}
I let the first process to finish
em.flush();
(no exceptions). Then, I switched to the second process. Surprisingly – it paused after the first line, and continued only after the first process exited the function.
Note: It was all working as expected with LockModeType.OPTIMISTIC.
Is it a normal behavior? am I missing something? here it seems to behave in a different way.
Thanks,
Danny
It is perfectly normal behavior. Lock is released in transaction commit/rollback and that is not happening as a consequence of calling em.flush().