houbie
houbie

Reputation: 91

grails 3 (spring-boot) - how to configure hibernate second level cache

When trying to use ehcache as hibernate second level cache, I get following exception: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given

I tried to configure it in application.yml:

spring:
  jpa:
    properties:
      hibernate:
        cache:
          region:
            factory_class: org.hibernate.cache.ehcache.EhCacheRegionFactory

and:

grails:
    hibernate:
      cache:
        use_second_level_cache: true
        provider_class: net.sf.ehcache.hibernate.EhCacheProvider
        region:
          factory_class: org.hibernate.cache.ehcache.EhCacheRegionFactory

but none of these seem to help.

Upvotes: 2

Views: 3628

Answers (3)

Martin
Martin

Reputation: 630

It's a late answer but may help someone in the future.

Try adding this dependency in your pom.xml:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
    <version>5.2.2.Final</version>
</dependency>

and then in application.yml:

spring:
    jpa:
        properties:
            hibernate.cache.use_second_level_cache: true
            hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.EhCacheRegionFactory

Upvotes: 0

Wiggitamoo
Wiggitamoo

Reputation: 303

You should remove the grails before hibernate. Grails is expecting the structure of region to be under the cache.

Remember tabs are important in yml files

So the code should look something like this:

hibernate:
cache:
    queries: false
    use_second_level_cache: true
    use_query_cache: false
    region:
        factory_class: 'org.hibernate.cache.ehcache.EhCacheRegionFactory'

Upvotes: 0

Graeme Rocher
Graeme Rocher

Reputation: 7985

The latter should work if you remove the grails top level config since Grails looks for 'hibernate.blah' not 'grails.hibernate.blah':

  hibernate:
      cache:
        use_second_level_cache: true
        provider_class: net.sf.ehcache.hibernate.EhCacheProvider
      region:
         factory_class: org.hibernate.cache.ehcache.EhCacheRegionFactory

Upvotes: 1

Related Questions