Why is EhCacheProvider deprecated?
Asked Answered
T

4

21

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.

Torrez answered 11/9, 2010 at 5:4 Comment(0)
M
47

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

Mauramauralia answered 11/9, 2010 at 7:40 Comment(1)
Doesn't work here due to class : org.hibernate.cache.TimestampsRegion not being found. I have ehcache, jcache and hibernate 5 on the classpath ..Randellrandene
M
17

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

Macpherson answered 2/1, 2012 at 14:20 Comment(1)
Very good point...here is the reason from a terracotta engineer : (forums.terracotta.org/forums/posts/list/6674.page --> second post)Wenonawenonah
K
10

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
Kuvasz answered 19/2, 2019 at 21:2 Comment(5)
JCache is ehcache ? I am confused. What is JCache. Why are EcacheREgionFactory classes still not deprecated. It is also not picking up the configuration file of EhCache.xmlRandellrandene
@mmm Ehcache from version 3.0 is a JCache API implementation (also called JSR-107). Look at the Ehcache docs: ehcache.org/documentation/3.8/107.htmlDevanagari
does that mean that I now have to annotate my entities? I did not have to do that before. My old configuration is not working now. I made a new post here: #63484997Randellrandene
In 2022, ehcache 3 and hibernate 5 in programatic approaches for legacy code (not using Cache annotations) is a hairball of dead links and confusion. JSR 107 seems only partly implemented. I had to give up on ehcache 3 programatic configuration due to all the holes and class conflicts I was encountering. Hibernate and ehcache teams have duplicate class entries in different package structures (e.g. EhCacheRegionFactory or when to use hibernate-ehcache). Documentation and examples are sparse and inadequate. I'm reading sources just to try and grok whats going on.Pompadour
dead end. Failed to collect dependencies at org.ehcache:ehcache:jar:3.10.8 ->org.glassfish.jaxb:jaxb-runtime:jar:2.3.0-b170127.1453 ->org.glassfish.jaxb:jaxb-core:jar:2.3.0-b170127.1453 ->javax.xml.bind:jaxb-api:jar:2.3.0-b161121.1438: Failed to read artifact descriptor for javax.xml.bind:jaxb-api:jar:2.3.0-b161121.1438: Could not transfer artifact javax.xml.bind:jaxb-api:pom:2.3.0-b161121.1438 from netbeans (bits.netbeans.org/nexus/content/groups/netbeans): Not authorized, ReasonPhrase:Repository decommissioned. Please refer to netbeans.apache.org/about/oracle-transition.htmlPompadour
D
6

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)

Delta answered 11/9, 2010 at 7:38 Comment(1)
FYI That EhCache doc link is dead.Tunable

© 2022 - 2024 — McMap. All rights reserved.