Manoj Kathiriya
Manoj Kathiriya

Reputation: 3416

Which is best Spring Cache Abstraction or Hibernate EHCache?

I am planning to implement the Cache in the web application.

I have already used the Hibernate EHCache in one of my previous web application.

Now a days, recently i came to know that in latest Spring release there is Cache Abstraction layer.

Hence i would like to is it just wrapper of Hibernate EHCache or it is Cache features provided by Spring itself.

If different then would like to know which is best or better option to use Cache ?

Also, i came to know from few articles that Hibernate EHCache not supporting clustering, is it ? Because clustering required for big Web Application.

I have used Spring 3.1 and Hibernate 3.3 in Web Application.

Upvotes: 2

Views: 2407

Answers (3)

Amit Patil
Amit Patil

Reputation: 790

Just to add, for database-level caching using hibernate, hibernate/JPA second-level cache seems more appropriate. Because it manages data caching at entity level at the persistence context level (JPA APIs), so there are very fewer chances of inconsistency in database and cache, since hibernate manages it well.

But if we use spring cache for database caching, then we always have to ensure every method operating on database need to be annotated with appropriate cache annotation (For example, @CacheEvict for remove), so there are chances of missing annotations and causing inconsistency.

Also, spring cache can only be used on public methods, since it backed my AOP (We can use AspectJ though to solve this).

Upvotes: 1

Paul Cunningham
Paul Cunningham

Reputation: 91

Hibernate caching continues to work with Spring using EhCache (or another caching provider). It offers a 2nd level cache for storing em.find(key) entities and relationships (@Cache annotations or XML), as well as query caching if you q.setHint("org.hibernate.cacheable",true). These are integrated and well-documented. Spring caching is not needed for this.

But Spring provides method caching which is independent of Hibernate caching, and can still use EhCache as the caching provider (sharing EhCache with Hibernate). This can be a little confusing because the EhCache configurations are overlapping, but essentially Spring Caching allows you to independently cache the results of methods that are marked as @Cacheable.

Note 1: Spring/Method caching is not integrated with Hibernate. Results cached with method caching are not recognized or maintained by Hibernate.

Note 2: Hibernate Query caching is not recommended for highly distributed systems without a good understanding of how the caching is used. It increases the risk of deadlocks due to the use of a shared entity timestamp cache that Hibernate maintains to keep track of when the cached query results should be evicted.

Upvotes: 0

pap
pap

Reputation: 27614

Hibernate and EHCache are different things.

EHCache is the cache provider, i.e. the actual implementation of the cache.

Hibernate can use multiple different providers for the L2 cache, including EHCache.

Spring Cache is a framework for caching method calls. It can use multiple different providers, including EHCache.

EHCache offers distribution (clustering) in several modes, from basic JMS-driven synchronization to Terracotta BigMemory distribution. Hibernate L2 cache should support clustering.

Spring Cache has nothing to do with Hibernate, but you can use Hibernate L2 cache and Spring Cache (method caching) in parallel with EHCache as the underlying provider for both.

Upvotes: 4

Related Questions