2017-02-10 62 views
0

您好,我的项目是Struts2/Hibernate/Spring。 用下面的版本:Struts2/Hibernate/Spring项目与多个ehcache cacheManager

  1. 休眠5.2.5.Final
  2. Struts2的2.3.31
  3. 春4.3.5.RELEASE
  4. 的Ehcache 2.10.3

我们正在使用基于注释的配置。我们有以下的Ehcache类:

@Configuration 
@EnableCaching 
public class CacheConfig { 

@Bean 
public EhCacheCacheManager cacheManager() { 
    return new EhCacheCacheManager(ehCacheCacheManager().getObject()); 
} 

@Bean 
public EhCacheManagerFactoryBean ehCacheCacheManager() { 
    EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean(); 
    cmfb.setConfigLocation(new ClassPathResource("ehcache.xml")); 
    cmfb.setShared(true); 
    cmfb.setAcceptExisting(true); 
    return cmfb; 
} 

}

我们有一个通过石英cron作业称为主方法:

public static void main(String[] args) { 
    ApplicationContext context = new AnnotationConfigApplicationContext("com.abc"); 
    MyJobUtil myJob = new MyJobUtil(context); 

    ...our business logic code... 

    myJob.destroy(context); 
} 

的问题是,当主方法被触发我们运行的错误如下:

Caused by: net.sf.ehcache.CacheException: Another CacheManager with same name 'oddsCache' 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. 

我们搜索了论坛和将EhCacheManagerFactoryBean设置为share = true,但这种情况正在发生。任何建议/想法来解决这个问题? 谢谢。

回答

0

在您的cacheManager()方法中更改new CacheManager(Configuration configuration)CacheManager.newInstance(Configuration configuration)。请参考CacheManager Creation Modes