Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write-Behind Cache for JPA

It would appear from searching around here and the web at large that it is not possible to implement EHCache as a write-behind cache for Hibernate, as that would require substantial changes to the Hibernate code.

Are there any other solutions (preferably open source) for a JPA provider that can 'transparently' hook into a write-behind cache implementation, and preferably one that can be distributed with something like Terracotta?

I've read that EclipseLink and Oracle Coherence can achieve this, but Coherence is sadly not a cheap solution!

like image 458
EngineerBetter_DJ Avatar asked Aug 26 '11 12:08

EngineerBetter_DJ


People also ask

What is write-behind cache?

9.4 Write-Behind Caching In the Write-Behind scenario, modified cache entries are asynchronously written to the data source after a configurable delay, whether after 10 seconds, 20 minutes, a day or even a week or longer.

Does JPA have cache?

Caching in JPA is required within a transaction or within an extended persistence context to preserve object identity, but JPA does not require that caching be supported across transactions or persistence contexts. JPA 2.0 defines the concept of a shared cache.

In which scenario should we implement a write through caching strategy?

This is used when there are no frequent writes to the cache(The number of write operations is less). It helps in data recovery (In case of a power outage or system failure). A data write will experience latency (delay) as we have to write to two locations (both Memory and Cache).


1 Answers

We did write a write-behind cache handler for Coherence, based on Hibernate.

What's stopping you from writing an EHCache CacheWriter using any JPA implementation, as described in http://ehcache.org/documentation/apis/write-through-caching. You could extend AbstractCacheWriter, and all you'll need to implement is write(net.sf.ehcache.Element), writeAll(java.util.Collection), delete(net.sf.ehcache.CacheEntry) and deleteAll(java.util.Collection).

Just make sure that it is completely independent of the surrounding transaction. Your application then writes to the cache alone, and does not use JPA anymore.

What are the problems you've encountered?

like image 66
GeertPt Avatar answered Sep 16 '22 15:09

GeertPt