[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[eclipselink-users] Optimistic Lock Exception expected
|
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