Kiran Chitturi
Kiran Chitturi

Reputation: 503

Grails 4.0.1 error enabling hibernate second level cache

I am having trouble enabling second level cache for hibernate using grails 4.0.1 version.

I have the following enabled in application.yaml

hibernate:
    cache:
        queries: false
        use_second_level_cache: true
        use_query_cache: false
        provider_class: net.sf.ehcache.hibernate.EhCacheProvider
        region:
           factory_class: org.hibernate.cache.ehcache.internal.EhCacheRegionFactory

I have the following in build.gradle

compile group: 'net.sf.ehcache', name: 'ehcache-core', version: '2.6.11'
compile "org.hibernate:hibernate-ehcache:5.4.0.Final"

Getting below error :

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.cache.spi.RegionFactory]
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:184)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:300)
        ... 36 common frames omitted
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.cache.spi.RegionFactory]
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:275)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
        at org.hibernate.boot.internal.MetadataBuilderImpl$MetadataBuildingOptionsImpl.<init>(MetadataBuilderImpl.java:688)
        at org.hibernate.boot.internal.MetadataBuilderImpl.<init>(MetadataBuilderImpl.java:123)
        at org.hibernate.boot.MetadataSources.getMetadataBuilder(MetadataSources.java:136)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:653)
        at org.grails.orm.hibernate.cfg.HibernateMappingContextConfiguration.buildSessionFactory(HibernateMappingContextConfiguration.java:287)
        at org.grails.orm.hibernate.connections.HibernateConnectionSourceFactory.create(HibernateConnectionSourceFactory.java:86)
        at org.grails.orm.hibernate.connections.AbstractHibernateConnectionSourceFactory.create(AbstractHibernateConnectionSourceFactory.java:39)
        at org.grails.orm.hibernate.connections.AbstractHibernateConnectionSourceFactory.create(AbstractHibernateConnectionSourceFactory.java:23)
        at org.grails.datastore.mapping.core.connections.AbstractConnectionSourceFactory.create(AbstractConnectionSourceFactory.java:64)
        at org.grails.datastore.mapping.core.connections.AbstractConnectionSourceFactory.create(AbstractConnectionSourceFactory.java:52)
        at org.grails.datastore.mapping.core.connections.ConnectionSourcesInitializer.create(ConnectionSourcesInitializer.groovy:24)
        at org.grails.orm.hibernate.HibernateDatastore.<init>(HibernateDatastore.java:200)
        at sun.reflect.GeneratedConstructorAccessor53.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:172)
        ... 38 common frames omitted
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.cache.ehcache.internal.EhCacheRegionFactory] as strategy [org.hibernate.cache.spi.RegionFactory]
        at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:133)
        at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:212)
        at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:169)
        at org.hibernate.cache.internal.RegionFactoryInitiator.resolveRegionFactory(RegionFactoryInitiator.java:93)
        at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:47)
        at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:32)
        at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:94)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
        ... 56 common frames omitted
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.cache.ehcache.internal.EhCacheRegionFactory]
        at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:136)
        at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:129)
        ... 63 common frames omitted
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.cache.ehcache.internal.EhCacheRegionFactory
        at org.hibernate.boot.registry.classloading.internal.AggregatedClassLoader.findClass(AggregatedClassLoader.java:210)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)
        at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:133)
        ... 64 common frames omitted

Anything I am missing from configuring second level cache for hibernate in this grails version 4.0.1 ?

Thanks.

Upvotes: 0

Views: 987

Answers (2)

Kiran Chitturi
Kiran Chitturi

Reputation: 503

Apparently, i had ehcache-core in my gradle build instead of ehcache.

This fixed my issue :

compile group: 'net.sf.ehcache', name: 'ehcache', version: '2.10.6'

Grails 4.0.1 uses hibernate 5.4.0, so left hibernate-ehcache version to 5.4.0 same as hibernate version.

Changed back cache configuration as below :

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

Upvotes: 1

Soundararajan
Soundararajan

Reputation: 2194

I have the same error, surprisingly today with grails 3.3.11 and removing

compile group: 'net.sf.ehcache', name: 'ehcache-core', version: '2.6.11'

and adding

compile "org.hibernate:hibernate-ehcache:5.1.17.Final"

helped me get past the error. My application.yml configuration is as follows


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

Upvotes: 0

Related Questions