Clearing Hibernate Query Cache in Grails

23 / Jun / 2010 by Vivek Krishna 6 comments

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

FOUND THIS USEFUL? SHARE IT

comments (6)

  1. Sigma Infosolutions

    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

    Reply
  2. Mikael

    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.

    Reply
    1. Vivek Krishna

      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

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *