4

I've got this spring/hibernate project which I'm trying to add 2nd level cache to hibernate via ehcache and terracotta. Everything seems to plug fine, I can even see in the terracota console the entries for the entities I'm trying to cache. But based on the statistics and log from the DB there's nothing cached at all!

The load hit ratio is 0%, the load statistics is 0 as well. What is that that I'm doing wrong?

Here is what I did, I added the required jars via maven.

        <dependency>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache-core</artifactId>
                <version>2.5.2</version>
        </dependency>
        <dependency>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache-terracotta</artifactId>
                <version>2.5.2</version>
        </dependency>
        <dependency>
                <groupId>org.terracotta</groupId>
                <artifactId>terracotta-toolkit-1.5-runtime</artifactId>
                <version>4.2.0</version>
        </dependency>

Changed my hibernate properties to enable 2nd level cache

<property name="hibernateProperties">
            <props>
                ...
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
                <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.use_structured_entries">true</prop>
                <prop key="hibernate.cache.generate_statistics">true</prop>
            </props>
        </property>

Added the @Cache annotation to my test entity

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User implements java.io.Serializable
 {
...
}

Here is my extremely simple ehcache.xml (I also tried setting a cache entry for my entity with the same result)

<?xml version="1.0" encoding="UTF-8"?>
<ehcache >    
    <defaultCache  
        maxElementsInMemory="10000"   
        eternal="false" 
        maxEntriesLocalHeap="10"         
        timeToIdleSeconds="120"         
        timeToLiveSeconds="120">        
        <terracotta/>    
    </defaultCache>        
    <terracottaConfig         
        url="localhost:9510"/>
</ehcache>

And after I start my terracotta server and run my test code

@Test
    @Transactional
    @Rollback(false)
    public void testCache() {
        long start = System.currentTimeMillis();
        List<User> list = userRepository.listAll(0, 100);
        long end = System.currentTimeMillis();
        log.info("Total time "+(end-start));
        assertNotNull(list);
        assertThat(list.size(), is(100));

        for (int i=0; i<100; i++) {
            long start2 = System.currentTimeMillis();
            list = userRepository.listAll(0, 100);
            long end2 = System.currentTimeMillis();
            log.info("Total time 2 "+(end2-start2));
        }
        assertNotNull(list);
        assertThat(list.size(), is(100));
    }

My log shows 100 SQL which shouldn't occurred. It also shows hit ratio as 0%.

Here are some screen shots from the terracotta console while my test is running.

What is that last piece that I need in order to have this working?

2nd level cache statistics ehcache overview Entity statistics

4
  • The @Cache annotation you have used is the one provided in Spring Framework or something else? May 16, 2012 at 5:05
  • I see you have the Hibernate statistics enabled... I think the second screenshot does actually gets the stats from there, but can you verify in code ? If these values are correct, for some reason Hibernate isn't using the 2nd level cache at all... Can you load entities by id ? Actually access stuff in the list ?
    – Alex Snaps
    May 16, 2012 at 11:27
  • Also, since the test is actually trying to measure latency, you might just as well turn off hibernate.cache.use_structured_entries. Except if you really planned on inspect the stuff in the cache...
    – Alex Snaps
    May 16, 2012 at 11:29
  • Thanks guys, actually I've just found the reason to the problems. I'm posting it here for the record. @AlexSnaps thanks for the tip about hibernate.cache.use_structured_entries I'll turn it off. And check the minor change on the config that was causing the stats not to be shown.
    – maverick
    May 16, 2012 at 12:58

1 Answer 1

3

Found the solution to the problems I was experimenting, here are the details:

  • The statistics weren't getting set due to a wrong key in the hibernate properties

Use this (note no .cache. )

<prop key="hibernate.generate_statistics">true</prop>

instead of

<prop key="hibernate.cache.generate_statistics">true</prop>
  • The queries wasn't getting cached since I was not using ".setCachable(true)" for the method that was in charge of listing/loading the entities.

Your Answer

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

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