Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure second level cache in Hibernate 4.1.5 SP1?

I have read other threads on here about this subject but none of the solutions work for me.

I tried putting this in my hibernate.cfg.xml:

<property name="hibernate.cache.region.factory_class">org.hibernate.cache.spi.EntityRegion</property>

I always get this error: could not instantiate RegionFactory [org.hibernate.cache.spi.EntityRegion]

I also tried most suggestions from threads on Hibernate websites but no luck.

So how do I configure this?

like image 937
MrStack Avatar asked Aug 06 '12 16:08

MrStack


3 Answers

Well, I found the answer (from a Youtube user):

  1. Use hibernate-release-4.1.0.Final or later versions.
  2. Add jars from lib\optional\ehcache directory
  3. Update hibernate.cfg.xml:

    <property name="cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</property>
    
  4. Add slf4j-api-1.6.1.jar (I found it in ehcache-2.5.1-distribution.tar­.gz downloaded from ehcache.org in addition) because of ClassNotFoundException.

  5. Add this to your hibernate.cfg.xml:

    <!-- Enable Hibernate's automatic session context management -->
    <property name="cache.use_second_level_cache">true</property>
    

The key point here was adding the ehcache jar from the optional\ directory in Hibernate4.

like image 85
MrStack Avatar answered Oct 13 '22 16:10

MrStack


Add the following properties to the Hibernate properties:

<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">hibernate-ehcache.xml</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.cache.use_structured_entries">true</prop> 

You need hibernate-ehcache 4.1.1 jar or not ehcache jar.

Please make a note of the cache provider class which has changed.

like image 22
amit shetye Avatar answered Oct 13 '22 15:10

amit shetye


Hope, it may be of some use to somebody (hibernate 4.x).

The excerpt from my spring configuration:

public Properties hibernateProperties() {
    Properties properties = new Properties();
    properties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
    properties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
    properties.setProperty("hibernate.format_sql", "true");
    properties.setProperty("hibernate.generate_statistics", env.getProperty("hibernate.generate_statistics"));
    // second-level cache:
    properties.setProperty("hibernate.cache.use_second_level_cache", "true");        
    properties.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.EhCacheRegionFactory");
    properties.setProperty("net.sf.ehcache.configurationResourceName", env.getProperty("net.sf.ehcache.configurationResourceName"));
    return properties;
}

You must have 'org.hibernate:hibernate-ehcache:HIBERNATE_VERSION' on your classpath.

See: Ehcache doc on hibernate configuration

like image 36
Łukasz Dumiszewski Avatar answered Oct 13 '22 14:10

Łukasz Dumiszewski