public void test() { Vector primaryKeys = new Vector(); primaryKeys.add(new java.math.BigDecimal(4)); CacheKey cacheKey = getAbstractSession() .getIdentityMapAccessorInstance() .acquireDeferredLock( primaryKeys, Employee.class, getSession().getDescriptor(Employee.class), false); CacheKey cacheKey2 = getAbstractSession() .getIdentityMapAccessorInstance() .acquireDeferredLock( primaryKeys, Employee.class, getSession().getDescriptor(Employee.class), false); if (cacheKey != cacheKey2) { throw new TestErrorException( "WeakIdentityMap failed to return same cachkey for successive calls for same primary key and class"); } // must release because the deferred lock is not removed on an initialize identity map cacheKey.releaseDeferredLock(); cacheKey2.releaseDeferredLock(); }
/** * INTERNAL: Check if existence can be determined without going to the database. Note that custom * query check is not require for does exist as the custom is always used. Used by unit of work, * and will return null if checkDatabaseIfInvalid is set and the cachekey is invalidated */ public Object checkEarlyReturn( Object object, Vector primaryKey, AbstractSession session, AbstractRecord translationRow) { // For bug 3136413/2610803 building the selection criteria from an EJBQL string or // an example object is done just in time. buildSelectionCriteria(session); // Return false on null since it can't exist. Little more done in case PK not set in the query if (object == null) { return Boolean.FALSE; } ClassDescriptor descriptor = session.getDescriptor(object.getClass()); if (primaryKey == null) { primaryKey = getPrimaryKey(); if (primaryKey == null) { primaryKey = descriptor.getObjectBuilder().extractPrimaryKeyFromObject(object, session, true); } } if ((primaryKey == null) || (primaryKey.contains(null))) { return Boolean.FALSE; } // Need to do the cache check first if flag set or if we should check the cache only for // existence. if ((shouldCheckCacheForDoesExist() || this.checkCacheFirst) && !descriptor.isDescriptorForInterface()) { // If this is a UOW and modification queries have been executed, the cache cannot be trusted. if (this.checkDatabaseIfInvalid && (session.isUnitOfWork() && ((UnitOfWorkImpl) session).shouldReadFromDB())) { return null; } CacheKey cacheKey; Class objectClass = object.getClass(); AbstractSession tempSession = session; while (tempSession.isUnitOfWork()) { // could be nested lets check all UOWs cacheKey = tempSession .getIdentityMapAccessorInstance() .getCacheKeyForObjectForLock(primaryKey, objectClass, descriptor); if (cacheKey != null) { // If in the UOW cache it can't be invalid. return Boolean.TRUE; } tempSession = ((UnitOfWorkImpl) tempSession).getParent(); } // Did not find it registered in UOW so check main cache and check for invalidation. cacheKey = tempSession .getIdentityMapAccessorInstance() .getCacheKeyForObject(primaryKey, objectClass, descriptor); if ((cacheKey != null)) { // Assume that if there is a cachekey, object exists. if (this.checkDatabaseIfInvalid) { checkDescriptor(object, session); if (this.descriptor .getCacheInvalidationPolicy() .isInvalidated(cacheKey, System.currentTimeMillis())) { return null; } } Object objectFromCache = cacheKey.getObject(); if ((session.isUnitOfWork()) && ((UnitOfWorkImpl) session).wasDeleted(objectFromCache)) { if (shouldCheckCacheForDoesExist()) { return Boolean.FALSE; } } else { return Boolean.TRUE; } } else if (shouldCheckCacheForDoesExist()) { // We know its not in cache, and a checkcache policy so return false. return Boolean.FALSE; } } // Check if we have to assume that the object does not exist. if (shouldAssumeNonExistenceForDoesExist()) { return Boolean.FALSE; } // Check to see if we only need to check that the object contains a primary key. if (shouldAssumeExistenceForDoesExist()) { return Boolean.TRUE; } return null; }