hibernate3与ehcache-2.8.3配合使用,在多个SessionFactory实例的情况下出现“Another unnamed CacheManager already exists in the same VM”问题
不想看分析过程的,直接拉到最下面看第4部分
1. 问题背景
由于某些原因,项目中使用了hibernate3与ehcache-2.8.3在配合使用,又由于某些原因,需要使用多个SessionFactory实例。在这个背景下,启动项目报错,具体异常如下:
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:591)
at net.sf.ehcache.CacheManager.init(CacheManager.java:385)
at net.sf.ehcache.CacheManager.<init>(CacheManager.java:369)
at org.hibernate.cache.EhCacheProvider.start(EhCacheProvider.java:124)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:183)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
大意就是同一个vm中有两个未命名的CacheManager存。具体代码细节可以根据上面的报错堆栈很容易发现:
private void assertNoCacheManagerExistsWithSameName(Configuration configuration) {
synchronized (CacheManager.class) {
final String name;
final boolean isNamed;
if (configuration.getName() != null) {
name = configuration.getName();
isNamed = true;
} else {
name = DEFAULT_NAME;
isNamed = false;
}
CacheManager cacheManager = CACHE_MANAGERS_MAP.get(name);
if (cacheManager == null) {
CACHE_MANAGERS_MAP.put(name, this);
CACHE_MANAGERS_REVERSE_MAP.put(this, name);
} else {
ConfigurationSource configurationSource = cacheManager.getConfiguration().getConfigurationSource();
final String msg = "Another "
+ (isNamed ? "CacheManager with same name '" + name + "'" : "unnamed CacheManager")
+ " already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:\n"
+ "1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name"
+ " or create one if necessary\n"
+ "2. Shutdown the earlier cacheManager before creating new one with same name.\n"
+ "The source of the existing CacheManager is: "
+ (configurationSource == null ? "[Programmatically configured]" : configurationSource);
throw new CacheException(msg);
}
}
}
2. 通过hibernate的配置项”hibernate.cache.provider_configuration_file_resource_path”单独指定SessionFactory每个实例的ehcache配置文件
hibernate与ehcahe对接的地方能发现些什么?
观察堆栈:
at org.hibernate.cache.EhCacheProvider.start(EhCacheProvider.java:124)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:183)
发先如下代码:
String configurationResourceName = null;
if (properties != null) {
configurationResourceName = (String) properties.get( Environment.CACHE_PROVIDER_CONFIG );
}
if ( StringHelper.isEmpty( configurationResourceName ) ) {
manager = new CacheManager();
} else {
URL url = loadResource(configurationResourceName);
manager = new CacheManager(url);
}
这里有重要的一个发现:
初始化SessionFactory实例时启动EhCacheProvider
启动EhCacheProvider时创建CacheManager实例,
创建CacheManager实例时可以通过hibernate的配置项hibernate.cache.provider_configuration_file_resource_path来决定CacheManager的配置
hibernate.cache.provider_configuration_file_resource_path这个配置项的值是什么形式?
分析org.hibernate.util.ConfigHelper.locateConfig(String)代码:
public static final URL locateConfig(final String path) {
try {
return new URL(path);
}
catch(MalformedURLException e) {
return findAsResource(path);
}
}
不难发现:
1).优先用标准的url形式查找。
2).失败后用classpath的方式查找。
3. 如何在ehcache的xml配置文件中指定CacheManager实例的名字
分析1中的assertNoCacheManagerExistsWithSameName代码发现cacheManager的name是从net.sf.ehcache.config.Configuration获取的。
net.sf.ehcache.config.Configuration实例又是依赖SAX解析配置文件xml实现的。
SAX解析需要配合handler使用,这里是net.sf.ehcache.config.BeanHandler这个类。
调试BeanHandler后发现,Configuration实例的cacheManagerName就是配置文件中ehcache xml配置文件中ehcache标签的name属性的值。
<ehcache name="jbpmEhcacheManager">
4. 总结解决办法:
1). 在classpath下放置多个ehcachexxx.xml配置文件(有多少个SessionFactory实例就放置多少个,文件名不同),每个配置文件中ehcache标签的name属性值要唯一。
2). 在每个SessionFactory实例装配时(如果编程式的就用api指定),指定hibernate.cache.provider_configuration_file_resource_path的值为相应的ehcache配置名
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop> <!-- hibernate3 配合ehcache2.5之后的版本使用,对于多个SessionFactory实例,需要为每个SessionFactory实例指定独立的ehcache的配置文件 -->
<!-- hibernate3对这一配置项的路径寻找方式为:1.优先用标准的url形式查找。2.失败后用classpath的方式查找。此处使用2方式。 -->
<prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache4common.xml</prop>
--EOF--
hibernate3与ehcache-2.8.3配合使用,在多个SessionFactory实例的情况下出现“Another unnamed CacheManager already exists in the same VM”问题的更多相关文章
- spring整合ehcache2.5.2缓存异常-- net.sf.ehcache.CacheException
报错如下: The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcach ...
- ehcache hibernate4整合问题
当在原有hibernate使用ehcache缓存的项目中加入ehcache时,报如下错误时 Caused by: org.hibernate.service.spi.ServiceException: ...
- SpringMVC + Mybatis + Shiro + ehcache时缓存管理器报错。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroFilter' ...
- 解决spring+shiro cacheManager 登录报错
一.项目启动,登录报错 org.springframework.beans.factory.BeanCreationException: Error creating bean with name ' ...
- 关于Springboot 中注入多个cacheManage 时候 存在报错
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'e ...
- cacheManager载入问题
net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please pr ...
- Shiro+SpringMVC 实现更安全的登录(加密匹配&登录失败超次数锁定帐号)
原文:http://blog.csdn.net/wlwlwlwl015/article/details/48518003 前言 初学shiro,shiro提供了一系列安全相关的解决方案,根据官方的介绍 ...
- Another unnamed CacheManager already exists in the same VM
今天学习Spring 缓存机制.遇到不少问题~ 好不easy缓存的单元測试用例调试成功了,在同一项目下单元測试另外一个文件时,发生了异常: org.springframework.beans.fact ...
- shiro中ecache-core版本引起的异常
ecache-core包版本不对引起的错误,将2.5.3换成2.4.5就好了 来源 WARN [RMI TCP Connection(3)-127.0.0.1] - Exception encount ...
随机推荐
- ThinkPHP 模板截取字符串 【转载】
对于英文字符可使用如下形式(模板中): {$vo.title|substr=,} 如果是中文字符thinkphp提供了msubstr,用法如下: function msubstr($str, $sta ...
- IT运维的五大基础知识
IT运维的五大基础知识 | 浏览:331 | 更新:2014-09-25 11:36 IT运维对于很多企业都很重要,接下来运维的一些基础知识天天客服IT运维总监龙少文,就给大家介绍下IT运维的基础知识 ...
- Bootstrap页面布局11 - BS表单
表单之 文本框 text <input type='text' value='' placeholder='输入您的用户名' class='input-mini' /> ①几个类控制文本框 ...
- jQuery之换肤与cookie插件
有时候一个网页可以有多个皮肤进行选择,也就是不同的背景,或是一整套新的css,能使整个页面变成另一种风格. 这个功能可以用jQuery来实现.外加cookie插件.有了cookie,就可以长时间的保存 ...
- Oracle数值处理函数 (绝对值、取整...)
1.绝对值:abs() select abs(-2) value from dual; 2.取整函数(大):ceil() select ceil(-2.001) value from du ...
- Apache Camel
Apache Camel 1 import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; i ...
- SQL Server 触发器【转】
触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程.触发器主要是通过事件进行触发被自动调用执行的.而存储过程可以通过存储过程的名称被调用. Ø 什么是触发器 触发器对表进行插入.更新.删 ...
- python的变量
Python变量 在Python中,变量的概念基本上和初中代数的方程变量是一致的.例如,对于方程式 y=x*x ,x就是变量.当x=2时,计算结果是4,当x=5时,计算结果是25. 只是在计算机程序中 ...
- hibernate manytoone中的lazy EAGER
Hibernate中的字段映射中的Fetch有两种方式:EAGER和LAZY Eager:全部抓取 Lazy:延迟抓取 如果在字段中声明为Eager,那么在取得当前Bean时,同时会抓取Bean中的关 ...
- 切记CMYK图片格式在IE中将无法显示
目前为止微软的Internet Explorer 浏览器IE6,IE7,IE8都不支持CMYK颜色模式图像 ,除IE外其他浏览器均能支持!所以大家要注意了 要选择RGB颜色模式,就可以了.