Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Clear object from cache with class and id
Clear object from cache with class and id [message #1014142] Mon, 25 February 2013 08:55 Go to next message
Filipp A. is currently offline Filipp A.Friend
Messages: 49
Registered: February 2010
Member
Hi All,
is it possible on some way to remove Object from Cache with Class and Id ?

my problem is -
- I get all data with NamedQuery + QueryHints.READ_ONLY
- For edit data i get object with load(Object.class, Object.Id) then merge()
and yet I have two( or more ) different Reference to Object in cache with the same Id.

Thanks in Advance!
Re: Clear object from cache with class and id [message #1014187 is a reply to message #1014142] Mon, 25 February 2013 10:17 Go to previous messageGo to next message
Filipp A. is currently offline Filipp A.Friend
Messages: 49
Registered: February 2010
Member
I think, I have it already -
((JpaEntityManager)em.getDelegate()).getActiveSession().getIdentityMapAccessor().removeFromIdentityMap(id, clazz);


or exists some better way to do this ?
Re: Clear object from cache with class and id [message #1014197 is a reply to message #1014187] Mon, 25 February 2013 10:50 Go to previous messageGo to next message
Renso Lohuis is currently offline Renso LohuisFriend
Messages: 44
Registered: July 2012
Member
You can also do:

em.clear();
entityManagerFactory = em.getEntityManagerFactory();
entityManagerFactory.getCache().evict(type, id);


But I don't know if that is better then your solution

[Updated on: Mon, 25 February 2013 10:51]

Report message to a moderator

Re: Clear object from cache with class and id [message #1014317 is a reply to message #1014197] Mon, 25 February 2013 15:28 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
Chances are that you are not seeing two objects in the cache with the same ID (since the objects are cached based on their ID), and instead are seeing the object referenced by the cache being different from one referenced be a relationship in the cache. For example B->A' where B is cached, while A is cached directly. If this is the case, B would need to be evicted so that it is rebuilt and references the A from the cache.
The two methods affect different layers of the cache:
((JpaEntityManager)em.getDelegate()).getActiveSession().getIdentityMapAccessor().removeFromIdentityMap(id, clazz);
affects the active Session, which would be the UnitOfWork or first level cache while
entityManagerFactory.getCache().evict(type, id);
affects the ServerSession's shared cache if it is being used.


In my opinion though, the question should be how is this occurring rather than how to fix it once it does, as I don't know how you are detecting the problems that can occur. I've seen it occur when modifying read-only objects from the cache, something that should be avoided. You might be able to turn off the read-only hint and see if the problem goes away, then check that you are not mistakenly modifying the returned objects somehow without having to add protection for the error case.

Best Regards,
Chris
Re: Clear object from cache with class and id [message #1014353 is a reply to message #1014317] Mon, 25 February 2013 16:39 Go to previous message
Filipp A. is currently offline Filipp A.Friend
Messages: 49
Registered: February 2010
Member
Thanks for your explanation,
i am sure, that my problem is with session cache, if i turn the cache off (PersistenceUnitProperties.CACHE_SHARED_DEFAULT, false) everything is fine.
my "standard" use of db is like:
1. get ReadOnly list of objects
2. if must be edited -
2.1 load ReadWrite Object with em.load()
2.2 merge to DB with em.merge()
2.3 update list with ReadOnly object

maybe exists any "best practice" for my case ?

and one more question - if object,that must be removed from cache, has many x to many, x to x relations (every object fetch = eager), is it enough to call ((JpaEntityManager)em.getDelegate()).getActiveSession().getIdentityMapAccessor().removeFromIdentityMap(id, mainObjectClazz) to remove all of subobject from the cache or not ?

thanks!
Previous Topic:JAXB unmarshaller : no AttachmentUnmarshaller set
Next Topic:Logging differences between Toplink and EclipseLink
Goto Forum:
  


Current Time: Tue Mar 19 05:05:56 GMT 2024

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

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

Back to the top