使用Hibernate L2 EHCache和Spring启动EnabledCache时,另一种未命名的CacheMan



我有一个应用程序,该应用程序具有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);
        };
    }
}

在应用程序中。

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个CacheManager如何解决此问题的例外。

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"作为Hibernate Factory_class。

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

相关内容

最新更新