I'm trying to setup Hibernate with EhCache as the second level cache but the TTL is not working.
Here are my dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jcache</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
Here's my YAML configuration:
spring:
jpa:
show-sql: true
properties:
hibernate:
dialect: Dialect
cache:
use_second_level_cache: true
region.factory_class: org.hibernate.cache.jcache.JCacheRegionFactory
use_query_cache: true
cache:
jcache:
config: classpath:ehcache.xml
Here's how my Entity class is configured:
@Entity
@javax.persistence.Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class PersonEntity {
//
}
And the JpaRepository for the entity:
public interface PersonRepository extends JpaRepository<PersonEntity, Integer> {
@org.springframework.data.jpa.repository.QueryHints({
@javax.persistence.QueryHint(name = "org.hibernate.cacheable", value = "true")
})
List<PersonEntity> findByName(String name);
}
I've configured the cache to expire in 2 seconds, but calling findByName
still uses the cache (there are no SQL Queries printed after the first one).
Here's the ehcache.xml
file:
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://www.ehcache.org/v3">
<cache-template name="simple">
<expiry>
<ttl>2</ttl>
</expiry>
<heap>100</heap>
</cache-template>
<cache alias="com.sample.PersonEntity" uses-template="simple"/>
</config>
EDIT:
I've done some debugging. I've added a break point in org.ehcache.jsr107.ExpiryPolicyToEhcacheExpiry
:
javax.cache.expiry.Duration duration = this.expiryPolicy.getExpiryForCreation();
This duration is INFINITE for some reason. So maybe the configuration is not set properly? I know the xml is being read because when I invalidate it (by removing heap tag for example) I get an error.
ehcache.xml
configuration file with a TTL of 2 seconds, but only the first method call is printing the query, meaning it's using a cache, but it's not expiring. – Agreementuse_query_cache
to false it will read everytime. If I remove@QueryHints
it will read every time. – AgreementGET localhost:8080/users?name=smith
. TTL for users is 1 minute. Hope it helps... – Northnortheastcache.jcache.config=classpath:ehcache.xml
about. Plz take a moment to reply. I want to know the difference between above andhibernate.javax.cache.uri=classpath:ehcache.xml
– Coenurus