Ehcache second level cache not working with JPA and Hibernate?
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
Comments
-
user1071914 almost 2 years
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 over 10 yearsJust 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.
-
Scarlett almost 10 yearsbut when specify the <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode> under <persistence-unit>, there will be error in the persistence.xml. cvc-complex-type.2.4.a: Invalid content was found starting with element 'shared-cache-mode'. "java.sun.com/xml/ns/persistence":non-jta-data-source, "java.sun.com/xml/ns/persistence":mapping-file, "http:// java.sun.com/xml/ns/persistence":jar-file, "java.sun.com/xml/ns/persistence":class, "java.sun.com/xml/ns persistence":exclude-unlisted-classes, "java.sun.com/xml/ns/persistence":properties}' is expected.
-
mowwwalker about 7 yearsThanks 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..