天天看點

緩存架構Ehcache學習(一)建立多個CacheManager抛出異常

最近在研究緩存架構Ehcache的配置和使用,我想根據多個ehcache.xml建立多個CacheManager對象,但是報了異常:

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.

錯誤提示很明顯:Ehcache不允許建立同樣名稱的CacheManager對象。如果我們沒有ehcache.xml中配置CacaheManager的名稱,那麼預設的名稱是__DEFAULT__。解決方式是<ehcache name="">中配置CacheManager的名稱,并確定唯一。這樣如下代碼就不會報錯了

URL url = CacheHelper.class.getClassLoader().getResource("ehcache.xml");
 CacheManager manager = new CacheManager(url);
  // __DEFAULT__
 System.out.println(manager.getName());

 URL url2 = CacheHelper.class.getClassLoader().getResource("ehcache2.xml");
		
 CacheManager manager2 = new CacheManager(url2);
           
上一篇: 今天的收獲
下一篇: 滿滿的收獲