Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ehcache 3 spring boot not working in integration test

I need your help please. I integrated Ehcache 3 with springboot 2. everything works perfectly in production. integration tests also work if i execute them manualy ( I'm using Intellij IDE ). but when i run "mvn install", i get this error :

java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehCacheManager' defined in class path resource [com/afklm/belem/payment/config/EhCacheConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cache.CacheManager]: Factory method 'ehCacheManager' threw exception; nested exception is javax.cache.CacheException: A Cache named [currenciesCache] already exists

CurrencyRepository interface :

@Repository
public interface CurrencyRepository extends JpaRepository<Currency, String> {
    /**
     * Gets currencies from Cache.
     * The real call to the DataBase will be performed if the response is not in cache
     *
     * @return list of {@link Currency}
     */
    @Cacheable(cacheNames = "currenciesCache", key ="#root.methodName", unless = "#result==null or #result.size()==0")
    default List<Currency> getCachedCurrencies() {
        return findAll();
    }

Cache configuration class

import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.jsr107.Eh107Configuration;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.cache.CacheManager;

import javax.cache.Caching;
import java.time.Duration;
import java.util.List;


@EnableCaching
@Configuration
public class EhCacheConfiguration extends CachingConfigurerSupport {

    @Bean
    public CacheManager ehCacheManager() {

        //-> currencies cache configuration
        CacheConfiguration<String, List> currenciesCacheConfig =
                CacheConfigurationBuilder
                        .newCacheConfigurationBuilder(String.class, List.class, ResourcePoolsBuilder.heap(1000))
                        .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofDays(30)))
                        .withDefaultEventListenersThreadPool()
                        .build();

        javax.cache.CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();

        cacheManager.createCache("currenciesCache", Eh107Configuration.fromEhcacheCacheConfiguration(currenciesCacheConfig));

  

        return  new JCacheCacheManager(cacheManager);
    }

Thanks for your help; i'm stuck

like image 692
Alae Annassi Avatar asked May 07 '26 16:05

Alae Annassi


1 Answers

I think I faced this before when at least one of my tests had the @DirtiesContext annotation which re-initializes the Spring context. I got around it by making a call to the cacheManager.destroyCache method before attempting to create the cache. Can you try adding the statement cacheManager.destroyCache("currenciesCache"); before the cacheManager.createCache statement? Alternatively, you could try initializing the cache manager without the explicit call to the createCache method like it has been done in this Stack Overflow answer

like image 136
devatherock Avatar answered May 10 '26 08:05

devatherock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!