Why is EhCacheProvider deprecated?
-
02-10-2019 - |
Question
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.
Solution
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 documentation
OTHER TIPS
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 ofnet.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
andorg.hibernate.cache.ehcache.EhCacheRegionFactory
instead ofnet.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
)
EhCache 2 is now deprecated and discontinued. You should use EhCache 3 instead. In Hibernate versions after 5.3 it is recommended to use JSR-107 (JCache). In order to do that 2 dependencies are needed:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jcache</artifactId>
<version>your_hibernate_version</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.6.3</version>
<scope>runtime</scope>
</dependency>
The first one provides JSR-107 API compliant with Hibernate. The second one is actual cache implementation - EhCache 3.
Also new RegionFactory
must be used:
hibernate.cache.region.factory_class=org.hibernate.cache.jcache.JCacheRegionFactory