Yunus Evren (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
) *updated* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiZTJkODIyMDY3...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16594?atlOrigin=eyJpIjoiZTJkOD...
) HHH-16594 (
https://hibernate.atlassian.net/browse/HHH-16594?atlOrigin=eyJpIjoiZTJkOD...
) Query cache miss when query contains multiple parameters (
https://hibernate.atlassian.net/browse/HHH-16594?atlOrigin=eyJpIjoiZTJkOD...
)
Change By: Yunus Evren (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
)
When query caching is enabled on a query that contains multiple query parameters,
subsequent queries with the exact same parameters run into cache misses. This seems to be
caused because {{QueryKey.ParameterBindingsMementoImpl}} is constructed with a random
order of parameter bindings in each different transaction. See
{{generateQueryKeyMemento()}} method:
[
https://github.com/hibernate/hibernate-orm/blob/6.2.2/hibernate-core/src/...]
{{parameterBindingMap}} is a {{ConcurrentHashMap}} that will have an unpredictable order
in each session. This method iterates over the values of this map and alters the
{{hashCode}} of the cache key on each {{addToCacheKey()}} call. Different order of
parameters result in {{ParameterBindingsMemento}} objects with different {{hashCode}}
values even though they consist of exact identical parameters. This eventually results in
a cache miss because the cache key object ({{QueryKey}}) equals method will return false
when comparing the inner ParameterBindingsMemento objects that have different
{{hashCode}}s:
[
https://github.com/hibernate/hibernate-orm/blob/6.2.2/hibernate-core/src/...]
The issue does not occur when query contains only 1 parameter because the order will be
unchanged. If the query contains 2 parameters, then you have 50% chance for miss. If it
has more parameters the possibility to miss increases exponentially. This is impacting the
performance of the application quite severely because queries that have e.g. 7 parameters
have 1/5040 (7 factorial) chance to do a cache hit the second time. So in practice, query
cache is more or less not working at all for these queries.
Reproducer test case to be added , work in progress….
(
https://hibernate.atlassian.net/browse/HHH-16594#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16594#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100225- sha1:84d3b45 )