Community
Participate
Working Groups
The contains method only verifies that the object is in the shared cache. It does not address objects in the cache that are invalid through expiration or explicit invalidation/eviction. The following test fails on the assertFalse after the evict call: @Test public void evictByPK() { EntityManager em = getEntityManager(); Employee emp = getExamples().minimumEmployee(em, null); assertNotNull(emp); int select = getQuerySQLTracker(em).getTotalSQLSELECTCalls(); Cache cache = em.getEntityManagerFactory().getCache(); assertTrue(cache.contains(Employee.class, emp.getId())); em.clear(); Employee findEmp = em.find(Employee.class, emp.getId()); assertNotNull(findEmp); assertEquals(select, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); cache.evict(Employee.class, emp.getId()); assertFalse(cache.contains(Employee.class, emp.getId())); em.clear(); findEmp = em.find(Employee.class, emp.getId()); assertNotNull(findEmp); assertTrue(getQuerySQLTracker(em).getTotalSQLSELECTCalls() > select); } This can be fixed in the CacheImpl class by accessing the CacheKey as: public boolean contains(Class cls, Object primaryKey) { emf.verifyOpen(); Vector pk = createPKVector(cls, primaryKey); ClassDescriptor descriptor = this.serversession.getDescriptor(cls); CacheKey key = this.imap.getCacheKeyForObject(pk, cls, descriptor); return key != null && descriptor.getCacheInvalidoldationPolicy().isInvalidated(key); } Note: This requires the use of the IdentityMapAccessor (impl) and not the interface. The CacheImpl class also maintains a lot of redundant state which may want to be cleaned up as it couples the CacheImpl to the state of the EMF when it is created.
THe above impl of contains was missing a '!'. This one works: public boolean contains(Class cls, Object primaryKey) { emf.verifyOpen(); Vector pk = createPKVector(cls, primaryKey); ClassDescriptor descriptor = this.serversession.getDescriptor(cls); CacheKey key = this.imap.getCacheKeyForObject(pk, cls, descriptor); return key != null && !descriptor.getCacheInvalidationPolicy().isInvalidated(key); }
Created attachment 153010 [details] patch
checked in
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink