Skip to content

Add support for configuring Hibernate L2 Cache #14586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
marceloverdijk opened this issue Sep 23, 2018 · 4 comments
Open

Add support for configuring Hibernate L2 Cache #14586

marceloverdijk opened this issue Sep 23, 2018 · 4 comments
Labels
type: enhancement A general enhancement

Comments

@marceloverdijk
Copy link

As discussed with @snicoll on Gitter, it would be nice if Spring Boot could offer an easy way to use the JCache CacheManager created by Spring Boot with the Hibernate second-level cache.

Hibernate supports different L2 caching options which can be set by e.g. using specific properties like:

spring.jpa.hibernate.cache.region.factory_class=jcache
spring.jpa.hibernate.javax.cache.provider=org.ehcache.jsr107.EhcacheCachingProvider
spring.jpa.hibernate.javax.cache.uri=classpath:ehcache3.xml

However Hibernate also supports explicitly setting the cache manager which can be done via a custom HibernatePropertiesCustomizer like:

@Configuration
public class HibernateConfig {

    @Autowired
    private CacheManager cacheManager;

    @Bean
    public HibernatePropertiesCustomizer hibernatePropertiesCustomizer() {
        return hibernateProperties -> hibernateProperties.put(ConfigSettings.CACHE_MANAGER, cacheManager);
    }
}

It would be nice if Spring Boot could offer a specific property like spring.jpa.hibernate.use-explicit-cache-manager, which when set to true auto configures a HibernatePropertiesCustomizer which sets the cache manager automatically?

PS: This also relates to PR #14570 adding documentation how set up Hibernate second-level caching currently.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 23, 2018
@snicoll
Copy link
Member

snicoll commented Oct 3, 2018

I am not keen to add a specific property. IMO, this should happen out-of-the-box based on the context (single JCacheCacheManager available and JCache integration library on the classpath).

Is there any reason you want this to be a property?

I'd like to understand the effect of setting it unconditionally but haven't found it referenced in the doc so I've raised HHH-13009

@snicoll snicoll added the status: on-hold We can't start working on this issue yet label Oct 3, 2018
@marceloverdijk
Copy link
Author

Yes, out-of-the-box based on the context is also fine. No specific reason for an explicit property.

@snicoll snicoll changed the title Introduce new Boot application property to auto configure explicit cache manager for Hibernate L2 caching Add support for configuring Hibernate L2 Cache Oct 3, 2018
@snicoll
Copy link
Member

snicoll commented Oct 9, 2018

I've documented things in #14734 and it looks like auto-adding the customizer in the case of JCache should be enough. There is also integration for EhCache 2 and Infinispan so it would be nice to reasearch how we can support that.

The support can be easily disabled using spring.jpa.properties.hibernate.cache.use_second_level_cache (that is true by default).

@snicoll snicoll added type: enhancement A general enhancement and removed status: on-hold We can't start working on this issue yet status: waiting-for-triage An issue we've not yet triaged labels Oct 9, 2018
@snicoll snicoll added this to the General Backlog milestone Oct 9, 2018
@andvasp
Copy link

andvasp commented Apr 20, 2020

Would be good spring do it automatically. Otherwise we have to do as @marceloverdijk mentioned above to use the same config file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants