users@glassfish.java.net

JPA OptimisticLockException even after I locked the entity

From: <glassfish_at_javadesktop.org>
Date: Thu, 22 Oct 2009 11:39:23 PDT

Hi,

I've been getting errors like this one:

javax.persistence.OptimisticLockException: Exception [TOPLINK-5006] (Oracle TopLink Essentials - 2.1 (Build b60e-fcs (12/23/2008))): oracle.toplink.essentials.exceptions.OptimisticLockException
Exception Description: The object [StatsEntity[hotelCode=CHIRC, statsDate=Thu Oct 22 00:00:00 CDT 2009, transactionType=CHECK_IN, classification=HALT, deviceType=DESKTOP]] cannot be updated because it has changed or been deleted since it was last read.
Class> com.company.product.model.Stats Primary Key> [689303]

I did some reading about JPA 1.0 and OptimisticLockException. I learned that I'm responsible for catching the exception, reloading the data, updating it again, and trying to save again... until it works or until I give up.

I also learned about locking the entity to prevent this error from happening. That is what I chose to do. After loading the entity I call:

entityManager.lock(entity, LockModeType.READ);

I thought that I would hold a lock on the entity until the transaction commits or rolls back. I suspect my problem is LockModeType.READ. Maybe it should be LockModeType.WRITE.

Can anyone please comment?


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

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