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.
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
- Hibernate Blog
- Ehcache 2.0 supports new Hibernate 3.3 caching provider
- EhCache documentation
- Upgrading From Ehcache versions prior to 2.0
- Hibernate Second Level Cache
if you wish to use Hibernate 4.0.0.Final. for the value of hibernate.cache.region.factory_class property use:
org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
instead of net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
and
org.hibernate.cache.ehcache.EhCacheRegionFactory
instead of net.sf.ehcache.hibernate.EhCacheRegionFactory
Otherwise you will end up with some internal ClassNotFound exceptions
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
)