Ehcache is registering beans two times

UPDATE: this problem should be resolved in the (upcoming) version 3.9.7, as per the issue mentioned below. Update your ehcache version or track the version releases in order to have the issue fixed.

After some investigation this seems to be because of the following bug: CacheConfiguration and CacheStatistics MBeans are registered again and again

What happens is that we want to use the combination of Spring and EhCache 3.

Unfortunately there is no org.springframework.cache.CacheManager available for EhCache 3 in spring at this moment (only for v2, i.e. org.springframework.cache.ehcache.EhCacheCacheManager), so we have to fall back to using the JCacheCacheManager.

The JCacheCacheManager has to initialize its caches (these are ~ proxying caches) internally as well, which is done through the afterPropertiesSet() method inherited from InitializingBean. Upon calling this afterPropertiesSet(), after some indirection the loadCaches() method is called, in which the following snippet of code occurs:

    for (String cacheName : cacheManager.getCacheNames()) {
        javax.cache.Cache<Object, Object> jcache = cacheManager.getCache(cacheName);
        caches.add(new JCacheCache(jcache, isAllowNullValues()));

The relevant part is basically this: cacheManager.getCacheNames().

As can be read in the bug report, this method mistakenly disrespects the no-side-effects principle and re-initializes the caches.

TLDR: This is a known bug in EHCache v3, and an issue exists already. Maybe some day we will see a fix. I have also commented on the issue with a reference to this answer.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top