views:

68

answers:

3

I am configuring my hibernate project to use a 2nd-level cache provider, so that I can take advantage of query caching.

I added a dependency to ehcache:

   <dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-core</artifactId>
    <version>2.2.0</version>
   </dependency>

I think that the provider class I want to use is:

net.sf.ehcache.hibernateEhCacheProvider

When I look at the referenced libraries in eclipse, I see the @Deprecated annotation on EhCacheProvider, and also on SingletonEhCacheProvider. What gives? Is there an up-to-date replacement provider that I can use?

I am using hibernate version 3.4.0.GA, in case it matters.

+2  A: 

The EhCache docs say that from Hibernate 3.3 onward you should use:

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

(or net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory)

Bozho
+3  A: 

What gives? Is there an up-to-date replacement provider that I can use?

They have been deprecated in favor of the classes implementing the new Hibernate 3.3/3.5 SPI with its CacheRegionFactory. These implementations are respectively:

  • net.sf.ehcache.hibernate.EhCacheRegionFactory
  • net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory

Benefits of the new SPI include:

  • 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. (...)

You are thus encouraged to use the new implementations. Configuration is done via the following property:

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

That replaces the hibernate.cache.provider_class property.

References

Pascal Thivent
A: 

True. But how to reset region prefix for this new provider?

Example 1 - this works ok

<property name="hibernate.cache.region_prefix"></property> 
<property name="hibernate.cache.provider_class">org.hibernate.cache.EHCacheProvider</property>

Example 2 - after change

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

Second configuration doesn't respect common region prefix and gives warn WARN [AbstractEhcacheRegionFactory] Couldn't find a specific ehcache configuration for cache named [persistence.unit:unitName=services-ear.ear/APP-INF/lib/services-jpa-0.4.09.1-SNAPSHOT-par.jar#services-jpa.Par]; using defaults.

Krashan