My colleague and I discussed when (if?) A collection cached in the second level cache can return stale data (we use ehcache and hibernate 3.2.4).
Here's the script:
- P's parent is cached and is an entity.
- A parent has a collection (bag) of children who are cached and lazy.
- Children C objects are cached and are objects.
- We use Nonstrict read-write as our concurrency cache strategy for all of these.
- Our factory session is a process area and only 1 JVM is involved.
- Assume that the cache areas for all of the above caches are large enough to easily store all instances of all children and parents in memory.
At time T1: load parent P and children C1 ... CN by doing session.load (p) and iterating over the children's collection to load all C1 ... CN.
At time T2: another session loads C1 and updates C1, changing some of its data.
Under what circumstances can I call P.getChildren (). get (0), and will the obsolete version C1 be returned (version C1 loaded at time T1)?
I think there are two:
- If I am in the same hibernation session as the operations in T1 (in this case, the session caching version is returned)
- With a non-strict reading record, it will be possible to have a race condition, when updating to T2 and loading occurs almost simultaneously, and loading wins the race and retrieves an obsolete object from the cache. (In this case, I can switch to read-write and I will be fine)
source share