2012-04-03 53 views
0

直覺:它看起來有點像兩個線程都嘗試初始化基於磁盤的溢出緩存,並且其中一個失敗。彈簧連接的EhCache磁盤存儲在多線程應用程序中創建衝突

在這個應用程序中有兩個線程。在以下日誌摘錄中,它們由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通過彈簧接線:

<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 

我懷疑兩個線程都試圖寫入sa我的文件。時間戳不是製作獨特文件名的好方法。在具有更多線程的應用程序中,此錯誤發生得更頻繁。

如何避免這些碰撞?最好是,我想將線程ID或其他唯一標識符嵌入到文件名中。

回答

1

我以爲你的設置可能只是看着配置工作。

一種選擇是定義兩個不同的緩存管理器,每個緩存管理器都有自己的磁盤存儲。

或者,您可以使用CacheManager工廠方法創建單例CacheManager,從而消除由於兩個緩存管理器訪問同一個存儲而導致的爭用。 Ehcache應該處理這個案件,並給你一個警告;可能是特定於版本的問題。

最後,檢查這是否是Windows權限問題。嘗試其他位置。例如:

<diskStore path="ehcache.disk.store.dir2" />並將一個-D變量設置爲您知道可寫入的位置。例如:

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

(假定C:/溫度存在)

+0

Righteo。我調用了[Spring的EhCacheManagerFactoryBean上的setShared](http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/cache/ehcache/EhCacheManagerFactoryBean.html#setShared(boolean))。這應該夠了吧。 – Synesso 2012-04-30 01:24:58

相關問題