Ehcache second level cache not working with JPA and Hibernate?

user1071914

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?

Planky

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Hibernate 4 second level cache hibernate is not working with association (EhCache)

From Dev

Collections not read from hibernate/ehcache second-level-cache

From Dev

Hibernate + ehcache second level cache miss in simple example

From Dev

Unable to configure second level cache of hibernate-ehcache-4.3.5.Final.jar in Hibernate 5.1.0.Final

From Dev

Spring Boot + JPA2 + Hibernate - enable second level cache

From Dev

Second Level Cache never hits using, spring3, hibernate4, ehcache?

From Dev

Hibernate second level cache example

From Dev

Hibernate : Invalidating Second Level cache

From Dev

infinispan as second level cache hibernate

From Dev

Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given JPA+hibernate+Spring

From Dev

EhCache Hibernate 2nd level cache maxBytesLocalHeap slow

From Dev

Hazelcast Hibernate second-level cache configuration

From Dev

How to enable second level cache in Hibernate

From Dev

dirty reads of associations in hibernate second level cache

From Dev

How hibernate second level cache works internally?

From Dev

Entity update using hibernate second level cache

From Dev

Configuring Infinispan as remote second level cache for Hibernate

From Dev

hibernate second level cache with Redis -will it improve performance?

From Dev

How hibernate second level cache works internally?

From Dev

Is second level cache is Session specific in Hibernate

From Dev

When to use second level cache in hibernate

From Dev

How to enable second level cache in Hibernate 5.2.2?

From Dev

Second level cache not working With Spring and JSF

From Dev

Second level cache not working With Spring and JSF

From Dev

With Sping's cache support (for ehcache), do we still need Hibernate 2nd level cache?

From Dev

With Sping's cache support (for ehcache), do we still need Hibernate 2nd level cache?

From Dev

How to provide second level cache with redis in spring data jpa?

From Dev

How hibernate ensures second level cache is updated with latest data in database

From Dev

Hibernate: Is it possible to save a transient field in second level cache?

Related Related

  1. 1

    Hibernate 4 second level cache hibernate is not working with association (EhCache)

  2. 2

    Collections not read from hibernate/ehcache second-level-cache

  3. 3

    Hibernate + ehcache second level cache miss in simple example

  4. 4

    Unable to configure second level cache of hibernate-ehcache-4.3.5.Final.jar in Hibernate 5.1.0.Final

  5. 5

    Spring Boot + JPA2 + Hibernate - enable second level cache

  6. 6

    Second Level Cache never hits using, spring3, hibernate4, ehcache?

  7. 7

    Hibernate second level cache example

  8. 8

    Hibernate : Invalidating Second Level cache

  9. 9

    infinispan as second level cache hibernate

  10. 10

    Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given JPA+hibernate+Spring

  11. 11

    EhCache Hibernate 2nd level cache maxBytesLocalHeap slow

  12. 12

    Hazelcast Hibernate second-level cache configuration

  13. 13

    How to enable second level cache in Hibernate

  14. 14

    dirty reads of associations in hibernate second level cache

  15. 15

    How hibernate second level cache works internally?

  16. 16

    Entity update using hibernate second level cache

  17. 17

    Configuring Infinispan as remote second level cache for Hibernate

  18. 18

    hibernate second level cache with Redis -will it improve performance?

  19. 19

    How hibernate second level cache works internally?

  20. 20

    Is second level cache is Session specific in Hibernate

  21. 21

    When to use second level cache in hibernate

  22. 22

    How to enable second level cache in Hibernate 5.2.2?

  23. 23

    Second level cache not working With Spring and JSF

  24. 24

    Second level cache not working With Spring and JSF

  25. 25

    With Sping's cache support (for ehcache), do we still need Hibernate 2nd level cache?

  26. 26

    With Sping's cache support (for ehcache), do we still need Hibernate 2nd level cache?

  27. 27

    How to provide second level cache with redis in spring data jpa?

  28. 28

    How hibernate ensures second level cache is updated with latest data in database

  29. 29

    Hibernate: Is it possible to save a transient field in second level cache?

HotTag

Archive