4
votes

EDIT: I should state that I've been researching this for a couple of days now - there is plenty of information on "how to configure ehcache with Hibernate" but they mostly do not refer to using JPA and annotations - they either refer to pure Hibernate or to configuration through XML. (Just want to make it clear that I have already been around the internet on this problem.) I'm using JPA and annotations, so most of these configuration guides refer to files I don't have in my app (like hbm.xml files).

I have an app that is using Hibernate 3.6.10.FINAL, Spring Data 1.3.2.RELEASE, and Spring version 3.2.1.RELEASE. I'm trying to get the second-level caching working in hibernate. According to the documentation, I can do that simply by including the following dependency and configuration:

(POM.XML)

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

(PERSISTENCE.XML)

        <property name="hibernate.cache.provider_class"  value="org.hibernate.cache.EhCacheProvider"  />
        <property name="hibernate.cache.use_second_level_cache" value="true" />
        <property name="hibernate.cache.use_query_cache" value="true" />
        <property name="hibernate.generate_statistics" value="true" />

I've annotated one of my entity classes using the javax.persistence.Cacheable annotation and tried to view the statistics in a JUnit test:

public void cacheTest() {
    RandomDataGenerator randomData = new RandomDataGenerator();
    for (int i = 0; i < 10; i++) {
        AppMaster master = masterService.findOne(randomData.nextLong(1, 10));
        logger.debug(String.format("Read one record back = %1$d, %2$s", master.getApplicationId(), master.getApContact()),AppMasterServiceTest.class);
        //  Get statistics from hibernate session
        Session session = (Session)masterService.getEntityManager().getDelegate();
        Statistics statistics = session.getSessionFactory().getStatistics();
        logger.debug(String.format("Second level stats = %1$d, %2$d, %3$d", 
                statistics.getSecondLevelCachePutCount(), 
                statistics.getSecondLevelCacheHitCount(),
                statistics.getSecondLevelCacheMissCount()), AppMasterServiceTest.class);
    }

But the statistics appear to always be zero.

2013-11-11 11:20:33,908 DEBUG [main] test.service.AppMasterServiceTest  - Second level stats = 0, 0, 0

Can anyone help me diagnose this?

1
From what you've posted it looks like you haven't set ENABLE_SELECTIVE in your persistence.xml. Check out the docs on docs.oracle.com/javaee/6/api/javax/persistence/Cacheable.html and take a look at this related question stackoverflow.com/questions/3663979/…Planky
You seem to be correct, adding a tag to specify ENABLE_SELECTIVE explicitly in the persistence.xml seems to have done the trick. I've read that ENABLE_SELECTIVE should be the default (docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/…), however I've also read that it seems to not work that way. If you want to post your comment as an answer I'll be happy to mark it correct! Thanks.user1071914

1 Answers

3
votes

javax.persistence.Cachable requires you set ENABLE_SELECTIVE in your persistence.xml. You should include a line like the following:

<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>

Check out the docs on http://docs.oracle.com/javaee/6/api/javax/persistence/Cacheable.html

Also, take a look at this related question: How to use JPA2's @Cacheable instead of Hibernate's @Cache