Help

Ehcache 2.0, which was recently released, supports the new Hibernate 3.3/3.5 SPI with its CacheRegionFactory.

This new provider has the following benefits:

  • The SPI removed synchronization in the Hibernate cache plumbing. It is left up to the caching implementation on how to control concurrent access. Ehcache, starting with 1.6, removed syncrhonization in favour of a CAS approach. The results, for heavy workloads are impressive.
  • The new SPI provides finer grained control over cache region storage and cache strategies. Ehcache 2.0 takes advantage of this to reduce memory use. It provides read only, nonstrict read write and read write strategies, all cluster safe.
  • Ehcache 2.0 is readily distributable with Terracotta Server Array. This gives you cluster safe operation (coherency), HA and scale beyond the limits of an in-process cache, which is how most Hibernate users use Ehcache today. There is the existing ehcache.jar and ehcache-terracotta.jar which provides the client library. It is a couple of lines of config to turn that on in ehcache.xml:
<ehcache>
<terracottaConfig url="someserver:9510"/>
<defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToLiveSeconds="600"
            <terracotta clustered="true" coherent="true"/>
/>

    <cache name="com.company.domain.Pets"
           maxElementsInMemory="10000"
           eternal=”true”>
           <terracotta clustered="true" coherent="true"/>
           </cache>
</cache>
</ehcache> 

Enabling Ehcache 2.0 as the CacheRegionFactory

Various versions of Ehcache ship with Hibernate. To get version 2.0 you can either download it or, if you are Maven based, add the following Maven snippet:

       <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>[2.0.0,]</version>
            <type>pom</type>
        </dependency>

You then enable it in your hibernate settings with:

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

More Information

We have been updating the Hibernate Chapter on Ehcache.org. We plant to continue to add tips and tricks together with deep reference information on Hibernate caching with Ehcache there.

4 comments:
 
23. Mar 2010, 16:33 CET | Link

This is interesting. We're using the default cache implementation in jboss seam 2.1.2 with jboss 4.2.3, jboss cache 1.4.1. Do you know if we can plug ehcache instead without too much trouble ?

Another thing, the s:cache jsf component allows to put and get a page fragment into jboss cache. Do you know if it's possible to replace it with ehcache ?

The performance looks interesting. I guess we should switch to jboss 5 too to get jboss cache 3 to compare. Thanks !

 
07. Apr 2010, 22:15 CET | Link
Alejandro

Hi, im new using hibernate...... Well i builded a simple ajax app with jsp in the backened. I tried to implement a cache with ehcache but when i inserted the data and the made a query the table didnt refreshed.... if someone can help me....

 
08. Jul 2010, 14:15 CET | Link
Why in Hibernate website the clustering option for EHCahce is not distinguished?
http://docs.jboss.org/hibernate/core/3.5/reference/en/html/performance.html#performance-cache
 
03. Apr 2014, 11:14 CET | Link

The new SPI provides finer grained control over cache region storage and cache strategies. Ehcache 2.0 takes advantage of this to reduce memory use. It provides read only, nonstrict read write and read write strategies, all cluster safe.