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:
<terracottaConfig url="someserver:9510"/>
            <terracotta clustered="true" coherent="true"/>

    <cache name=""
           <terracotta clustered="true" coherent="true"/>

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:


You then enable it in your hibernate settings with:

<property name="hibernate.cache.region.factory_class">

More Information

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

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

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?