Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Optimistic Lock Exception expected

A lock error should occur on your merge() call, it is unexpected if it does
not.

Ensure that you have an @Version in your object.  If still not getting the
error please include the code for your class and the log on finest of your
test.



Mathias Koehnke - Sun Microsystems wrote:
> 
> Hi,
> 
> while replacing Toplink Essentials (2.0.1-b04-fcs (04/11/2008)) by 
> EclipseLink 1.1 for a Java SE project, I came across a problem that 
> prevents me from switching: I would expect getting an Optimistic Lock 
> exception in case an entity gets removed from the database (by another 
> thread or whatever) and one tries to commit on a still-existing runtime 
> object of that entity. That was my expectation and it worked with 
> Toplink Essentials so far. Using EclipseLink it doesn't throw an OL 
> exception, instead the entity gets re-inserted in the database.
> 
> Am I using JPA/EclipseLink the wrong way or do I miss something? From my 
> understanding the following example should throw an OL exception when 
> committing the last transaction:
> 
> Entity entity = new Entity();
> entity.setValue("entity");
> 
> //persist "entity" and create clone "entityCopy"
> EntityManager em1 = getEntityManagerFactory().createEntityManager();
> em1.getTransaction().begin();
> em1.persist(entity);
> em1.getTransaction().commit();
> Entity entityCopy = em1.find(Entity.class, entity.getId());
> em1.close();
> 
> //delete "entity" from database
> EntityManager em2 = getEntityManagerFactory().createEntityManager();
> em2.getTransaction().begin();
> entity = em2.merge(entity);
> em2.remove(entity);
> em2.getTransaction().commit();
> em2.close();
> 
> //merge clone "entityCopy" and commit
> EntityManager em3 = getEntityManagerFactory().createEntityManager();
> em3.getTransaction().begin();
> entityCopy = em3.merge(entityCopy);
> em3.lock(entityCopy, LockModeType.WRITE);
> em3.getTransaction().commit();
> em3.close();
> 
> 
> Note: I already added a SessionCustomizer modifying the session the 
> following way:
> 
> session.getProject().assumeExistenceForDoesExist();
> 
> So, EclipseLink shouldn't check the database for the existence of the 
> entity at all. Instead it should assume that the entity exists and 
> simply execute an "UPDATE ..." sql statement.
> 
> 
> Thanks for your help in advance.
> 
> Best regards,
> Mathias Koehnke
> 
> 


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/Optimistic-Lock-Exception-expected-tp22742662p22788601.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top