Class CacheInterceptor
- java.lang.Object
-
- org.eclipse.persistence.sessions.interceptors.CacheInterceptor
-
- All Implemented Interfaces:
Cloneable,IdentityMap
public abstract class CacheInterceptor extends Object implements IdentityMap
Purpose: Define a class through which Cache access can be intercepted.
Description: EclipseLink makes extensive use of caching functionality. This class provides the mechanism for intercepting the cache access. Once intercepted applications can allow the process to continue by calling the method on the class attribute targetIdentityMap as described in the API javadocs or redirect the process entirely. * As with IdentityMaps an entire class inheritance hierarchy will share the same interceptor.
To implement a CacheInterceptor users should subclass this class implementing those methods they wish to intercept.
Configuration: Interceptors may be added to a session by using a Session or Descriptor customizer to set the class name of the interceptor on the target ClassDescriptor through the method setCacheInterceptorClass(Class).
-
-
Field Summary
Fields Modifier and Type Field Description protected AbstractSessioninterceptedSessionprotected IdentityMaptargetIdentityMapThis attribute stores the actual IdentityMap as configured by the user.
-
Constructor Summary
Constructors Modifier Constructor Description protectedCacheInterceptor(IdentityMap targetIdentityMap, AbstractSession interceptedSession)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description CacheKeyacquireDeferredLock(Object primaryKey, boolean isCacheCheckComplete)Acquire a deferred lock on the object.CacheKeyacquireLock(Object primaryKey, boolean forMerge, boolean isCacheCheckComplete)Acquire an active lock on the object.CacheKeyacquireLockNoWait(Object primaryKey, boolean forMerge)Acquire an active lock on the object, if not already locked.CacheKeyacquireLockWithWait(Object primaryKey, boolean forMerge, int wait)Acquire an active lock on the object, if not already locked.CacheKeyacquireReadLockOnCacheKey(Object primaryKey)Acquire a read lock on the object.CacheKeyacquireReadLockOnCacheKeyNoWait(Object primaryKey)Acquire a read lock on the object, if not already locked.abstract Objectclone()Clone the map and all of the CacheKeys.Enumeration<CacheKey>cloneKeys()Allow for theCacheKeyelements to be iterated.voidcollectLocks(HashMap threadList)Add all locked CacheKeys to the map grouped by thread.booleancontainsKey(Object primaryKey)Return true if an CacheKey with the primary key is in the map.protected abstract CacheKeyInterceptorcreateCacheKeyInterceptor(CacheKey wrappedCacheKey)Enumerationelements()Allow for the cache to be iterated on.Objectget(Object primaryKey)Return the object cached in the identity map or null if it could not be found.abstract Map<Object,CacheKey>getAllCacheKeysFromIdentityMapWithEntityPK(Object[] pkList, ClassDescriptor descriptor, AbstractSession session)ADVANCED: Using a list of Entity PK this method will attempt to bulk load the entire list from the cache.abstract Map<Object,Object>getAllFromIdentityMapWithEntityPK(Object[] pkList, ClassDescriptor descriptor, AbstractSession session)ADVANCED: Using a list of Entity PK this method will attempt to bulk load the entire list from the cache.CacheKeygetCacheKey(Object primaryKey, boolean forMerge)Get the cache key (with object) for the primary key.CacheKeygetCacheKeyForLock(Object primaryKey)Get the cache key (with object) for the primary key.ClassDescriptorgetDescriptor()Return the descriptor that this is the map for.Class<?>getDescriptorClass()Return the class that this is the map for.intgetMaxSize()intgetSize()Return the number of CacheKeys in the IdentityMap.intgetSize(Class<?> myClass, boolean recurse)Return the number of actual objects of type myClass in the IdentityMap.IdentityMapgetTargetIdenttyMap()Return the instance of the IdentityMap that this intercpetor is wrapping.ObjectgetWrapper(Object primaryKey)Get the wrapper object from the cache key associated with the given primary key, this is used for EJB2.ObjectgetWriteLockValue(Object primaryKey)Get the write lock value from the cache key associated to the primarykey.Enumeration<CacheKey>keys()Allow for theCacheKeyelements to be iterated.Enumeration<CacheKey>keys(boolean checkReadLocks)Allow for theCacheKeyelements to be iterated.voidlazyRelationshipLoaded(Object rootEntity, ValueHolderInterface valueHolder, ForeignReferenceMapping mapping)Notify the cache that a lazy relationship has been triggered in the object and the cache may need to be updatedCacheKeyput(Object primaryKey, Object object, Object writeLockValue, long readTime)Store the object in the cache at its primary key.Objectremove(Object primaryKey, Object object)Remove the CacheKey with the primaryKey from the map.Objectremove(CacheKey cacheKey)Remove the CacheKey from the map.voidsetDescriptor(ClassDescriptor descriptor)Set the descriptor that this is the map for.voidsetWrapper(Object primaryKey, Object wrapper)Update the wrapper object in the CacheKey associated with the given primaryKey, this is used for EJB2.voidsetWriteLockValue(Object primaryKey, Object writeLockValue)Update the write lock value of the CacheKey associated with the given primaryKey.StringtoString()voidupdateMaxSize(int maxSize)This method will be used to update the max cache size, any objects exceeding the max cache size will be remove from the cache.-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.eclipse.persistence.internal.identitymaps.IdentityMap
release
-
-
-
-
Field Detail
-
targetIdentityMap
protected IdentityMap targetIdentityMap
This attribute stores the actual IdentityMap as configured by the user.
-
interceptedSession
protected AbstractSession interceptedSession
-
-
Constructor Detail
-
CacheInterceptor
protected CacheInterceptor(IdentityMap targetIdentityMap, AbstractSession interceptedSession)
-
-
Method Detail
-
acquireDeferredLock
public CacheKey acquireDeferredLock(Object primaryKey, boolean isCacheCheckComplete)
Acquire a deferred lock on the object. This is used while reading if the object has relationships without indirection. This first thread will get an active lock. Other threads will get deferred locks, all threads will wait until all other threads are complete before releasing their locks.- Specified by:
acquireDeferredLockin interfaceIdentityMap
-
acquireLock
public CacheKey acquireLock(Object primaryKey, boolean forMerge, boolean isCacheCheckComplete)
Acquire an active lock on the object. This is used by reading (when using indirection or no relationships) and by merge.- Specified by:
acquireLockin interfaceIdentityMap
-
acquireLockNoWait
public CacheKey acquireLockNoWait(Object primaryKey, boolean forMerge)
Acquire an active lock on the object, if not already locked. This is used by merge for missing existing objects.- Specified by:
acquireLockNoWaitin interfaceIdentityMap
-
acquireLockWithWait
public CacheKey acquireLockWithWait(Object primaryKey, boolean forMerge, int wait)
Acquire an active lock on the object, if not already locked. This is used by merge for missing existing objects.- Specified by:
acquireLockWithWaitin interfaceIdentityMap
-
acquireReadLockOnCacheKey
public CacheKey acquireReadLockOnCacheKey(Object primaryKey)
Acquire a read lock on the object. This is used by UnitOfWork cloning. This will allow multiple users to read the same object but prevent writes to the object while the read lock is held.- Specified by:
acquireReadLockOnCacheKeyin interfaceIdentityMap
-
acquireReadLockOnCacheKeyNoWait
public CacheKey acquireReadLockOnCacheKeyNoWait(Object primaryKey)
Acquire a read lock on the object, if not already locked. This is used by UnitOfWork cloning. This will allow multiple users to read the same object but prevent writes to the object while the read lock is held.- Specified by:
acquireReadLockOnCacheKeyNoWaitin interfaceIdentityMap
-
collectLocks
public void collectLocks(HashMap threadList)
Add all locked CacheKeys to the map grouped by thread. Used to print all the locks in the identity map.- Specified by:
collectLocksin interfaceIdentityMap
-
clone
public abstract Object clone()
Clone the map and all of the CacheKeys. This is used by UnitOfWork commitAndResumeOnFailure to avoid corrupting the cache during a failed commit.- Specified by:
clonein interfaceIdentityMap- Overrides:
clonein classObject
-
containsKey
public boolean containsKey(Object primaryKey)
Return true if an CacheKey with the primary key is in the map. User API.- Specified by:
containsKeyin interfaceIdentityMap- Parameters:
primaryKey- is the primary key for the object to search for.
-
createCacheKeyInterceptor
protected abstract CacheKeyInterceptor createCacheKeyInterceptor(CacheKey wrappedCacheKey)
-
elements
public Enumeration elements()
Allow for the cache to be iterated on. This method is only used during debugging when validateCache() has been called to print out the contents of the cache.- Specified by:
elementsin interfaceIdentityMap
-
get
public Object get(Object primaryKey)
Return the object cached in the identity map or null if it could not be found. User API.- Specified by:
getin interfaceIdentityMap
-
getAllFromIdentityMapWithEntityPK
public abstract Map<Object,Object> getAllFromIdentityMapWithEntityPK(Object[] pkList, ClassDescriptor descriptor, AbstractSession session)
ADVANCED: Using a list of Entity PK this method will attempt to bulk load the entire list from the cache. In certain circumstances this can have large performance improvements over loading each item individually.- Specified by:
getAllFromIdentityMapWithEntityPKin interfaceIdentityMap- Parameters:
pkList- List of Entity PKs to extract from the cachedescriptor- Descriptor type to be retrieved.- Returns:
- Map of Entity PKs associated to the Entities that were retrieved
-
getAllCacheKeysFromIdentityMapWithEntityPK
public abstract Map<Object,CacheKey> getAllCacheKeysFromIdentityMapWithEntityPK(Object[] pkList, ClassDescriptor descriptor, AbstractSession session)
ADVANCED: Using a list of Entity PK this method will attempt to bulk load the entire list from the cache. In certain circumstances this can have large performance improvements over loading each item individually.- Specified by:
getAllCacheKeysFromIdentityMapWithEntityPKin interfaceIdentityMap- Parameters:
pkList- List of Entity PKs to extract from the cachedescriptor- Descriptor type to be retrieved.- Returns:
- Map of Entity PKs associated to the Entities that were retrieved
-
getCacheKey
public CacheKey getCacheKey(Object primaryKey, boolean forMerge)
Get the cache key (with object) for the primary key.- Specified by:
getCacheKeyin interfaceIdentityMap
-
getCacheKeyForLock
public CacheKey getCacheKeyForLock(Object primaryKey)
Get the cache key (with object) for the primary key.- Specified by:
getCacheKeyForLockin interfaceIdentityMap
-
getDescriptor
public ClassDescriptor getDescriptor()
Return the descriptor that this is the map for.- Specified by:
getDescriptorin interfaceIdentityMap
-
getDescriptorClass
public Class<?> getDescriptorClass()
Return the class that this is the map for.- Specified by:
getDescriptorClassin interfaceIdentityMap
-
getMaxSize
public int getMaxSize()
- Specified by:
getMaxSizein interfaceIdentityMap- Returns:
- The maxSize for the IdentityMap (NOTE: some subclasses may use this differently).
-
getSize
public int getSize()
Return the number of CacheKeys in the IdentityMap. This may contain weak referenced objects that have been garbage collected.- Specified by:
getSizein interfaceIdentityMap
-
getSize
public int getSize(Class<?> myClass, boolean recurse)
Return the number of actual objects of type myClass in the IdentityMap. Recurse = true will include subclasses of myClass in the count.- Specified by:
getSizein interfaceIdentityMap
-
getTargetIdenttyMap
public IdentityMap getTargetIdenttyMap()
Return the instance of the IdentityMap that this intercpetor is wrapping. Any call that is to be passed on to the underlying EclipseLink cache should be passed to this IdentityMap.
-
getWrapper
public Object getWrapper(Object primaryKey)
Get the wrapper object from the cache key associated with the given primary key, this is used for EJB2.- Specified by:
getWrapperin interfaceIdentityMap
-
getWriteLockValue
public Object getWriteLockValue(Object primaryKey)
Get the write lock value from the cache key associated to the primarykey. User API.- Specified by:
getWriteLockValuein interfaceIdentityMap
-
keys
public Enumeration<CacheKey> keys()
Allow for theCacheKeyelements to be iterated.CacheKeyCollectionis reused so this iteration may not be thread safe.- Specified by:
keysin interfaceIdentityMap- Returns:
EnumerationofCacheKeyinstances.
-
cloneKeys
public Enumeration<CacheKey> cloneKeys()
Allow for theCacheKeyelements to be iterated. Clone ofCacheKeyCollectionis returned so this iteration should be thread safe.- Specified by:
cloneKeysin interfaceIdentityMap- Returns:
Enumerationwith clone of theCacheKeyCollection
-
keys
public Enumeration<CacheKey> keys(boolean checkReadLocks)
Allow for theCacheKeyelements to be iterated.CacheKeyCollectionis reused so this iteration may not be thread safe.- Specified by:
keysin interfaceIdentityMap- Parameters:
checkReadLocks- value oftrueif read lock on theCacheKeyinstances should be checked orfalseotherwise- Returns:
EnumerationofCacheKeyinstances.
-
lazyRelationshipLoaded
public void lazyRelationshipLoaded(Object rootEntity, ValueHolderInterface valueHolder, ForeignReferenceMapping mapping)
Notify the cache that a lazy relationship has been triggered in the object and the cache may need to be updated- Specified by:
lazyRelationshipLoadedin interfaceIdentityMap
-
put
public CacheKey put(Object primaryKey, Object object, Object writeLockValue, long readTime)
Store the object in the cache at its primary key. This is used by InsertObjectQuery, typically into the UnitOfWork identity map. Merge and reads do not use put, but acquireLock. Also an advanced (very) user API.- Specified by:
putin interfaceIdentityMap- Parameters:
primaryKey- is the primary key for the object.object- is the domain object to cache.writeLockValue- is the current write lock value of object, if null the version is ignored.
-
remove
public Object remove(Object primaryKey, Object object)
Remove the CacheKey with the primaryKey from the map. This is used by DeleteObjectQuery and merge. This is also an advanced (very) user API.- Specified by:
removein interfaceIdentityMap
-
remove
public Object remove(CacheKey cacheKey)
Remove the CacheKey from the map.- Specified by:
removein interfaceIdentityMap
-
updateMaxSize
public void updateMaxSize(int maxSize)
This method will be used to update the max cache size, any objects exceeding the max cache size will be remove from the cache. Please note that this does not remove the object from the identityMap, except in the case of the CacheIdentityMap.- Specified by:
updateMaxSizein interfaceIdentityMap
-
setDescriptor
public void setDescriptor(ClassDescriptor descriptor)
Set the descriptor that this is the map for.- Specified by:
setDescriptorin interfaceIdentityMap
-
setWrapper
public void setWrapper(Object primaryKey, Object wrapper)
Update the wrapper object in the CacheKey associated with the given primaryKey, this is used for EJB2.- Specified by:
setWrapperin interfaceIdentityMap
-
setWriteLockValue
public void setWriteLockValue(Object primaryKey, Object writeLockValue)
Update the write lock value of the CacheKey associated with the given primaryKey. This is used by UpdateObjectQuery, and is also an advanced (very) user API.- Specified by:
setWriteLockValuein interfaceIdentityMap
-
toString
public String toString()
- Specified by:
toStringin interfaceIdentityMap- Overrides:
toStringin classObject
-
-