From 1dc79f7f9cdf916dbd4c88a9fdedc77d1aebe6c9 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 2 Dec 2013 16:10:44 +0100 Subject: [PATCH] Synchronized cache creation on CacheManager Issue: SPR-11132 (cherry picked from commit de890fd) --- .../cache/ehcache/EhCacheFactoryBean.java | 70 ++++++++++--------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java index 02f7f38396a..70dfe2a0f4a 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java +++ b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java @@ -316,46 +316,48 @@ public class EhCacheFactoryBean implements FactoryBean, BeanNameAware, this.cacheName = this.beanName; } - // Fetch cache region: If none with the given name exists, - // create one on the fly. - Ehcache rawCache; - boolean cacheExists = this.cacheManager.cacheExists(cacheName); - if (cacheExists) { - if (logger.isDebugEnabled()) { - logger.debug("Using existing EhCache cache region '" + this.cacheName + "'"); + synchronized (this.cacheManager) { + // Fetch cache region: If none with the given name exists, + // create one on the fly. + Ehcache rawCache; + boolean cacheExists = this.cacheManager.cacheExists(this.cacheName); + if (cacheExists) { + if (logger.isDebugEnabled()) { + logger.debug("Using existing EhCache cache region '" + this.cacheName + "'"); + } + rawCache = this.cacheManager.getEhcache(this.cacheName); } - rawCache = this.cacheManager.getEhcache(this.cacheName); - } - else { - if (logger.isDebugEnabled()) { - logger.debug("Creating new EhCache cache region '" + this.cacheName + "'"); + else { + if (logger.isDebugEnabled()) { + logger.debug("Creating new EhCache cache region '" + this.cacheName + "'"); + } + rawCache = createCache(); } - rawCache = createCache(); - } - if (this.cacheEventListeners != null) { - for (CacheEventListener listener : this.cacheEventListeners) { - rawCache.getCacheEventNotificationService().registerListener(listener); + if (this.cacheEventListeners != null) { + for (CacheEventListener listener : this.cacheEventListeners) { + rawCache.getCacheEventNotificationService().registerListener(listener); + } + } + if (this.statisticsEnabled) { + rawCache.setStatisticsEnabled(true); + } + if (this.sampledStatisticsEnabled) { + rawCache.setSampledStatisticsEnabled(true); + } + if (this.disabled) { + rawCache.setDisabled(true); } - } - if (this.statisticsEnabled) { - rawCache.setStatisticsEnabled(true); - } - if (this.sampledStatisticsEnabled) { - rawCache.setSampledStatisticsEnabled(true); - } - if (this.disabled) { - rawCache.setDisabled(true); - } - if (!cacheExists) { - this.cacheManager.addCache(rawCache); + if (!cacheExists) { + this.cacheManager.addCache(rawCache); + } + Ehcache decoratedCache = decorateCache(rawCache); + if (decoratedCache != rawCache) { + this.cacheManager.replaceCacheWithDecoratedCache(rawCache, decoratedCache); + } + this.cache = decoratedCache; } - Ehcache decoratedCache = decorateCache(rawCache); - if (decoratedCache != rawCache) { - this.cacheManager.replaceCacheWithDecoratedCache(rawCache, decoratedCache); - } - this.cache = decoratedCache; } /**