使用 Hibernate L2 ehcache 和 spring boot enabledCache 时,另一个未命名的 CacheManager 已经存在

Another unnamed CacheManager already exists when using Hibernate L2 ehcache and spring boot enabledCache

我有一个包含 100 多个域模型的应用程序,我想集成 ehcache 和 hibernate L2cache,我的应用程序使用 ehcache 来缓存一些服务的方法。我的 CacheConfiguration 是这样的

 package org.roshan.framework.config;

import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.expiry.Duration;
import org.ehcache.expiry.Expirations;
import org.ehcache.jsr107.Eh107Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PreDestroy;
import java.util.concurrent.TimeUnit;


@Configuration
@EnableCaching
@AutoConfigureAfter(value = {DatabaseConfiguration.class})
public class CacheConfiguration {

    private final Logger log = LoggerFactory.getLogger(CacheConfiguration.class);
    private final javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration;


    @PreDestroy
    public void destroy() {
        log.info("Remove Cache Manager metrics");
        log.info("Closing Cache Manager");
    }

    public CacheConfiguration(JHipsterProperties jHipsterProperties) {
        JHipsterProperties.Cache.Ehcache ehcache = jHipsterProperties.getCache().getEhcache();

        jcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
                CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
                        ResourcePoolsBuilder.heap(ehcache.getMaxEntries()))
                        .withExpiry(Expirations.timeToLiveExpiration(Duration.of(ehcache.getTimeToLiveSeconds(), TimeUnit.SECONDS)))
                        .build()
        );
    }

    @Bean
    public JCacheManagerCustomizer cacheManagerCustomizer() {
        log.debug("Starting Ehcache");

        return cm -> {
            // some cache for using in method of service
            cm.createCache("baseInfoCache", jcacheConfiguration);
            cm.createCache("attachments", jcacheConfiguration);
        };
    }

}

在 application.yml 中像这样更改休眠缓存配置

spring:
    devtools:
        restart:
            enabled: true
        livereload:
            enabled: true # we use gulp + BrowserSync for livereload
    application:
        name: roshanframework
    jpa:
        open-in-view: true
        hibernate:
            ddl-auto: none
        properties:
              hibernate.cache.use_second_level_cache: true
              hibernate.cache.use_query_cache: false
              hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.EhCacheRegionFactory
              hibernate.generate_statistics: false
              hibernate.dialect : org.hibernate.dialect.MySQL5Dialect
              #hibernate.default_schema : ihsl
              hibernate.show_sql : true
              hibernate.current_session_context_class:  org.springframework.orm.hibernate5.SpringSessionContext

但是当我启动我的应用程序时出现异常,我有 2 个缓存管理器如何解决这个问题。

The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ]
    at org.hibernate.cache.ehcache.EhCacheRegionFactory.start(EhCacheRegionFactory.java:90)
    at org.hibernate.cache.spi.RegionFactory.start(RegionFactory.java:63)
    at org.hibernate.internal.CacheImpl.<init>(CacheImpl.java:71)
    at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:28)
    at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:20)
    at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.initiateService(SessionFactoryServiceRegistryImpl.java:58)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259)
    ... 45 common frames omitted
Caused by: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ]
    at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:626)
    at net.sf.ehcache.CacheManager.init(CacheManager.java:391)
    at net.sf.ehcache.CacheManager.<init>(CacheManager.java:269)
    at org.hibernate.cache.ehcache.EhCacheRegionFactory.start(EhCacheRegionFactory.java:69)
    ... 51 common frames omitted

我不知道为什么存在两个缓存管理器?如何将配置更改为对休眠和方法使用同一个缓存管理器?

我最近也运行遇到了这个问题。 soln是使用"org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"作为hibernatefactory_class.

spring:
    jpa:
      properties:
        hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory