4

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?

user1071914
  • 3,295
  • 11
  • 50
  • 76
  • From what you've posted it looks like you haven't set ENABLE_SELECTIVE in your persistence.xml. Check out the docs on http://docs.oracle.com/javaee/6/api/javax/persistence/Cacheable.html and take a look at this related question http://stackoverflow.com/questions/3663979/how-to-use-jpa2s-cacheable-instead-of-hibernates-cache – Planky Nov 11 '13 at 21:24
  • 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 (http://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/configuration.html#setup-configuration), 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 Nov 12 '13 at 13:28

1 Answers1

3

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

Community
  • 1
  • 1
Planky
  • 3,185
  • 3
  • 29
  • 39
  • Just to amplify, ENABLE_SELECTIVE should be the default (you should not need to specify it explicitly), but for some reason you must put this line in your persistence.xml or you get nothing. – user1071914 Nov 21 '13 at 20:56
  • but when specify the ENABLE_SELECTIVE under , there will be error in the persistence.xml. cvc-complex-type.2.4.a: Invalid content was found starting with element 'shared-cache-mode'. "http://java.sun.com/xml/ns/persistence":non-jta-data-source, "http://java.sun.com/xml/ns/persistence":mapping-file, "http:// java.sun.com/xml/ns/persistence":jar-file, "http://java.sun.com/xml/ns/persistence":class, "http://java.sun.com/xml/ns/ persistence":exclude-unlisted-classes, "http://java.sun.com/xml/ns/persistence":properties}' is expected. – Scarlett May 21 '14 at 10:07
  • Thanks for this answer! That is pretty strange that, like @user1071914 says, the property needs to be set even though hibernate set that that's the default value.. – mowwwalker Mar 30 '17 at 01:36