Skip to content

Commit

Permalink
SDO helper context removal additional methods (#1915)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfelcman committed Oct 16, 2023
1 parent d5b1cbe commit d02ecb8
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -43,6 +44,7 @@
import org.eclipse.persistence.exceptions.SDOException;
import org.eclipse.persistence.internal.helper.Helper;
import org.eclipse.persistence.internal.identitymaps.CacheIdentityMap;
import org.eclipse.persistence.internal.identitymaps.CacheKey;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
import org.eclipse.persistence.internal.security.PrivilegedGetMethod;
import org.eclipse.persistence.sdo.SDOConstants;
Expand Down Expand Up @@ -1496,4 +1498,47 @@ public static Map<SDOWrapperTypeId,SDOWrapperType> getWrapperTypes() {
public static Map<SDOWrapperTypeId,SDOWrapperType> putWrapperTypes(Map<SDOWrapperTypeId,SDOWrapperType> wrapperTypes) {
return SDO_WRAPPER_TYPES.put(getApplicationName(Thread.currentThread().getContextClassLoader()), wrapperTypes);
}

/**
* ADVANCED:
* Remove the HelperContext for the specified key and expiration time.
* This is mainly focused to free some JEE server memory.
* WARNING: Use it carefully! Application must register it's application helper context if it will be needed again after removal.
*
* @param key helperContext identifier. It's usually application classloader or custom identifier.
* @param expirationTime defines age of helper context records. Older one will be removed. It's compared against creation time.
* @return number of removed helper context records
*/
public static int removeExpiredHelperContexts(Object key, Instant expirationTime) {
int removedCounter = 0;
CacheIdentityMap contextMap = helperContexts.get(key);
if (contextMap != null) {
Map<Object, CacheKey> cacheKeys = contextMap.getCacheKeys();
for (Object key0: cacheKeys.keySet()) {
CacheKey cacheKey = cacheKeys.get(key0);
if (cacheKey.getConcurrencyManagerCreationDate().getTime() < expirationTime.getEpochSecond() * 1000) {
cacheKeys.remove(key0);
removedCounter++;
}
}
}
return removedCounter;
}

/**
* ADVANCED:
* Remove the HelperContext for the specified expiration time. It's applied across all applications.
* This is mainly focused to free some JEE server memory.
* WARNING: Use it carefully! Applications must register it's application helper context if it will be needed again after removal.
*
* @param expirationTime defines age of helper context records. Older one will be removed. It's compared against creation time.
* @return number of removed helper context records
*/
public static int removeAllExpiredHelperContexts(Instant expirationTime) {
int removedCounter = 0;
for (Object key: helperContexts.keySet()) {
removedCounter += removeExpiredHelperContexts(key, expirationTime);
}
return removedCounter;
}
}

0 comments on commit d02ecb8

Please sign in to comment.