Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7546203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:08:19+00:00 2026-05-30T09:08:19+00:00

Part I In a Grails app, I understand that you enable the 2nd level

  • 0

Part I

In a Grails app, I understand that you enable the 2nd level cache per domain class by adding

static mapping {
  cache true
}

By default the 2nd level cache is only used when get() is called, but it can also be used for criteria queries and dynamic finders by adding cache true to the query.

However, I’m still not sure I understand how the query cache works. My best guess is that:

  • there are separate query caches for each domain class, e.g. one for Book and another for Author
  • before a query like Author.findByName('bob', [cache: true]) is executed, a cache key is computed, which is based on the domain class (Author), the query (findByName) and the query parameters (‘bob’). If that key is found in the Author query cache, the cached results are returned instead of executing the query
  • any time an Author is saved, deleted, or updated, the Author query cache is flushed

This seems reasonable until we consider that a query that returns Book instances may join to the Author table. In that case, it would be necessary to flush both the Book and Author query caches when an Author is saved, deleted, or updated. This leads me to suspect that perhaps there is just one single query cache and it is cleared whenever any cached domain class is saved?

Part II

In the Grails docs it mentions that

As well as the ability to use Hibernate’s second level cache to cache instances you can also cache collections (associations) of objects.

For example:

class Author {    

  static hasMany = [books: Book]

  static mapping = { 
    cache true        // Author uses the 2nd level cache
    books cache: true // associated books use the 2nd level cache
  } 
}

class Book {
  static belongsTo = [author: Author]

  static mapping = {
    cache true // Book uses the 2nd level cache
  }
}

Does the configuration above make sense, i.e. if Author and Book are themselves using the 2nd level cache, is there any benefit to making the Author-Book association also use the 2nd level cache?

Part III

Finally, I’ve read this advice about using the 2nd level query cache, which suggests that it should only be used for infrequently changing domain classes. Are there any circumstances under which one should not enable the 2nd level cache for get() operations, i.e. any reason why one wouldn’t add the following to a domain class

static mapping = {
  cache true // Book uses the 2nd level cache
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:08:20+00:00Added an answer on May 30, 2026 at 9:08 am

    Part 1:

    Hibernate does the right thing. The query cache is not per entity. There is a single query cache region, shared by all queries, unless you set a specific region for a query. Each time a table is updated, its timestamp in the timestamps cache is updated. Each time a query is executed, the timestamp of each of the tables where the query searches is compared to the timestamp of the cached result. And of course, the cached result is returned only if itstimestamp is more recent than all the table timestamps.

    Part 2:

    Yes, it makes sense. The cache for the author remembers that the author with ID 456 has the name “foo” and the birth date 1975/07/19. Only the data stored in the author table is remembered. So, caching the association is also useful: instead of making an additional query to get the set of books of the author when calling author.getBooks(), Hibernate will get the IDs of the books of the author from its cache, and then load each book from the second-level cache. Make sure to cache the Books, though.

    Part 3:

    I can imagine several reasons:

    • there are so many entities and they are so changing that the number of cache hits would be very low, and that the second-level cache handling would in fact consume more time and memory than a solution without cache
    • the application is clustered, and the cost and complexity of a distributed second-level cache is too high, for a low gain
    • other non-hibernate applications write to the same database, and the cache has thus a big risk of returning stale data, which is not acceptable
    • everything goes very well without a second-level cache, and there is no reason to make the application more complex than it is.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working through the Grails in Action book, and I'm stuck at that part
I'm building a simple Grails app for a web development class. For the most
I have a Groovy/Grails web app that reads e-mails from Gmail. It gets messages
I am to build a web app and I have realized that my domain
How do I set up a variable within a domain class that is not
Part of the install for an app I am responsible for, compiles some C
Part of our java application needs to run javascript that is written by non-developers.
For our grails application, I've written a method that lets you upload a CSV
I am attempting to write integration tests for a Grails service that does, among
My problem is this: I want to create a grails domain instance, defining the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.