-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Not able to cache queries - only entities?
PostPosted: Sat Feb 27, 2010 2:40 pm 
Newbie

Joined: Sat Mar 07, 2009 2:24 am
Posts: 9
Hi all

I have come across a problem and don't believe this is by design. I am
unable to get nhibernate to use the cache when using a query like:

Code:
var acc = session.CreateQuery("from Account").SetCacheable(true).List();


I have set the following in the config:
Code:
<property name="cache.provider_class">NHibernate.Cache.HashtableCacheProvider</property>
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache" >true</property>


My test:
Code:
[Test]
        public void Can_Cache_Query()
        {
            Console.WriteLine("1st query");
            using (var session = SessionFactory.OpenSession())
            {
                var acc = session.CreateQuery("from Account").SetCacheable(true).List();
                acc.ShouldNotBeNull();
            }
            Console.WriteLine("2nd query");
            using (var session = SessionFactory.OpenSession())
            {
                var acc = session.CreateQuery("from Account").SetCacheable(true).List();
                acc.ShouldNotBeNull();
            }
        }


unless I use session.Get, which works fine, the query hits the db
instead of the cache. This is ok:

Code:
[Test]
        public void account_should_be_in_second_level_cache()
        {
            using (var session = SessionFactory.OpenSession())
            {
                Console.WriteLine("--> Now loading account");
                var acc = session.Get<Account>(newAccount.Id);
                acc.ShouldNotBeNull();
                acc.Name.ShouldEqual(newAccount.Name);
            }
        }


Going nuts here


Top
 Profile  
 
 Post subject: Re: Not able to cache queries - only entities?
PostPosted: Mon Mar 01, 2010 3:29 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
1. Which 2L-Cache implementation are you using?

2. Did you define class Account to be cached in 2L ?
Be aware that the 2L-query-cache only caches the primary keys of the result set,
premising that regarding entities itself are already cached in 2L cache.
So ii is very important to annotate regarding classes with the @Cache tag

3. When did you create the last Account instance?
If you do that within transaction then the cache-lookup may fail due the updateTimeStamp check in the TimeStampsRegion.
For investing in this direction enable following logs:
log4j.logger.org.hibernate.cache.StandardQueryCache=DEBUG
log4j.logger.org.hibernate.cache.UpdateTimestampsCache=DEBUG


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.