Hibernate second level cache collection eviction
Hi,
In our application we have a JSF tree which displays the hierarchy of our entities. Whenever the relationship between parent and child is changed in the database, we immediately synchronize the state with the tree.
We recently added EhCache as our second level cache implementation, both for Entities and Collections. It turns out that even if you add a new entity that is involved in a parent child relationship, the Collection that resides in the cache will NOT be updates and so you will have a dirty version.In our case that resulted in a tree that does not reflect the true stat of the DB.
What needs to be done is to use the session factory to evict to parent entity and then to evict the collection from the cache. Only then will it be updated again and reflect the true state of the DB. I followed the code suggested here:
http://jaitechwriteups.blogspot.com/2006/08/evict-collection-from-hibern...
And using a SEAM observer to handle the event.
If someone (Yanai ...) has a better solution , please speak up.

Comments
Hi,
In my opinion there is an easier way - cache usage="read-write"
You should use read-write both on the entity level and the collection level.
If you use read-write strategy, Hibernate will automatically invalidate your entity/collection cache whenever they get updated, and your next fetch will go to the DB and retrieve the right tree while updating the cache.
Hi Yanai,
I am using:
@org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
I use it both on entity level and collection level, however hibernate does not invalidate the cache. Where is this behavior you are referring to documented? couldn't find anything about it.
Thanks.
In Hibernate book its said:
"Read-write—This strategy maintains read committed isolation, using a timestamping mechanism and is available only in nonclustered environments. Again, use this strategy for read-mostly data where it’s critical to prevent stale data in concurrent transactions, in the rare case of an update."
So as long as you didn't change the transaction-level (read committed), and assuming we are NOT talking about some critical race on a cluster env, this should work smoothly and in coordination with your DB transaction (your DB is getting updated with your new collection and on the same time cache is updated appropriately).
I think the best way to go is write a simple unit test against the code make it work and then try to understand the different behavior in your system.
I actually started with a simple unit test, tested over and over again and then left with no choice used the eviction method mentioned above
The quote above might only be applicable to cached Entities and not to cached collections, this is the-facto behavior I am observing.
Bottom line is that unless I evict the owing entity and the collection itself, I am left out with a dirty collection which does not reflect the actual DB state.
Hi,
In order to prove this functionality actually **does work**, I created a simple project with your case (parent-child).
Please download it at: https://planet.tikalk.com/downloads/courses/hibernate/cache-invalidation...
It is maven based project, so if you run the mvn install after unzip you'll see the TestCase works. Here a simple explanation for the test:
I created an Entity called Person which has a set of children from the same Person class (a.la. parent-child). And here is the test case (each step is a separate transaction and session):
1. Create a parent person in the DB
2. Load the parent person from db and assert the children set is empty
3. Add a new child to the parent and save both the child and updated parent into the db.
4. Assert cache invalidation - load the parent again. Since the children set became stale, Hibernate hit the DB rather than the cache.
Please note - If your case is bi-directional relationship and you don't update both sides, then this might cause the problem in your code...
Yanai,
First, thank you very much for setting up this test case, I really appreciate it.
Yes, you were right, I do have a bi-directional relationship and I did NOT update both sides hence the Collection cache wasn't updated. This resolved the issue.
Once again thanks for your help,
Great,my pleasure.
hi
i have the same problem as shlomo, but i cant download the sample application it asks for login and password but my tikalk login doesnt works, what login should i use, or can you send it to me via email on robert.machata@gmail.com
Here it is.
Sorry for inconvenience.
thank you very much