Skip to content

Commit 1dc79f7

Browse files
committed
Synchronized cache creation on CacheManager
Issue: SPR-11132 (cherry picked from commit de890fd)
1 parent c1b772c commit 1dc79f7

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -316,46 +316,48 @@ public void afterPropertiesSet() throws CacheException, IOException {
316316
this.cacheName = this.beanName;
317317
}
318318

319-
// Fetch cache region: If none with the given name exists,
320-
// create one on the fly.
321-
Ehcache rawCache;
322-
boolean cacheExists = this.cacheManager.cacheExists(cacheName);
323-
if (cacheExists) {
324-
if (logger.isDebugEnabled()) {
325-
logger.debug("Using existing EhCache cache region '" + this.cacheName + "'");
319+
synchronized (this.cacheManager) {
320+
// Fetch cache region: If none with the given name exists,
321+
// create one on the fly.
322+
Ehcache rawCache;
323+
boolean cacheExists = this.cacheManager.cacheExists(this.cacheName);
324+
if (cacheExists) {
325+
if (logger.isDebugEnabled()) {
326+
logger.debug("Using existing EhCache cache region '" + this.cacheName + "'");
327+
}
328+
rawCache = this.cacheManager.getEhcache(this.cacheName);
326329
}
327-
rawCache = this.cacheManager.getEhcache(this.cacheName);
328-
}
329-
else {
330-
if (logger.isDebugEnabled()) {
331-
logger.debug("Creating new EhCache cache region '" + this.cacheName + "'");
330+
else {
331+
if (logger.isDebugEnabled()) {
332+
logger.debug("Creating new EhCache cache region '" + this.cacheName + "'");
333+
}
334+
rawCache = createCache();
332335
}
333-
rawCache = createCache();
334-
}
335336

336-
if (this.cacheEventListeners != null) {
337-
for (CacheEventListener listener : this.cacheEventListeners) {
338-
rawCache.getCacheEventNotificationService().registerListener(listener);
337+
if (this.cacheEventListeners != null) {
338+
for (CacheEventListener listener : this.cacheEventListeners) {
339+
rawCache.getCacheEventNotificationService().registerListener(listener);
340+
}
341+
}
342+
if (this.statisticsEnabled) {
343+
rawCache.setStatisticsEnabled(true);
344+
}
345+
if (this.sampledStatisticsEnabled) {
346+
rawCache.setSampledStatisticsEnabled(true);
347+
}
348+
if (this.disabled) {
349+
rawCache.setDisabled(true);
339350
}
340-
}
341-
if (this.statisticsEnabled) {
342-
rawCache.setStatisticsEnabled(true);
343-
}
344-
if (this.sampledStatisticsEnabled) {
345-
rawCache.setSampledStatisticsEnabled(true);
346-
}
347-
if (this.disabled) {
348-
rawCache.setDisabled(true);
349-
}
350351

351-
if (!cacheExists) {
352-
this.cacheManager.addCache(rawCache);
353-
}
354-
Ehcache decoratedCache = decorateCache(rawCache);
355-
if (decoratedCache != rawCache) {
356-
this.cacheManager.replaceCacheWithDecoratedCache(rawCache, decoratedCache);
352+
if (!cacheExists) {
353+
this.cacheManager.addCache(rawCache);
354+
}
355+
Ehcache decoratedCache = decorateCache(rawCache);
356+
if (decoratedCache != rawCache) {
357+
this.cacheManager.replaceCacheWithDecoratedCache(rawCache, decoratedCache);
358+
}
359+
this.cache = decoratedCache;
357360
}
358-
this.cache = decoratedCache;
359361
}
360362

361363
/**

0 commit comments

Comments
 (0)