Edited to add – to clarify I’m using EhCache as my Cache Provider
This will be a slightly more technical post than usual, I’ve struggled through a few problems whilst writing a chapter on “Second Level Caching” for our new Hibernate and JPA course, and I wanted to capture the results of the struggles.
This will be a long post, so sorry for the waffle, but the executive summary is:
JPA2 has a @Cacheable annotation, but it allows no parameters to tune the CacheConcurrencyStrategy. It isn’t clear, but the default setting will be READ_WRITE.
This blog post assumes you already understand the basics of the Second Level Cache (2LC), so I’ll dive straight into the depths:
(Of course, if you don’t know about how 2LC works – then you’ll need our course! Get it from our website, the release date will be early February).
What is the CacheConcurrencyStrategy?
When configuring a 2LC using Hibernate, it is necessary to set the Concurrency Strategy for each cache region. This was either done in the XML mapping file, or using their @Cache annotation:
@org.hibernate.annotations.Cache(usage=CacheConcurrencyStrategy.READ_ONLY) public class Tutor { // etc }
- READ_ONLY. This sets the cache region as a read only cache, and this means it can be very performant as the cache doesn’t have to worry about locking. If you try to modify any of these objects though, you will get an exception.
- NONSTRICT_READ_WRITE. This allows you to modify cacheable objects, but the cache provider doesn’t need to implement a strict lock on the cache. This means there is a potential for stale objects (ie one transaction has modified an object, but another object picks up the old version of the object from the cache). Choose this for writeable objects, but only if you don’t care about stale data.
- READ_WRITE. The “safest” but least performant option. The cache provider will lock the cache when the object is updated, ensuring that all transactions will see the most up to date version.
@Cacheable // eeek - we can't specify anything here! public class Tutor { // etc }
Default access-type used when the configured using JPA 2.0 config. JPA 2.0 allows
@Cacheable(true)
to be attached to an entity without any access type or usage qualification.We are conservative here in specifyingAccessType.READ_WRITE
so as to follow the mantra of “do no harm”.
So, if you just use @Cacheable you will get a READ_WRITE Cache Region. Safe, but possibly not as performant as you would like.
If you don’t like this, then until JPA supports this property (if it ever does – it is an implementation specific feature), then you will need to add the old org.hibernate annotation:
@Cacheable @org.hibernate.annotations.Cache(usage=CacheConcurrencyStrategy.READ_ONLY) public class Tutor {
I guess you may as well remove the javax.persistence.Cachable annotation, it is really redundant.
I can’t decide if this is the fault of JPA2 or EhCache/Hibernate. I’m leaning towards the latter – why should we need to configure the Concurrency Strategy through the code when there’s a perfectly good place to do it, in the ehcache.xml file. This configures the details of each cache region, why isn’t it done in there?
Do feel free to comment and set me straight on anything I’ve missed or misunderstood!