gpt4 book ai didi

java - Spring 连接的 EhCache 磁盘存储在多线程应用程序中创建冲突

转载 作者:行者123 更新时间:2023-12-01 15:27:34 26 4
gpt4 key购买 nike

直觉:看起来有点像两个线程都尝试初始化基于磁盘的溢出缓存,但其中一个失败了。

此应用程序中有两个线程。在以下日志摘录中,它们由 ID 0000004a0000003d 表示。它们同时开始:

[3/04/12 10:17:50:832 EST] 0000004a EhCacheManage I org.springframework.cache.ehcache.EhCacheManagerFactoryBean afterPropertiesSet Initializing EHCache CacheManager
[3/04/12 10:17:50:832 EST] 0000003d EhCacheManage I org.springframework.cache.ehcache.EhCacheManagerFactoryBean afterPropertiesSet Initializing EHCache CacheManager

EhCache 通过 Spring 连接:

<ehcache:annotation-driven cache-manager="ehCacheManager"/>
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>

并如此配置:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
<diskStore path="java.io.tmpdir/GMM_DAO_Caches"/>

<defaultCache eternal="false" maxElementsInMemory="1000"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU"/>

<cache name="dao_results" eternal="false" maxElementsInMemory="1000"
overflowToDisk="true" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="3600"
memoryStoreEvictionPolicy="LRU"/>

<cache name="dao_results_shortlived" eternal="false" maxElementsInMemory="1000"
overflowToDisk="true" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LRU"/>

</ehcache>

错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.googlecode.ehcache.annotations.config.internalEhCach
eCachingAdvisor': Cannot resolve reference to bean 'com.googlecode.ehcache.annotations.impl.CacheStaticMethodMatcherPointcut#0' while setting bean property
'pointcut'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.googlecode.ehcache.annotations
.impl.CacheStaticMethodMatcherPointcut#0': Cannot resolve reference to bean 'com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl#0' while sett
ing bean property 'cacheAttributeSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.g
ooglecode.ehcache.annotations.impl.CacheAttributeSourceImpl#0': Cannot resolve reference to bean 'ehCacheManager' while setting bean property 'cacheManager
'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehCacheManager' defined in class path resour
ce [ApplicationContext-DAOs.xml]: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: Could not create cache directory "C:
\DOCUME~1\JMAWSO~1.NT3\LOCALS~1\Temp\GMM_DAO_Caches\ehcache_auto_created_1333412270879".
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.aop.framework.autoproxy.BeanFactoryAdvisorRetrievalHelper.findAdvisorBeans(BeanFactoryAdvisorRetrievalHelper.java:86)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors(AbstractAdvisorAutoProxyCreator.java:100)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:86)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:68)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:359)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanF
actory.java:407)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1426)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
... 35 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.googlecode.ehcache.annotations.impl.CacheStaticMetho
dMatcherPointcut#0': Cannot resolve reference to bean 'com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl#0' while setting bean property 'cac
heAttributeSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.googlecode.ehcache.anno
tations.impl.CacheAttributeSourceImpl#0': Cannot resolve reference to bean 'ehCacheManager' while setting bean property 'cacheManager'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehCacheManager' defined in class path resource [ApplicationContext
-DAOs.xml]: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: Could not create cache directory "C:\DOCUME~1\JMAWSO~1.NT3
\LOCALS~1\Temp\GMM_DAO_Caches\ehcache_auto_created_1333412270879".
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
... 53 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.googlecode.ehcache.annotations.impl.CacheAttributeSo
urceImpl#0': Cannot resolve reference to bean 'ehCacheManager' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.fa
ctory.BeanCreationException: Error creating bean with name 'ehCacheManager' defined in class path resource [ApplicationContext-DAOs.xml]: Invocation of ini
t method failed; nested exception is net.sf.ehcache.CacheException: Could not create cache directory "C:\DOCUME~1\JMAWSO~1.NT3\LOCALS~1\Temp\GMM_DAO_Caches
\ehcache_auto_created_1333412270879".
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
... 63 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehCacheManager' defined in class path resource [Applicat
ionContext-DAOs.xml]: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: Could not create cache directory "C:\DOCUME~1\JM
AWSO~1.NT3\LOCALS~1\Temp\GMM_DAO_Caches\ehcache_auto_created_1333412270879".
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
... 73 more
Caused by: net.sf.ehcache.CacheException: Could not create cache directory "C:\DOCUME~1\JMAWSO~1.NT3\LOCALS~1\Temp\GMM_DAO_Caches\ehcache_auto_created_1333
412270879".
at net.sf.ehcache.store.compound.factories.DiskOverflowStorageFactory.getDataFile(DiskOverflowStorageFactory.java:89)
at net.sf.ehcache.store.compound.factories.DiskOverflowStorageFactory.<init>(DiskOverflowStorageFactory.java:71)
at net.sf.ehcache.store.compound.impl.OverflowToDiskStore.create(OverflowToDiskStore.java:63)
at net.sf.ehcache.Cache.initialise(Cache.java:969)
at net.sf.ehcache.CacheManager.addCacheNoCheck(CacheManager.java:967)
at net.sf.ehcache.CacheManager.addConfiguredCaches(CacheManager.java:608)
at net.sf.ehcache.CacheManager.init(CacheManager.java:339)
at net.sf.ehcache.CacheManager.<init>(CacheManager.java:280)
at org.springframework.cache.ehcache.EhCacheManagerFactoryBean.afterPropertiesSet(EhCacheManagerFactoryBean.java:115)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
... 80 more

我怀疑两个线程都试图写入同一个文件。时间戳并不是创建唯一文件名的好方法。此错误在线程数较多的应用中更频繁地发生。

如何避免这些冲突?最好,我想将线程 ID 或其他一些唯一标识符嵌入到文件名中。

最佳答案

我本以为你的设置只需查看配置就可以工作。

一种选择是定义两个不同的缓存管理器,每个缓存管理器都有自己的磁盘存储。

或者,您可以使用 CacheManager 工厂方法来创建单例 CacheManager,从而消除因两个缓存管理器访问同一存储而导致的争用。然而 Ehcache 应该处理这种情况并给你一个警告;可能是版本特定的问题。

最后,检查这是否是Windows权限问题。尝试其他位置。例如:

<diskStore path="ehcache.disk.store.dir2" />并将 -D 变量设置为您知道可写的位置。例如:

-Dehcache.disk.store.dir2=C:/temp/ehcache

(假设C:/temp存在)

关于java - Spring 连接的 EhCache 磁盘存储在多线程应用程序中创建冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9985676/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com