Repository navigation
fix(hibernate5): a criteria's cache(false) is no longer overridden by the mapping in list(map) { } - #17014
fix(hibernate5): a criteria's cache(false) is no longer overridden by the mapping in list(map) { }#17014borinquenkid wants to merge 6 commits into
Conversation
… the mapping in list(map) { }
list(map) { } builds a paged result list, and populating it re-applied the mapping's cache default over a cache(false) the closure had set. The criteria's own cacheable state is now handed on when the caller gave no cache argument.
Fixes #14472
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. cache(false) is still overridden when the arguments sort through an association whose mapping caches queries.
…ther the query is cached A sort argument through an association (sort: 'author.name') created a sub-criteria and applied the cache mapping of the associated entity to it. A sub-criteria shares the setting of its root, so this ran after the root had taken the cache argument or the cache() call, and turned caching back on over an explicit false. The cache setting of a query now comes from its own entity, the cache argument and the cache call only; the entity that is sorted through takes no part. QueryCacheOptionSpec gains rows for a criteria list, a static list and a dynamic finder sorted through a cached association, with cache(false), with cache: false, with cache(true) and with no setting, on a cached and an uncached mapping, and the total count of a sorted criteria list with cache(false). Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Thank you. That was a second way to the same override: a sort argument through an association ( The cache setting of a query now comes from its own entity, the One behaviour changes beyond your case, and I want it stated: before, sorting through a cached association could switch caching on for a root whose own mapping does not cache. It no longer does. A root that wants the cache says so through its mapping, the argument or
|
jamesfredley
left a comment
There was a problem hiding this comment.
The items this review found have been addressed. cache(false) stays off when the arguments sort through an association, and a criteria that does not set cache still uses the mapping.
jdaugherty
left a comment
There was a problem hiding this comment.
The fix does what #14472 asks: list(map) { cache(false) } is no longer put in the query cache, also when the closure calls setCacheable(false) directly, and lock: true still keeps the query out of the cache. #14458 does not reproduce on 7.0.x for me either.
Requested changes, details inline:
- an upgrade guide section for the queries whose caching changes
- test rows that reach the change in
GrailsHibernateQueryUtils - two statements in
caching.adocthat do not match the behaviour
| Class<?> propertyTargetClass = ((Association) property).getAssociatedEntity().getJavaClass(); | ||
| GrailsHibernateUtil.cacheCriteriaByMapping(datastore, propertyTargetClass, subCriteria); | ||
| // the cache setting of a query comes from its own entity, the cache argument and the cache call: the mapping of an | ||
| // entity that is only sorted through is no part of it, and a sub-criteria shares the setting of its root |
There was a problem hiding this comment.
This PR changes what existing queries do, so it needs a section in the upgrade guide (grails-doc/src/en/guide/upgrading/upgrading60x.adoc, next free number is 22). Comparing this branch with 7.0.x:
| Query | 7.0.x | This PR |
|---|---|---|
createCriteria().list(map) { cache(false) } on an entity whose mapping caches |
cached | not cached |
entity whose mapping does not cache, createCriteria().list(max: 2, sort: 'author.name') { }, where author is an entity whose mapping caches |
cached | not cached |
the same entity with findWhere / findAllWhere / find(example, args) / findAll(example, args) and sort: 'author.name' |
cached | not cached |
createCriteria().list(max: 2, sort: 'author.name', lock: true) { } on an entity whose mapping caches |
cached | not cached |
The second and third rows are more than #14472 asks for: an application can be relying on those queries being cached today. Please list each change with what happened before, what happens now, and how to keep the old result (cache: true in the arguments).
| Class<?> propertyTargetClass = associatedEntity.getJavaClass(); | ||
| cacheCriteriaByMapping(propertyTargetClass, subCriteria); | ||
| // the cache setting of a query comes from its own entity, the cache argument and the cache call: the mapping of an | ||
| // entity that is only sorted through is no part of it, and a sub-criteria shares the setting of its root |
There was a problem hiding this comment.
No row of QueryCacheOptionSpec reaches this change. This helper is only called for findWhere, findAllWhere, find(example, args) and findAll(example, args). The "static list" and "dynamic finder" sorted rows go through other code (list builds a JPA CriteriaQuery, the finder a HibernateQuery) and pass on 7.0.x as it is.
With this file reverted, QcBook.findAllWhere([title: 'b1'], [sort: 'author.name', cache: false]) is still put in the query cache, and so is QcNote.findAllWhere([title: 'b1'], [sort: 'author.name']). Please add rows of that shape so the change here is covered.
| } | ||
| ---- | ||
|
|
||
| An entity whose mapping enables the cache has its queries cached by default. The `cache` argument and the `cache` call inside a criteria take precedence over that default, in either direction, so `cache false` (or `cache: false`) keeps one query out of the query cache, and the total count of a paged list follows it: |
There was a problem hiding this comment.
Two statements in this sentence do not match what the code does:
- "An entity whose mapping enables the cache has its queries cached by default" is too broad. On such an entity, criteria queries,
list, dynamic finders and where queries are cached by default, butcount(),executeQuery('from ...')andfindAll('from ...')put nothing in the query cache. Please name the kinds of query the default applies to. - "the total count of a paged list follows it" holds only for
false. When the list is cached, its total count is still not cached (a cachedlist(max: 2)followed bytotalCountgives one put, not two), which the PR description lists under "Left out". Please say what happens instead: the total count is not put in the query cache. The class comment ofQueryCacheOptionSpec("the total count of a paged list follows the list") has the same wording.
It would also help to say here which one wins when both are given: list(max: 2, cache: true) { cache(false) } is cached, so the argument takes precedence over the call. A row in the spec for each direction would pin that.
…bernateQueryUtils, caching.adoc says what is cached, and the upgrade guide lists the queries that change QueryCacheOptionSpec has rows for findAllWhere, findWhere, find(example, args) and findAll(example, args) sorted through a cached association, a criteria list with lock and a sort, and the precedence of the cache argument over cache() inside the closure. 13 rows fail without the change. caching.adoc names the ways a query is cached (the mapping, cache: true, grails.hibernate.cache.queries), says that an explicit false or true takes precedence in either direction and the argument wins over the call, and that the total count of a paged list is not put in the query cache. The upgrade guide has a section with a table of the queries that were cached against the cache option or because of the entity they sort through, and how to keep the old result. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
The new commit covers the requested upgrade section, the spec rows and the two corrections to
If a shape does not reproduce first time, a failing spec on a throwaway branch, or the few lines you ran, would help me a lot. |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A criteria closure that turns the cache off is ignored when the arguments turn it on.
| protected List createPagedResultList(Map args) { | ||
| GrailsHibernateUtil.populateArgumentsForCriteria(datastore, targetClass, criteria, args, conversionService); | ||
| Map populateArgs = args; | ||
| if (!args.containsKey(GrailsHibernateUtil.ARGUMENT_CACHE)) { |
There was a problem hiding this comment.
The arguments ask to cache the query, and the closure turns the cache off. The query is cached. The closure asked not to cache it. Keep the cache off.
…ache argument, in both directions Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley: @jdaugherty: this reverses what the spec pinned after your review (the argument won). James asked for the closure to win; if you prefer the other direction, it is the two rows of the spec and one sentence of Before asking for another look, I ran every combination of the mapping (cached or not), the argument (none, true, false) and the closure (nothing, |
# Conflicts: # grails-doc/src/en/guide/upgrading/upgrading60x.adoc
jdaugherty
left a comment
There was a problem hiding this comment.
#17070 (part 10 of the #16853 split) rewrites GrailsHibernateUtil.addOrderPossiblyNested: it walks the sort path itself and still calls cacheCriteriaByMapping(datastore, currentClass, criteria) for each association it joins for the sort (at e431f96). The call that this pull request removes from that method moves there, so whichever of the two lands second has to drop it in its new place too; otherwise Team.createCriteria().list(max: 10, sort: 'club.name') { cache(false) } is still made cacheable when the mapping of Club enables the cache. The call this pull request removes from GrailsHibernateQueryUtils.addOrderPossiblyNested is still in place in #17071 and #17072, but #17072 reworks the code around it.
…e-option-7.0.x # Conflicts: # grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/query/GrailsHibernateQueryUtils.java # grails-doc/src/en/guide/upgrading/upgrading60x.adoc
🔎 No tests executed 🔎🏷️ Commit: 79b3d19 Learn more about TestLens at testlens.app/docs. |
|
@jdaugherty Agreed. #17066, which this waited for, has merged, and the description now says so. This stays a draft while #17070 is open. If it lands second, I will remove the |
#17066, which this pull request waited for, has merged. It stays a draft while #17070 is open: #17070 moves a call that this pull request removes, so whichever of the two lands second drops it in its new place.
Entity.createCriteria().list([:]) { cache(false) }on an entity whose mapping enables the cache still put the query in the query cache. Alistwith a map builds a paged result list and populates it after the closure has run, and with nocacheargument that re-applied the mapping's default over thecache(false)the closure had set (GrailsHibernateUtil.populateArgumentsForCriteria, as the issue says). The criteria already holds the mapping's default, set when it was created, soHibernateCriteriaBuilder.createPagedResultListnow hands the criteria's own cacheable state on when the caller gave nocacheargument. Thecacheargument still wins when it is there.Spec:
QueryCacheOptionSpecreads Hibernate's query cache statistics (puts) for 31 rows: what asks for caching (the mapping, thecacheargument, thecachecall in a criteria) x how the query is run (criterialist,list(map),count, staticlist, dynamic finder, where query) x the globalgrails.hibernate.cache.queries, plus the total count of a paged list for the same forms. Three rows fail without the change, all of thelist(map) { cache(false) }shape.#14458 (the total count ignores
cache: false) does not reproduce: the total count puts nothing in the cache when the list is not cached, for criteria, static list and where queries, with and without the global switch. The rows pin it.Left out: the total count of a paged list is not cached when its list is (it is a separate criteria that takes nothing from the first), and
grails.hibernate.cache.queriesdoes not make a criteria list cacheable on an uncached mapping; neither is what the two issues ask. Hibernate 7 has its own criteria builder; the same table goes on 8.0.x with the merge-up.The caching guide says the
cacheargument and thecachecall take precedence over the mapping, in either direction.Full Hibernate 5 suite on this branch: 718 tests, 0 failures;
codeStyleclean.Fixes #14472
Refs #14458
https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL