From d66d1605430c73444082d219214edb74327d3c13 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 19 Apr 2015 21:06:41 +0200 Subject: [PATCH] Use ConcurrentHashMaps in DefaultContextCache The changes made in 0cb22fc8f34a29c42313056671a947444b60050b would result in contexts not being properly closed if evicted from the ConcurrentReferenceHashMap by the Garbage Collector. This commit reverts those changes and returns to using standard ConcurrentHashMaps for the time being. Issue: SPR-7687 --- .../test/context/support/DefaultContextCache.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/support/DefaultContextCache.java b/spring-test/src/main/java/org/springframework/test/context/support/DefaultContextCache.java index bcb5c21df4..511a85f611 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/DefaultContextCache.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/DefaultContextCache.java @@ -21,10 +21,12 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.style.ToStringCreator; @@ -32,19 +34,16 @@ import org.springframework.test.annotation.DirtiesContext.HierarchyMode; import org.springframework.test.context.ContextCache; import org.springframework.test.context.MergedContextConfiguration; import org.springframework.util.Assert; -import org.springframework.util.ConcurrentReferenceHashMap; /** * Default implementation of the {@link ContextCache} API. * - *

Uses Spring's {@link ConcurrentReferenceHashMap} to store - * {@linkplain java.lang.ref.SoftReference soft references} to cached - * contexts and {@code MergedContextConfiguration} instances. + *

Uses {@link ConcurrentHashMap ConcurrentHashMaps} to cache + * {@link ApplicationContext} and {@link MergedContextConfiguration} instances. * * @author Sam Brannen * @author Juergen Hoeller * @since 2.5 - * @see ConcurrentReferenceHashMap */ public class DefaultContextCache implements ContextCache { @@ -54,7 +53,7 @@ public class DefaultContextCache implements ContextCache { * Map of context keys to Spring {@code ApplicationContext} instances. */ private final Map contextMap = - new ConcurrentReferenceHashMap(64); + new ConcurrentHashMap(64); /** * Map of parent keys to sets of children keys, representing a top-down tree @@ -63,7 +62,7 @@ public class DefaultContextCache implements ContextCache { * of other contexts. */ private final Map> hierarchyMap = - new ConcurrentReferenceHashMap>(64); + new ConcurrentHashMap>(64); private final AtomicInteger hitCount = new AtomicInteger();