我试图升级一个Spring/Hibernate应用程序,使用EhCache 2.6.8 (从2.3.1)作为二级缓存。
我已经更新了我的pom以使用新版本,修改了ehcache.xml以使用一些新的属性(按字节来调整缓存大小,而不是元素),并且它编译和构建ok。但是,当它试图访问缓存时,我得到了这个NPE:
java.lang.NullPointerException at net.sf.ehcache.Cache.isKeyInCache(Cache.java:3034)
at net.sf.ehcache.hibernate.regions.EhcacheDataRegion.contains(EhcacheDataRegion.java:197)
at net.sf.ehcache.hibernate.strategy.ReadOnlyEhcacheEntityRegionAccessStrategy.putFromLoad(ReadOnlyEhcacheEntityRegionAccessStrategy.java:60) 查看net.sf.ehcache.Cache代码,它是这一行,所以看起来compoundStore是空的:
return compoundStore.containsKey(key);这是否意味着EhCache没有正确地初始化缓存--对于这个较晚版本的库,我还需要执行额外的步骤吗?
在启动时,我可以看到显示EhCache正在进入缓存的条目,ok:
Initialized net.sf.ehcache.store.MemoryStore for com.x.domain.AssetHibernate根据http://ehcache.org/documentation/user-guide/hibernate#build-with-maven,我的POM依赖项是:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.8</version>
<scope>compile</scope>
</dependency>
<!-- New for ehcache 2.6.8 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>3.3.1.GA</version>
<exclusions>
<exclusion>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</exclusion>
</exclusions>
</dependency>(我已经添加了将ehcache排除到hibernate-ehcache,因为它依赖ehcache-1.2.3,这与ehcache-core-2.6.8.jar冲突-我不太确定为什么1.2.3上有一个dep,因为它肯定会与您使用的任何版本的ehcache-core冲突?)
以下是Hibernate的Spring配置,与其使用ehcache 2.3.1时相同:
<bean id="coreSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="coreDataSource">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
</props>
</property>
<property name="packagesToScan" value="com.x.domain" />
</bean>
<bean id="coreTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="coreSessionFactory">
<qualifier value="core" />
</bean>这是我正在使用的轻量级ehcache.xml,就在我启动并运行它的时候
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"
monitoring="autodetect" dynamicConfig="true"
maxBytesLocalHeap="512m"
>
<diskStore path="java.io.tmpdir" />
<defaultCache
timeToIdleSeconds="180"
timeToLiveSeconds="300"
overflowToDisk="true"
memoryStoreEvictionPolicy="LFU"
/>
</ehcache>有人有什么想法吗?它看起来类似于EHCache error on looking up by cache key,但它来自EhCache的一个版本,它甚至比我们目前使用的版本还要老。
干杯。
发布于 2017-07-13 18:46:25
我得到了相同的NullPointerException,但它发生在应用程序关闭时。我需要修复关闭命令来修复NPE。
另见:http://camel.465427.n5.nabble.com/Camel-spring-boot-destroys-beans-with-wrong-order-td5799304.html#a5799322
https://stackoverflow.com/questions/23652548
复制相似问题