Clearing Hibernate Query Cache in Grails
In one of the projects, we had used Query Caching to improve the performance. However, it also meant that the updates/inserts into a table did not get reflected immediately, i.e. something like:
DomainClass.findByPropertyName("propertyName", [cache: true])
returned the same list as it was, before the insertion/updation took place. We found that this could be resolved by clearing the query cache every time an update took place. This was done in Grails by making a call:
sessionFactory.queryCache.clear()
Make sure that the artefact(controller, service, taglib, job etc.) has sessionFactory injected into it using the line:
def sessionFactory
Hope this helps.
– Vivek
vivek[at]IntelliGrape[dot]com
IT worked for me ..Thanks
Great information 🙂
Time spent reading your post is worthwhile. Many of people should certainly read this!
Hi ,
My Problem is that I am using grails Weceem CMS Plugin .
I want to combine Jsecurity plugin with this weceem cms plugin.
how to do it ?
Please help me in find the solution.
Regards
DINESH T
Query cache entries are automatically invalidated when performing an insert/update. Check your mapping closure in the domain class if you have set up caching correctly. The exception is if you update your db from outside your app. Also be very careful with clearing the whole query cache at once – on a high-load website it could be too much for the db to handle.
Hi Mikael,
We had not configured caching as such inside our domain class. We just had the entry “cache.use_query_cache=true” in DataSource.groovy.
The cache was not getting cleared automatically, which is why we had to resort to such a technique.
Also, the application was small and the content had to be changed only occasionally.
Thanks for the tips. 🙂
–Vivek