nirav
nirav

Reputation: 77

spring hibernate and caching using ehcache

I am using spring 3.2 and hibernate 4.2.8 and tried using ehcache but the moment I annotate an entity class with @Cache(org.hibernate.annotations.Cache) i get the following exception

Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given, please either disable second level cache or set correct region factory class name to property hibernate.cache.region.factory_class (and make sure the second level cache provider, hibernate-infinispan, for example, is available in the classpath).
    at org.hibernate.cache.internal.NoCachingRegionFactory.buildEntityRegion(NoCachingRegionFactory.java:69)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:351)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1797)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1868)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:247)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:373)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:358)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1547)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1485)
    ... 42 more

My Configuration is as shown below

<bean id="sessionFactoryAdmin"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="adminDataSource" />
        <property name="packagesToScan" value="com.digilegal.services.ahc.model.user" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
                <prop key="hibernate.cache.provider_configuration">/WEB-INF/ehcache-entity.xml</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
            </props>
        </property>
    </bean>

I have tried all possible links on the internet but could not solve the problem

Here is my /WEB-INF/ehcache-entity.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache>
    <cache name="com.digilegal.services.ahc.model.user.UserNotifications"
        maxElementsInMemory="500"
        eternal="false"
        overflowToDisk="false"
        timeToIdleSeconds="69"
        timeToLiveSeconds="65" />


    <cache name="org.hibernate.cache.StandardQueryCache"
        maxEntriesLocalHeap="15" eternal="false" timeToLiveSeconds="60"
        overflowToDisk="true" />
</ehcache>

Thanks ​Nirav

Upvotes: 2

Views: 8051

Answers (1)

Angular University
Angular University

Reputation: 43087

I had this error recently, and in my case the error was in a test that was using a different session factory configuration to where no hibernate.cache.region.factory_class was specified.

So the presence of the @Cache annotation caused the test to abort with the exact same error. In my case i did not want caching for that particular session factory so I added:

<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>

have a look at your config to see if you don't have multiple session factories defined, and some don't provide hibernate.cache.region.factory_class.

Upvotes: 11

Related Questions