Ehcache配置时出错- Another unnamed CacheManager already exists in the same VM. Please provide unique name

shiro权限框架课程目标精讲篇(1)
shiro初识,全面介绍架构精讲篇
shiro三大核心组件介绍精讲篇
shiro配置jar包导入依赖精讲篇
shiro认证权限流程测试精讲篇
shiro权限授权测试精讲篇
shiro权限模型构建数据库表精讲篇
shiro权限框架自定义realm认证测试精讲篇
shiro散列算法密码加盐测试精讲篇
shiro权限管理自定义realm授权测试精讲篇
spring集成shiro不进入自定义realm是为什么?
shiro认证:连接数据库操作doGetAuthenticationInfo
shiro授权:链接数据库doGetAuthorizationInfo
shiro框架:缓存器
Ehcache配置时出错- Another unnamed CacheManager already exists in the same VM. Please provide unique name
shiro框架:缓存器Ehcache的详细配置流程
shiro框架:sessionManager设置数据会话的存储时间,账号密码到时间清空
shiro框架集成验证码验证配置流程
shiro框架:记住我remberMe自动登入
hibernate的一个错误。可能发生在hibernate更换版本或者整合其它框架时。我发生在springData整合JPA(hibernate是JPA的一个实现,所以要使用hibernate的包)

错误重现

先把详细错误放在这里,错误之后有解析思路:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5118)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5634)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1863)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1487)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:97)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1328)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1420)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:848)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
at sun.rmi.transport.Transport$2.run(Transport.java:202)
at sun.rmi.transport.Transport$2.run(Transport.java:199)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:198)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:567)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:828)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.access$400(TCPTransport.java:619)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:684)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:681)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:681)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:953)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:883)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 59 more
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.spi.CacheImplementor]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:264)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:228)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207)
at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.getService(SessionFactoryServiceRegistryImpl.java:68)
at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:241)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:465)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:880)
... 65 more
Caused by: org.hibernate.cache.CacheException: 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: InputStreamConfigurationSource [stream=java.io.BufferedInputStream@4fad14cc]
at org.hibernate.cache.ehcache.EhCacheRegionFactory.start(EhCacheRegionFactory.java:90)
at org.hibernate.internal.CacheImpl.(CacheImpl.java:49)
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:49)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:254)
... 71 more
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: InputStreamConfigurationSource [stream=java.io.BufferedInputStream@4fad14cc]
at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:628)
at net.sf.ehcache.CacheManager.init(CacheManager.java:392)
at net.sf.ehcache.CacheManager.(CacheManager.java:270)
at org.hibernate.cache.ehcache.EhCacheRegionFactory.start(EhCacheRegionFactory.java:69)
... 76 more

由于错误解决还是花费了一定的实践,所以把过程思路记录。

分析
根据错误提示,就是VM(虚拟机)中已经存在CacheManager,再次创建相同的name的CacheManager,就报错。有建议就是:

1,通过静态工厂方法来根据需要创建需要的类型的cachemanager,应该就是多态;

2,关闭,也就是移除早版本的cacheManager,然后创建最新的。

这个错误与提示其实让我们看了导入的包就知道有点怪吖,如我的包:基于maven:


org.hibernate
hibernate-core
5.1.6.Final



org.hibernate
hibernate-entitymanager
5.1.6.Final



org.hibernate
hibernate-ehcache
5.1.6.Final

那么关于cache的也就是hibernate-ehcache这个包有关。这个包有什么问题吗?感觉没有吖,那去看看maven帮我下载的这个包里面有什么,怎么就会有same的cacheManager这样的错误呢?


Ehcache配置时出错- Another unnamed CacheManager already exists in the same VM. Please provide unique name_第1张图片
image.png

这一看只有一个Singleton..的类有点趣,把这几个类都看一下,基本可以确定,cacheManager在此版本似乎变得是单例了。额,那么真正实现cache的应该另有包。怎么查看呢,想到这是从maven仓库中下来的,那么,当初引入这个依赖的时候,就有没有注意到缓存的实现有没有一起依赖荡下来。那么查看maven的dependency:

Ehcache配置时出错- Another unnamed CacheManager already exists in the same VM. Please provide unique name_第2张图片
image.png

可知这三个包都会被依赖下载下来,所以去找找这个ehcache它的cacheManager内部看看。

一找果真找到了惊喜:

image.png

如图有两个ehcache相关的包,那这两个包有什么区别呢?

查阅资料得知:

Ehcache的加载模块列表,他们都是独立的库,每个都为Ehcache添加新的功能:

ehcache-core:API,标准缓存引擎,RMI复制和Hibernate支持
ehcache:分布式Ehcache,包括Ehcache的核心和Terracotta的库
查看两个包的内容:

Ehcache配置时出错- Another unnamed CacheManager already exists in the same VM. Please provide unique name_第3张图片
image.png

可知ehcache包中基本拥有了ehcache-core包中所有内容。所以说报这种让我们2选一的错非常正常。因为当一个包中的cacheManager类被加载到VM后,再想加载第二次其它包中的这个类,就出这样的错。

另一小记:

这里在后来换思路排错时发现,直接从错误日志中入手查看cacheManager类就好了,这样可以很快的发现有两个包:

Ehcache配置时出错- Another unnamed CacheManager already exists in the same VM. Please provide unique name_第4张图片
image.png

解决

问题根源就在于此,出现了相同的包,但是如何解决?

分清楚两个包分别是为什么导入进来的,是否有哪个包可以不引入?

首先翻阅pom.xml,了解到因为引入了shiro安全框架,也引入了shiro的缓存机制并且也交给了spring管理。如下引入:


org.apache.shiro
shiro-ehcache
1.3.2

它就依赖:

那么解决就是让shiro-ehcache或hibernate-ehcache放弃一个,这里留下轻量级的,因为我的系统不需要通过ehcache实现分布式缓存,所以包越小越好。这里也是为了引出另一个问题。

那么我就在hibernate-ehcache中exculde 这个ehcache即可:


org.hibernate
hibernate-ehcache
5.1.6.Final


net.sf.ehcache
ehcache



此时可以看到此前的ehcache包没有了。这应该不会出现2选一的错误了吧,再次启动服务器
错误依然如此。难道不是这个原因??不,这应该算是一个原因,只不过还没有了解另一个问题。现在只有一个ehcache-core-2.5.3.jar,还出现same name ...的问题。让我想到了此前看到Single...这个类,那么,没办法再找找前辈有没有遇到这样的问题,大不了看官方文档有什么使用说明!

看官方的文档说明:

Versions of Ehcache before version 2.5 allowed any number of CacheManagers with the same name (same configuration resource) to exist in a JVM.

Ehcache 2.5 and higher does not allow multiple CacheManagers with the same name to exist in the same JVM. CacheManager() constructors creating non-Singleton CacheManagers can violate this rule

解释:

ehcache在2.5以后,CacheManager使用了Singleton,这样在创建多个CacheManager时就会产生上述错误。
http://ehcache.org/documentation/get-started/concepts 是官方解释
可以导入ehcache-core-2.4.5.jar包

你可能感兴趣的:(Ehcache配置时出错- Another unnamed CacheManager already exists in the same VM. Please provide unique name)