Hibernate NoCacheRegionFactoryAvailableException

I'm getting a bizarre Hibernate exception that I can't explain. It's telling me that I'm using 2nd level cache, but no where in hibernate.cfg.xml do I specify a 2nd level cache. Here's the exception:

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:348)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1769)
    at net.me.myapp.common.dao.SessionFactoryProvider.newSessionFactory(SessionFactoryProvider.java:37)
    at net.me.myapp.common.dao.BaseDAO.doPersist(BaseDAO.java:28)
    at net.me.myapp.common.dao.WordDAO.deleteAllWords(WordDAO.java:36)
    at net.me.myapp.tools.dmapper.DictionaryMapper.run(DictionaryMapper.java:88)
    at net.me.myapp.tools.dmapper.DictionaryMapper.main(DictionaryMapper.java:56)

And my hibernate.cfg.xml:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- DataSource & Connection info. -->
        <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
        <property name="hibernate.connection.driver.class">org.h2.Driver</property>
        <property name="hibernate.connection.url">jdbc:h2:file:/${MYAPP_HOME}/data/myapp</property>
        <property name="hibernate.connection.username">myapp</property>
        <property name="hibernate.connection.password">mypassword</property>
        <property name="hibernate.connection.pool_size">1</property>

        <!-- General Hibernate settings. -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="use_sql_comments">true</property>

        <!-- DDL Mode. -->
        <property name="hbm2ddl.auto">validate</property>

        <!-- All our Hibernate mapping XML files. -->
        <mapping class="net.me.myapp.common.dto.WordDTO" />
    </session-factory>
</hibernate-configuration>

Any ideas what would be triggering this exception? Thanks in advance!

Pau wrote on hibernate.cache.region.factory_class Required in hibernate.cfg.xml:

The exception is quite self-explanatory. You have to set the hibernate.cache.region.factory_class property. For instance with ehcache would be adding the following line:

<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>

NoCacheRegionFactoryAvailableException (Hibernate JavaDocs), Indicates a condition where a second-level cache implementation was expected to be to available, but none was found on the classpath. See Also: Serialized  EHCache Configuration + Spring Boot: NoCacheRegionFactoryAvailableException Tag: hibernate , spring-mvc , spring-boot , ehcache , second-level-cache I'm trying to configure Spring Boot application with second level cache based on EHCache.

Using the following line fixed it :

<beans:entry key="hibernate.cache.use_second_level_cache" value="false"/>

But the Hibernate message is probably a warning that we should use second level cache?

org.hibernate.cache.NoCacheRegionFactoryAvailableException, org.hibernate.cache. Class NoCacheRegionFactoryAvailableException. java.​lang. Implementation of NoCacheRegionFactoryAvailableException. See Also:​  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).

I also received this error and took me a while to track down. At one point we were going to have multiple cache regions, but in the end decided we were just going to have one cache pool.

When we merged that change into an older branch - we still had an entity with the old strategy of a cache pool per entity:

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region="tableRegion")
@Table(name = "table")
public class table {

By removing the Cache annotation - it resolved the org.hibernate.cache.NoCacheRegionFactoryAvailableException:

@Entity
@Table(name = "table")
public class table {

Figured I'd post in case anyone else had a similar situation

hibernate-orm/NoCacheRegionFactoryAvailableException.java at , Hibernate, Relational Persistence for Idiomatic Java. *. * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the  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).

This error is very misleading, I spent almost a day to finally figure out the root cause. Thought my hibernate config file has defined second level cache and factory class, it was giving me error that hibernate.cache.region.factory_class is not given.

I see that by hibernate.cfg.xml file is also available in classpath. But even after any change in that there was no impact and getting same error.

Finally I realized that for test purpose I have overridden persistence.xml file which has below missing properties under persistence-unit. After adding that issue was resolved.

 <properties>
            <property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml" />
</properties>

So this means my application was not able to find hibernate.cfg.xml file and somehow instead of giving error related to missing configuration, it cries out for factory class.

org.hibernate.cache.NoCacheRegionFactoryAvailableException , NoCacheRegionFactoryAvailableException when Spring initializes When omitting the hibernate.cache.region.factory_class property from  Initial SessionFactory creation failed: 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

These are the property you need to add to enable second level cache

<!-- Provider for second level cache -->
        <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

Initial SessionFactory creation failed: org.hibernate.cache , NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is  Recibo una extraña excepción de Hibernate que no puedo explicar. Me está diciendo que estoy usando caché de segundo nivel, pero no en ningún lugar en hibernate.cfg.xml especifico un caché de segundo nivel.

Spring boot and Hibernate Second Level cache giving , Spring boot and Hibernate Second Level cache giving NoCacheRegionFactoryAvailableException: Second-level cache is used in the. hibernate.cache.use_second_level_cache is used to enable the second level cache. hibernate.cache.use_query_cache is used to enable the query cache, without it HQL queries results will not be cached.

Hibernate Second-Level Cache, As most other fully-equipped ORM frameworks, Hibernate has the concept of first-​level cache. It is a session scoped cache which ensures that  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 using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath.

property hibernate.cache.region.factory_class is not given, 53 more Caused by: org.hibernate.cache.​NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but  Parameters: message - Message explaining the exception condition cause - The underlying cause; CacheException public CacheException(Throwable cause)

Comments
  • I see this is quite old question, however still helped today! I was also getting this exception and he answer from @wilkejj helped me track it. Hibernate, I assume checks annotations. And in my case, I found annotation @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) to be the one triggering the second level cache. When I removed it, all worked as a charm!
  • Thanks @Yousuf Umar (+1) - I know the exception is self-explanatory, and I will try your suggestion out. However I'm confused as to why I'm getting it when I don't make any reference to the a 2nd level cache from inside my hibernate.cfg.xml. In other words, why does Hibernate think I need to use a 2nd level cache? I know that in Hibernate, 2nd level caches are optional, so why does Hibernate think I need one? Thanks again!
  • Check if you hava <beans:entry key="hibernate.cache.use_second_level_cache" value="true"/>
  • I did not have the use_second_level_cache and Hibernate actually thinks I should use a second level cache as well. It appeared after I added several new class to the hibernate.cfg.xml file...