2012-11-28 2 views
0

현재 응용 프로그램에서 캐싱을 구현하는 데 EhCache의 엔터프라이즈 버전을 사용하고 있습니다. here를 설명했듯이, 난으로 Ehcache 생성을 관리하기 위해 사용하는 내 EHCache는 클래스에 다음 생성자를 사용하여 프로그래밍 방식으로 두 개의 서로 다른 캐시 인스턴스를 만드는 오전 :테라코타 클러스터에 두 개의 다른 캐시를 만들 때 NullPointerException이 발생했습니다.

public class EhCache implements ICacheAccess {  

    private String name; 
    private Cache ehCache; 
    private CacheAttributes attrs; 

    public EhCache(final String name, final CacheAttributes attrs) { 
       this.name = name; 
       this.attrs = attrs; 

       Configuration configuration = new Configuration(); 


       TerracottaClientConfiguration terracottaConfig 
        = new TerracottaClientConfiguration(); 

       configuration.addTerracottaConfig(terracottaConfig); 

       final CacheConfiguration cfg = new CacheConfiguration(name, attrs.cacheSize)   
        .eternal(attrs.eternal).terracotta(new TerracottaConfiguration()) 
        .timeToLiveSeconds(attrs.timeToLiveSeconds) 
        .timeToIdleSeconds(attrs.timeToIdleSeconds) 
        .statistics(attrs.statistics).overflowToOffHeap(true).maxBytesLocalOffHeap(200,MemoryUnit.MEGABYTES); 

       configuration.addCache(cfg);  


       CacheConfiguration defaultCache = new CacheConfiguration("default", 
         1000).eternal(false); 
       configuration.addDefaultCache(defaultCache); 

       CacheManager mgr = CacheManager.create(configuration);   
       ehCache = mgr.getCache(name);   
       LOGGER.log("ehcache is "+ehCache);   
      } 
} 

그때 내 EHCache는 클래스의 두 인스턴스를 만들려면 다음 방법을 사용 : 생성

testCreateCache("cache1"); 
testCreateCache("cache2"); 

캐시 1 :

public void testCreateCache(String name) { 
     CacheAttributes attrs = new CacheAttributes();   
       attrs.timeToIdleSeconds = 0; 
       attrs.timeToLiveSeconds = 0; 

     Cache cache = new EhCache(name, attrs); 
    } 

나는 내 주요 방법으로 두 번 위의 메소드를 호출 성공적으로 cache2는 null입니다.

나는 캐시를 생성하는 순서 교환하는 경우 :

testCreateCache("cache2"); 
testCreateCache("cache1"); 

캐시 2가 성공적으로 생성하지만 cache1가 null됩니다.

왜 이렇게되는지 이해할 수 없습니다. 첫 번째 캐시는 성공적으로 생성되지만 두 번째 캐시는 항상 null입니다.

답변

1

CacheManager가 Singleton이기 때문에 CacheManager.create()를 두 번 호출하는 것이 문제라고 생각합니다. 두 캐시를 모두 Configuration 개체에 추가 한 후에 한 번 호출 해보십시오.

관련 문제