I'm using Spring Boot 1.2.5 with JPA2 to annotate entities (and hibernate as underlaying JPA implementation).
I wanted to use second level cache in that setup, so entities were annotated with @javax.persistence.Cacheable
I also added following in application.properties:
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
During bootup hibernate complained about lack of EhCacheRegionFactory
so I also added this to pom:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
</dependency>
But still queries like entityManager.find(Clazz.class, pk)
are firing DB query instead of using cached data.
Any idea what is missing?
Well after some more digging here's what I was missing in
application.properties
:Hope it helps someone :)
Did you add
on the class you want to cache?
@Daimon I am not really sure, whether
is the best decision.
Quoted from Hibernate 20.2.1. Cache mappings documentation section
whereas
So, could it be, that you have not annotated all affected entities with @javax.persistence.Cacheable or rather @org.hibernate.annotations.Cache ? This could lead to the affect, that the Query Cache tried to look up the affected entities in the Second Level Cache without success and then started to fetch each entity by a single select.
To sum everything (L2 cache and query cache) up:
The first thing to do is to add cache provider (I recommend using EhCache) to your classpath.
In previous version of Hibernate (prior to 5.3) it is done by adding hibernate-ehcache dependency. This library contains EhCache 2 which is now discontinued.
In newer versions of Hibernate caches implementing JSR-107 (JCache) API should be used. So there're 2 dependencies needed - one for JSR-107 API and the second one for the actual JCache implementation (EhCache 3).
Now let's move on to application.properties/yml file:
For EhCache 3 this region factory should be used:
You can also enable TRACE level logging for Hibernate to verify your code and configuration:
Now let's move on to the code. To enable L2 caching on your entity you need to add those two annotations:
Note - if you want to cache your
@OneToMany
or@ManyToOne
relation - add@Cache
annotation over this field as well.And to enable query cache in your spring-data-jpa repository you need to add proper
QueryHint
.Now verify via logs if your query is executed only once and remember to turn off all the debug stuff - now you're done.
Note 2 - you can also define missing cache strategy as
create
if you want to stay with defaults without getting warnings in your logs:You should have an ehcache.xml file in your classpath. The file should contains at least the default cache strategy. For easier debuging, make it eternal to be sure entities are not evicted from cache :
ehcache.xml:
To ensure that all is ok, you should have the following log during your application startup :
That means that your entity cache annotation have been correctly readed and default cache will be used.
If you test with
entityManager.find(Clazz.class, pk)
that's not envolve the query cache, but just the entity cache. Query cache is used for queries (em.createQuery(...) and for relations shipAlso, I use org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory, but I don't know wich is better.
You can use third party cache provider, among JCache, Ehcache, Gvava Cache, Hazelcast Cache, Caffeine Cache.
Please refer this answer on Quora to know how to enable and configure the second level cache in Spring boot.