2
votes

I'm trying to upgrade a Spring/Hibernate app to use EhCache 2.6.8 (from 2.3.1) as the second-level cache.

I've updated my pom to use the new version, modified ehcache.xml to use some new properties (sizing cache by bytes instead of elements) and it compiles and builds ok. When it tries to access a cache though, I get this 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) 

Looking into the net.sf.ehcache.Cache code, it's this line so looks like the compoundStore is null:

return compoundStore.containsKey(key);

Does this mean EhCache hasn't initialised the caches properly - is there an extra step I need to do for this later version of the lib?

On startup I can see entries suggesting that EhCache is initing caches ok:

Initialized net.sf.ehcache.store.MemoryStore for com.x.domain.AssetHibernate

As per http://ehcache.org/documentation/user-guide/hibernate#build-with-maven, my POM dependencies are:

<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>

(I've added the exclude for ehcache to hibernate-ehcache as it has a dependency on ehcache-1.2.3 which conflicts with the ehcache-core-2.6.8.jar - I'm not quite sure why there's a dep on 1.2.3 as it will surely conflict with any version of ehcache-core you use?)

Here's the Spring config for Hibernate, unchanged from when it worked with 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>

Here's the lightweight ehcache.xml I'm using just while I get it up and running

<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>

Anyone got any ideas? It seems similar to EHCache error on looking up by cache key but that's from a version of EhCache that's even older than the one we're currently using.

Cheers.

1
Have you been able to resolve it? I have run into the same issue - zibi
Afraid not; we ended up making architectural changes (for unrelated reasons) that meant we no longer used ehcache. Sorry, but good luck! - spacebot

1 Answers

0
votes

I got the same NullPointerException, but it happened on application shutdown. I needed to fix the shutdown order to fix the NPE.
Also see : http://camel.465427.n5.nabble.com/Camel-spring-boot-destroys-beans-with-wrong-order-td5799304.html#a5799322