16

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!

DeejUK
  • 12,891
  • 19
  • 89
  • 169
  • The problem with EHCache and Hibernate was probably referring using Hibernate from your client code and then transparently make hibernate transfer to ehcache first, and the actual persisting exectued asynchronously as write-behind. I don't see any problem with the client code writing to the cache directly and using hibernate independently for write-behind. – GeertPt Dec 25 '11 at 10:38

1 Answers1

5

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?

GeertPt
  • 16,398
  • 2
  • 37
  • 61
  • Thanks for sharing your experiences, that certainly sounds much easier than I was expecting! I haven't tried writing a write-behind cache, as in my original question we were looking for 'transparent' solutions - ones that require minimal coding, but can be achieved through configuration and use of libraries alone. – DeejUK Dec 27 '11 at 13:17
  • I've done something very similar: Parts of the application use EHCache, backed by JPA (behind an EJB3 SLSB). Very simple, as long as you are okay with a fully disconnected object graph. – Joshua Davis Dec 27 '11 at 21:43
  • @JoshuaDavis you need to find a balance between disconnecting your domain objects and denormalizing. I saw an interesting presentation at JAX London by Ben Stopford how they did id at the Royal Bank of Scotland: http://www.benstopford.com/2011/01/27/beyond-the-data-grid-building-a-normalised-data-store-using-coherence/ – GeertPt Dec 27 '11 at 22:22
  • @greyfairer True, and you have to be very careful when trying to mix this kind of cache with 'typical' JPA usage. – Joshua Davis Dec 28 '11 at 21:18