[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-3339) Query cache stops working after object save

Chris Pheby (JIRA) noreply at atlassian.com
Fri Aug 8 18:25:30 EDT 2008


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-3339?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_30815 ] 

Chris Pheby commented on HHH-3339:
----------------------------------

I have encountered this issue also. The reason this occurs is that the timestamp attached to the cache entry is 'session.getTimestamp()'. This means the issue will occur whenever the same query is repeated within the same session.

In Hibernate 3.2.6 the fix is simply to change the line:

Long ts = new Long ( session.getTimestamp() );

to

Long ts = new Long (queryCache.nextTimestamp() );

You can work around this by subclassing StandardQueryCache and providing a QueryCacheFactory implementation that overrides the put method with the above change. You set the Hibernate property 'hibernate.cache.query_cache_factory' to be the name of your custom factory. Note that the documentation at http://www.hibernate.org/hib_docs/reference/en/html/session-configuration.html is not quite right, as it describes the property as being the 'The classname of a custom QueryCache interface, defaults to the built-in StandardQueryCache. eg. classname.of.QueryCache', when it is actually the name of the factory for the query cache.

In the Hibernate code in trunk, the above change does not work due to refactoring of the cache interfaces. I am attaching a patch which should be similar in effect.

> Query cache stops working after object save
> -------------------------------------------
>
>                 Key: HHH-3339
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3339
>             Project: Hibernate3
>          Issue Type: Bug
>          Components: caching (L2)
>    Affects Versions: 3.2.6
>         Environment: Hibernate 3.2.6, MS SQL 2005 Database. Windows XP SP2
>            Reporter: Alex Oleynikov
>         Attachments: query_cache_bug.zip
>
>
> It seems like HB Query cache stops working as soon as given object type is being saved (it does not matter if it was save to existing object or insert of new object instance).
> The code looks like this. Where User is just a primitive POJO with couple of properties.
> // #1
> query = session.createQuery("from User where userID = :id");
> query.setCacheable(true);
> query.setParameter("id", 10);
> user = (User) query.list().get(0);
> // #2
> query = session.createQuery("from User where userID = :id");
> query.setCacheable(true);
> query.setParameter("id", 10);
> user = (User) query.list().get(0);
> // insert new User object
> user = new User();
> user.setUserID((int)(Math.random() * Integer.MAX_VALUE));
> user.setUserName("Ten");
> session.beginTransaction();
> session.save(user);
> session.getTransaction().commit();
> // #3
> query = session.createQuery("from User where userID = :id");
> query.setCacheable(true);
> query.setParameter("id", 10);
> user = (User) query.list().get(0);
> // #4
> query = session.createQuery("from User where userID = :id");
> query.setCacheable(true);
> query.setParameter("id", 10);
> user = (User) query.list().get(0);
> From MS SQL Profiler I can clearly see that no queries is executed for #2 (e.g. it hit the cache), but queries are executed for #3 and #4. From HB debug log I see following:
> For #2 Query (cache is hit) - correct:
> 06:54:25,337 DEBUG StandardQueryCache:102 - checking cached query results in region: org.hibernate.cache.StandardQueryCache
> 06:54:25,337 DEBUG StandardQueryCache:156 - Checking query spaces for up-to-dateness: [USERS]
> 06:54:25,337 DEBUG StandardQueryCache:117 - returning cached query results
> During User insert:
> 06:54:25,368 DEBUG UpdateTimestampsCache:65 - Invalidating space [USERS], timestamp: 4967466866147328
> For #3 Query:
> 06:54:25,368 DEBUG StandardQueryCache:156 - Checking query spaces for up-to-dateness: [USERS]
> 06:54:25,368 DEBUG UpdateTimestampsCache:86 - [USERS] last update timestamp: 4967466866147328, result set timestamp: 4967466865061888
> 06:54:25,368 DEBUG StandardQueryCache:113 - cached query results were not up to date
> For #4 Query (or any subsequent query for this matter):
> 06:54:25,368 DEBUG StandardQueryCache:156 - Checking query spaces for up-to-dateness: [USERS]
> 06:54:25,368 DEBUG UpdateTimestampsCache:86 - [USERS] last update timestamp: 4967466866147328, result set timestamp: 4967466865061888
> 06:54:25,368 DEBUG StandardQueryCache:113 - cached query results were not up to date
> Please note the timestamp numbers for #4 query - even though I would expect query to be re-cached in #3, it still shows old timestamp. So I may think that when re-caching a query it does not update cache entry timestamp which is not up-to-date due to save(). 

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list