Skip to content

Commit d66d160

Browse files
committed
Use ConcurrentHashMaps in DefaultContextCache
The changes made in 0cb22fc 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
1 parent c52a0cc commit d66d160

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

spring-test/src/main/java/org/springframework/test/context/support/DefaultContextCache.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,29 @@
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.Set;
24+
import java.util.concurrent.ConcurrentHashMap;
2425
import java.util.concurrent.atomic.AtomicInteger;
2526

2627
import org.apache.commons.logging.Log;
2728
import org.apache.commons.logging.LogFactory;
29+
2830
import org.springframework.context.ApplicationContext;
2931
import org.springframework.context.ConfigurableApplicationContext;
3032
import org.springframework.core.style.ToStringCreator;
3133
import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
3234
import org.springframework.test.context.ContextCache;
3335
import org.springframework.test.context.MergedContextConfiguration;
3436
import org.springframework.util.Assert;
35-
import org.springframework.util.ConcurrentReferenceHashMap;
3637

3738
/**
3839
* Default implementation of the {@link ContextCache} API.
3940
*
40-
* <p>Uses Spring's {@link ConcurrentReferenceHashMap} to store
41-
* {@linkplain java.lang.ref.SoftReference soft references} to cached
42-
* contexts and {@code MergedContextConfiguration} instances.
41+
* <p>Uses {@link ConcurrentHashMap ConcurrentHashMaps} to cache
42+
* {@link ApplicationContext} and {@link MergedContextConfiguration} instances.
4343
*
4444
* @author Sam Brannen
4545
* @author Juergen Hoeller
4646
* @since 2.5
47-
* @see ConcurrentReferenceHashMap
4847
*/
4948
public class DefaultContextCache implements ContextCache {
5049

@@ -54,7 +53,7 @@ public class DefaultContextCache implements ContextCache {
5453
* Map of context keys to Spring {@code ApplicationContext} instances.
5554
*/
5655
private final Map<MergedContextConfiguration, ApplicationContext> contextMap =
57-
new ConcurrentReferenceHashMap<MergedContextConfiguration, ApplicationContext>(64);
56+
new ConcurrentHashMap<MergedContextConfiguration, ApplicationContext>(64);
5857

5958
/**
6059
* Map of parent keys to sets of children keys, representing a top-down <em>tree</em>
@@ -63,7 +62,7 @@ public class DefaultContextCache implements ContextCache {
6362
* of other contexts.
6463
*/
6564
private final Map<MergedContextConfiguration, Set<MergedContextConfiguration>> hierarchyMap =
66-
new ConcurrentReferenceHashMap<MergedContextConfiguration, Set<MergedContextConfiguration>>(64);
65+
new ConcurrentHashMap<MergedContextConfiguration, Set<MergedContextConfiguration>>(64);
6766

6867
private final AtomicInteger hitCount = new AtomicInteger();
6968

0 commit comments

Comments
 (0)