Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Invalidation and JPA(EntityManager returns invalid/stale entities)
Invalidation and JPA [message #516591] Wed, 24 February 2010 13:52 Go to next message
No real name is currently offline No real nameFriend
Messages: 2
Registered: February 2010
Junior Member
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 12:24]

Report message to a moderator

Re: Invalidation and JPA [message #516974 is a reply to message #516591] Thu, 25 February 2010 16:30 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
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
Previous Topic:JPA / EclipseLink, Spring 2.5 and Tomcat 6
Next Topic:Using Observable Lists within entity classes?
Goto Forum:
  


Current Time: Tue Mar 19 07:56:41 GMT 2024

Powered by FUDForum. Page generated in 0.02355 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top