2

I have tried disabling L2 cache in EclipseLink with Eclipse indigo by using following properties in persistence.xml:-

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

<shared-cache-mode>NONE</shared-cache-mode>

Basically I am testing one scenario whether same object created in two different sessions is hitting database twice or both sessions are referring to same object created in earlier session in memory cache. It should not because L2 cache is disabled by mentioning above properties in persistence.xml

My code is as below:-

 Session session = DataAccessManager.getManager().openSession();
 ReferenceObjectRepository referenceObjectRepository =     ReferenceObjectRepository.getInstance();
 ReferenceObjectKey referenceObjectKey = new ReferenceObjectKey(getStringValue("testCacheByPass.input"));
 //load object first time.
 ReferenceObject referenceObject = referenceObjectRepository.load(ReferenceObject.class, referenceObjectKey);
 logger.log(Level.SEVERE, "Cache ReferenceObject: " + referenceObject);

 //load object in another session
 Session sessionNew = DataAccessManager.getManager().openNewSession();
 Object dbObject = referenceObjectRepository.load(ReferenceObject.class,    referenceObjectKey);
 logger.log(Level.SEVERE, "DB loaded ReferenceObject: " + dbObject);

Please help me whether I have missed something? or do I need to do it some other way??

user1751802
  • 21
  • 1
  • 3
  • This code abstracted out EclipseLink/JPA entirely, making it difficult to verify that you are using separate EntityManager instances for your queries. The code itself makes it look like you are reusing the same referenceObjectRepository - could this be part of the problem? If it is reusing the same underlying EntityManager instance, it is required to return the same entity instance unless it was cleared. – Chris Oct 17 '12 at 20:08

2 Answers2

3

Add this line in each function where the call is made. I use in the find function when consulted a view.

((JpaEntityManager)em.getDelegate()).getServerSession().getIdentityMapAccessor().invalidateAll();

This line clear the cache before run de query.

public Entity find(Object id) {
    ((JpaEntityManager)em.getDelegate()).getServerSession().getIdentityMapAccessor().invalidateAll();
    return em.find(Entity.class, id);
}
  • This works! But only when I created new `EntityManager`. `EntityManager e = Context.getFactory().createEntityManager();` `((JpaEntityManager)e.getDelegate()).getServerSession().getIdentityMapAccessor().invalidateAll();` – Xupypr MV Jan 27 '16 at 17:26
1

You have disabled the object cache, but I think you still have query cache in play. You should be able to disable query cache too with

<property name="eclipselink.query-results-cache" value="false"/>
<property name="eclipselink.refresh" value="true"/>

Same thing can be set with query hints, too. You could also try using query hints if persistence.xml configuration doesn't seem to be working.

Also note that essentially, even without the caching, you'd be comparing the same object, so unless it is detached it should be the same.

Related questions:

Community
  • 1
  • 1
eis
  • 45,245
  • 11
  • 129
  • 177