Ok, this was a weird problem, but I finally soved it and decided I should post it here for others to see and use.
I am using ehcache for a simple 2nd level cache.
In my application I noticed that the cache gets evicted, even for entities that are not updated and should be read only. I have created a test, and found out that my cache gets evicted when I execute a native SQL update using createSQLQuery.
It doesn't matter which table I try to update, the entire cache was evicted and cleared!
After some debugging inside the Hibernate code, I found that hibernate creates a query spaces list, of all the entities that are part of the query. It appear that if you do not specify anything, all the entities end up in this list, which means that all the entities in the cache are cleared.
To solve it, I have added a call to the addSynchronizedQuerySpace() method on my SQLQuery instance, adding the actual table name I want to change. This solved my problem.
I do not know if this is a bug or a feature, but I have not found any documentation about this behavior.
Hope it helps someone.
|