EHCache error on looking up by cache key
I am using EHCache 1.5.0 on a webapp running on a WebLogic 9.1 instance. Once in a while I run into the following error while getting an element from cache or while checking if an item exists in cache. Has anyone else seen this issue? Any suggestions on how to fix this would be great.
Code that causes this issue:
getMyCache().isKeyInCache(cacheKey)
ehcache configuration:
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="true"
I am using Spring to get an instance of CacheManager and here is my bean definition:
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation"><value>classpath:ehcache.xml</value></property>
</bean>
The error is as follows:
java.lang.NullPointerException
at net.sf.ehcache.Cache.isElementInMemory(Cache.java:1962)
at net.sf.ehcache.Cache.isKeyInCache(Cache.java:2075)
at com.test.services.impl.ContentServicesImpl.getContentItemFromCache(ContentServicesImpl.java:260) ......
There is nothing else in the log indicating the cause for the NullPointerException on looking up a key in cache.
Any point开发者_如何学编程ers, suggestions on how to fix this will be greatly appreciated. This does not happen consistently, seems to happen randomly in one environment.
Given the stack trace, it seems that the calling thread is seeing a Cache with a null MemoryStore (which shouldn't be possible). I have a sneaking suspicion that this might be a memory visibility issue in either the CacheManager creation code (double-checked locking) or the Cache itself. We've tightened up a lot of those field visibility issues for Ehcache 1.7.1 (not out yet but in a few weeks).
That makes it hard to give a definite fix but one crappy idea would be to add some spring initialization that guaranteed early during startup that only one thread constructed the CacheManager. If that made the problem go away, it would lend some credence to the above theory.
精彩评论