Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate.cache.region.factory_class Required in hibernate.cfg.xml

I am working on using memcache as second level cache for Hibernate. I am using hibernate-memcached-1.2.4, spymemcached 2.8.0 and hibernate 4.1.4.

But when i try to use it , it gives me error saying

    Initial sessionfactory creation failedorg.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).
    Exception in thread "main" java.lang.ExceptionInInitializerError
        at Util.HibernateUtil.(HibernateUtil.java:16)
        at hibba.AccountDAOimpl.getAccount(AccountDAOimpl.java:23)
        at hibba.Connect.main(Connect.java:7)
    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.buildTimestampsRegion(NoCachingRegionFactory.java:87)
        at org.hibernate.cache.spi.UpdateTimestampsCache.(UpdateTimestampsCache.java:63)
        at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:510)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1782)
        at Util.HibernateUtil.(HibernateUtil.java:12)
        ... 2 more

The properties written in hibernate.cfg.xml is:

<pre>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="cache.provider_class">com.googlecode.hibernate.memcached.MemcachedCacheProvider </property>
        <property name="hibernate.memcached.memcacheClientFactory">com.googlecode.hibernate.memcached.dangamemcached.DangaMemcacheClientFactory</property>      
        <property name="hibernate.Memcached.servers"> localhost:11211 </property>
        <property name="hibernate.Memcached.cacheTimeSeconds">300</property>
        <property name="hibernate.Memcached.connectionFactory">KetamaConnectionFactory</property> </pre>
like image 758
Anil Kumar Avatar asked Jul 05 '12 16:07

Anil Kumar


People also ask

Is it necessary to use Hibernate cfg XML for configuration in Hibernate?

Basically you are setting all the required properties via your properties object so there is no real need to tell Hibernate to look for a hibernate. cfg. xml file which is exactly what the configure() method does.

What is cache region in Hibernate?

For each entity class, Hibernate will use a separate cache region to store state of instances for that class. The region name is the fully qualified class name. For example, Foo instances are stored in a cache named com.

How do I enable caching in Hibernate?

To use the query cache, you must first activate it using the hibernate. cache. use_query_cache="true" property in the configuration file. By setting this property to true, you make Hibernate create the necessary caches in memory to hold the query and identifier sets.


1 Answers

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>
like image 78
Pau Avatar answered Sep 24 '22 04:09

Pau