Invalidation and JPA [message #516591] |
Wed, 24 February 2010 08:52  |
Eclipse User |
|
|
|
Hello,
i have a problem with manually invalidating a entity in the cache using the identitymap api. The object is invalid but it is not refreshed when it is accessed again.
The following code snipped executes only a single SQL but it should execute another one to refresh the invalid entity:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
MyEntity entity = em.find(MyEntity .class, "20");
Session eclipselinkSession = ((JpaEntityManager)em.getDelegate()).getActiveSession();
eclipselinkSession.getIdentityMapAccessor().invalidateObject(entity);
eclipselinkSession = ((JpaEntityManager)em.getDelegate()).getServerSession();
eclipselinkSession.getIdentityMapAccessor().invalidateObject(entity);
boolean valid = eclipselinkSession.getIdentityMapAccessor().isValid(entity);
em.getTransaction().commit();
em.getTransaction().begin();
entity = em.find(MyEntity.class, "20");
valid = eclipselinkSession.getIdentityMapAccessor().isValid(entity);
em.getTransaction().commit();
em.close();
emf.close();
Is it even possible to use the invalidation api together with JPA? Did i miss something else?
Tested it with EclipseLink 1.02, 1.1.1, 2.0
Regards Axel
Edit: is this possibly related to this: https://bugs.eclipse.org/bugs/show_bug.cgi?id=272895
Edit2:
With the native API from eclipselink the entity is refreshed as expected
eclipselinkSession.readObject(new MyEntity("20"));
[Updated on: Thu, 25 February 2010 07:24] by Moderator
|
|
|
Re: Invalidation and JPA [message #516974 is a reply to message #516591] |
Thu, 25 February 2010 11:30  |
Eclipse User |
|
|
|
Hello,
The persistence context has its own cache that is being used, inorder to keep transactions issolated. Because you are using the same EntityManager on an extended persistence context for the second find, it returns the same managed entity without having to go to the shared cache (where the object is marked as invalid). You will need to clear your em after you commit your transaction to clear out the persistence context's cache for the second find to cause a refresh.
Best Regards,
Chris
|
|
|
Powered by
FUDForum. Page generated in 0.03362 seconds