users@glassfish.java.net

Re: JPA OptimisticLockException even after I locked the entity

From: <glassfish_at_javadesktop.org>
Date: Thu, 22 Oct 2009 13:09:46 PDT

According to the spec, LockModeType.READ is what I want:

http://java.sun.com/javaee/5/docs/api/javax/persistence/LockModeType.html

Can someone help me better understand what that javadoc is saying? Is it saying that locking an entity won't prevent another transaction from reading the entity and making changes to it, but the lock will cause both transactions to fail because of dirty read or non-repeatable read?

I was hoping that the lock would block other transactions from reading the entity until the transaction has been committed, kind of like a synchronized block in Java code.

I did some googling and saw someone suggesting doing a flush(), then catching OptimisticLockException, then doing a refresh(). When would I do this? Immediately after calling lock(), or immediately after my merge()? If I did it after the merge(), then I'd have to re-do all the work on the entity to set the new values.


Thanks,
Ryan
[Message sent by forum member 'rdelaplante' (ryan_at_ijws.com)]

http://forums.java.net/jive/thread.jspa?messageID=369017