Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Removed entity can still be found by other entity manager instances

em2.find(User.class, id) adds the object to persistence unit cache;
the next time find is called the cached copy is returned.

Call em2.refresh(entity) instead of the second find - that throws EntityNotFoundException.

You could also call em.clear() before each find, but that means you would potentially read each object several times; also could unintentionally wipe out your changes before they committed. Consider defining the entity with optimistic locking instead, then you still can take advantage of caching and exception is thrown on attempt to update the object, which no longer exists (or has been updated by another app).

----- Original Message ----- From: <patric@xxxxxxxxxx>
To: <eclipselink-users@xxxxxxxxxxx>
Sent: Tuesday, July 28, 2009 10:56 AM
Subject: [eclipselink-users] Removed entity can still be found by other entity manager instances


Hello everyone,

imagine the following situation:

    User newObj = new User ();
    newObj.setUserMode('A');
    em1.persist(newObj);
    id = newObj.getUserId();

assertNull(em2.find(User.class, id)); //no exception, entity can not be found yet.

    //...committing/beginning transaction of em1 here...

assertNotNull(em2.find(User.class, id)); //no exception, entity is found
    em1.remove(newObj);

    //...committing/beginning transaction of em1 here...

    //after commit of em1:
assertNull(em1.find(User.class, id)); //no exception, entity is removed!

assertNull(em2.find(User.class, id)); //exception! em2 can still find and access the removed entity!



As you can see, an entity manager can still access a removed entity (by an other entity manager) if it has been fetched before its removal.

Even more odd, this entity can be changed without any exceptions after commiting and when looking into the finest logs, entity manager em2 seems to execute an update SQL but doesn't check
that no records have updated.

I would expect at least an exception on this, or even better that the last assertion won't fail already. How can I archive that? And how to archive that if an external application deletes/modifies a record?

I tried disabling all caching by setting eclipselink.cache.type.default to "NONE", but this didn't help.

Excerpt from my persistence.xml:

   <property name="eclipselink.cache.shared.default" value="false"/>

Best regards,
Patric




_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users




Back to the top