2017-12-06 23 views
1

나는 100 개가 넘는 도메인 모델을 가지고있는 애플리케이션을 가지고있다. ehcache를 통합하고 L2cache를 최대 절전 모드로 만들고 싶어, 내 애플리케이션은 ehcache를 캐시에 사용했다. 내 CacheConfiguration이Hibernate L2 ehcache와 spring boot enabledCache를 사용할 때 또 다른 무명의 CacheManager가 이미 존재한다

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 

같은 application.yml 변경 최대 절전 모드 캐시 설정이

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); 
     }; 
    } 

} 

같다하지만 난 내 응용 프로그램을 시작할 때 나는이 문제를 해결하는 방법이 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 

두 캐시 관리자가 존재하는 이유는 무엇입니까? 최대 절전 모드와 메소드 모두에 대해 하나의 캐시 관리자를 사용하도록 구성을 변경하는 방법은 무엇입니까?

+0

다른 CacheManager가 자동 구성되어 있는지 확인합니다. Spring Boot는 클래스 패스에있는 것을 기반으로 자동 설정을하며 때로는 이것이 원하는 것이 아닙니다. 이 사실이 귀하의 사례에 적용되는지 여부는 확실하지 않지만 체크 아웃 할 가치가 있습니다. 또한 명명 충돌이있는 것으로 보아서 빈에 @Qualifier를 사용하여 문제를 해결할 수 있어야합니다. – Kristoffer

+0

캐시 관리자 직접, 최대 절전 모드 및 봄 캐시 관리자를 사용하지 마십시오. –

답변

2

최근에이 문제가 발생했습니다. Soln은 최대 절전 모드 factory_class로 "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"를 사용합니다.

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