2

I have some problems getting the hibernate second level cache to work for caching domain objects. According to the ehcache documentation it shouldn't be too complicated to add caching to my existing working application.

I have the following setup (only relevant snippets are outlined):

@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE
public void Entity {
    // ... 
}

ehcache-entity.xml

<cache name="com.company.Entity" eternal="false"
    maxElementsInMemory="10000" overflowToDisk="true" diskPersistent="false"
    timeToIdleSeconds="0" timeToLiveSeconds="300"
    memoryStoreEvictionPolicy="LRU" />

ApplicationContext.xml

<bean class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="ds" />
    <property name="annotatedClasses">
        <list>
            <value>com.company.Entity</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="net.sf.ehcache.configurationResourceName">/ehcache-entity.xml</prop>
            <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</prop>
            .... 
    </property>
</bean>

Maven dependencies

   <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.4.0.GA</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-hibernate3</artifactId>
        <version>2.0.8</version>
        <exclusions>
            <exclusion>
                <artifactId>hibernate</artifactId>
                <groupId>org.hibernate</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.3.2</version>
    </dependency>

A test class is used which enables cache statistics:

    Cache cache = cacheManager.getCache("com.company.Entity");
    cache.setStatisticsAccuracy(Statistics.STATISTICS_ACCURACY_GUARANTEED);
    cache.setStatisticsEnabled(true);
    // store, read etc ... 
    cache.getStatistics().getMemoryStoreObjectCount(); // returns 0

No operation seems to trigger any cache changes. What am I missing? Currently I'm using HibernateTemplate in the DAO, perhaps that has some impact.

[EDIT]

The only ehcache log output when set to DEBUG is:

SettingsFactory: Cache region factory : net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
2
  • Does the log tell you anything when it load the ecache xml?
    – heldt
    Mar 19, 2011 at 20:40
  • @heldt, I've updated my post to contain ehcache warnings Mar 19, 2011 at 20:44

5 Answers 5

2

Do you need to manually tell Hibernate to use the EHCache provider? I've never really been sure if this is required, but Hibernate does support a number of cache providers so I suspect that it might be necessary to explicitly tell Hibernate which one you want. Try adding this property to ApplicationContext.xml:

<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
1
  • 2
    Thanks for your reply, according to the docs, since hibernate 3.3 that property shouldn't be necessary. Mar 20, 2011 at 9:59
1

There were several causes that I've identified:

  1. Correct maven dependencies:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.3.Final</version>
    </dependency>
    
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.4.1</version>
    </dependency>
    
  2. Added the @Cacheable annotation from javax.persistence to my entities.

  3. Read logging from hibernate instead of ehcache.

    getSessionFactory().getStatistics().logSummary();

  4. Not all hibernate operations seems to affect the cache. This I need to read up on further.

0

By looking at your config, it all seems fine afaict. Only thing worth noticing, is that when using the HibernateTemplate, you have to explicitly setCacheQueries(true) if you plan on using the query cache... Which I wouldn't recommend, except if you really need it: http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/HibernateTemplate.html#setCacheQueries(boolean)

Did you try the Hibernate statistics instead of the Ehcache ones ? Do you get cache misses there ? (reason I ask is to be sure you do use the same CacheManager as Hibernate does)...

1
  • I'm not planning to use the query cache so it's disabled, but thanks for noticing. I'm also in the process of refactoring away hibernatetemplate, so I'll get back when that's done :) Mar 21, 2011 at 16:20
0

you can reference following configure

<prop key="hibernate.cache.use_query_cache">true</prop>
0

In hibernate.cfg.xml add:

 <hibernate-configuration>
   <session-factory>
     ...
     <property name="cache.use_second_level_cache">true</property>
     <property name="cache.use_query_cache">true</property>
     <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.