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!
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.
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.
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).
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With