Session.CreateQuery("from Group g where groupnID
<= 5").SetCacheable(true).List<Group>();
Session.CreateQuery("from Group g where groupnID
<= 5").SetCacheable(true).List<Group>();
I'm pretty sure that you can't reproduce what you are saying here.
If you run the two queries above with the cache active you sure will have
only one hit to DB.Not only we have a lot of test about it but even I personally using query cache in a lot of projects. Are you changing something in one of the spaces (Tables) involved in that query ? Perhaps this post may help you to understand what happening behind: http://fabiomaulo.blogspot.com/2009/04/tuning-nhibernate-tolerant-querycache.html 2009/6/2 Max Badrak <[email protected]> > > Sorry, but I am not quite sure what you mean by "NHibernate.Caches" -- > right now my lg4net configuration is just set to DEBUG at the root > level. I tried various things during the day today, and among them I > tried to switch from using Criteria to creating queries explicitly. So > here is a piece of code I am actually executing (this is the only code > that runs, there's nothing else): > > Session.CreateQuery("from Group g where groupnID <= > 5").SetCacheable(true).List<Group>(); > > Session.CreateQuery("from Group g where groupnID <= > 5").SetCacheable(true).List<Group>(); > > This produces the log below. It seems like switching to explicit > queries does, at least, make NH look in the cache. However, it still > runs two DB queries (I can see the queries in the SQL Profiler), and > neither the query itself, nor the query's results (entity IDs) are > cached in the second level cache -- only the actual entities are > (which I verified by dumping the entire cache). If the query cache is > tied to the NH session, it's kind of useless for us -- our app needs > the query results cached across multiple HTTP requests... > > opened session > unable to locate HQL query plan in cache; generating (from Group g > where groupnID <= 5) > compiling query > HQL param location recognition took 0 mills (from Group g where > groupnID <= 5) > located HQL query plan in cache (from Group g where groupnID <= 5) > find: from Group g where groupnID <= 5 > named parameters: {} > HQL: from Domain.Group g where groupnID <= 5 > SQL: select group0_.GroupNID as GroupNID2_, group0_.GroupStatusID as > GroupSta2_2_, group0_.Name as Name2_, group0_.Address1 as Address4_2_, > group0_.Address2 as Address5_2_, group0_.City as City2_, group0_.Zip > as Zip2_, group0_.Phone as Phone2_, group0_.Fax as Fax2_, > group0_.ContactPerson as Contact10_2_, group0_.Comments as Comments2_, > group0_.LastModifiedDTM as LastMod12_2_, group0_.GoLiveDate as > GoLiveDate2_, group0_.be_OID as be14_2_, group0_.StateID as StateID2_ > from tblGroup group0_ where (groupnID<=5 ) > Opened new IDbCommand, open IDbCommands: 1 > Building an IDbCommand object for the SqlString: select > group0_.GroupNID as GroupNID2_, group0_.GroupStatusID as GroupSta2_2_, > group0_.Name as Name2_, group0_.Address1 as Address4_2_, > group0_.Address2 as Address5_2_, group0_.City as City2_, group0_.Zip > as Zip2_, group0_.Phone as Phone2_, group0_.Fax as Fax2_, > group0_.ContactPerson as Contact10_2_, group0_.Comments as Comments2_, > group0_.LastModifiedDTM as LastMod12_2_, group0_.GoLiveDate as > GoLiveDate2_, group0_.be_OID as be14_2_, group0_.StateID as StateID2_ > from tblGroup group0_ where (groupnID<=5 ) > select group0_.GroupNID as GroupNID2_, group0_.GroupStatusID as > GroupSta2_2_, group0_.Name as Name2_, group0_.Address1 as Address4_2_, > group0_.Address2 as Address5_2_, group0_.City as City2_, group0_.Zip > as Zip2_, group0_.Phone as Phone2_, group0_.Fax as Fax2_, > group0_.ContactPerson as Contact10_2_, group0_.Comments as Comments2_, > group0_.LastModifiedDTM as LastMod12_2_, group0_.GoLiveDate as > GoLiveDate2_, group0_.be_OID as be14_2_, group0_.StateID as StateID2_ > from tblGroup group0_ where (groupnID<=5 ) > select group0_.GroupNID as GroupNID2_, group0_.GroupStatusID as > GroupSta2_2_, group0_.Name as Name2_, group0_.Address1 as Address4_2_, > group0_.Address2 as Address5_2_, group0_.City as City2_, group0_.Zip > as Zip2_, group0_.Phone as Phone2_, group0_.Fax as Fax2_, > group0_.ContactPerson as Contact10_2_, group0_.Comments as Comments2_, > group0_.LastModifiedDTM as LastMod12_2_, group0_.GoLiveDate as > GoLiveDate2_, group0_.be_OID as be14_2_, group0_.StateID as StateID2_ > from tblGroup group0_ where (groupnID<=5 ) > Obtaining IDbConnection from Driver > Opened IDataReader, open IDataReaders: 1 > processing result set > result set row: 0 > returning '1' as column: GroupNID2_ > result row: EntityKey[Domain.Group#1] > Initializing object from DataReader: [Domain.Group#1] > Hydrating entity: [Domain.Group#1] > returning <bunch of junk removed> > result set row: 1 > <rows 1 through 4 removed> > done processing result set (5 rows) > running NHybridDataReader.Dispose() > Closed IDataReader, open IDataReaders :0 > Closed IDbCommand, open IDbCommands: 0 > aggressively releasing database connection > Closing connection > total objects hydrated: 5 > resolving associations for [Domain.Group#1] > loading entity: [Domain.State#MA] > creating new proxy for entity > creating collection wrapper:[Domain.Group.Offices#1] > adding entity to second-level cache: [Domain.Group#1] > Caching: Domain.Group#1 > Fetching object 'NHibernate-Cache:Domain.Group:Domain.Group#1' from > the cache. > Item was already cached: Domain.Group#1 > done materializing entity [Domain.Group#1] > resolving associations for [Domain.Group#2] > loading entity: [Domain.State#MA] > entity proxy found in session cache > creating collection wrapper:[Domain.Group.Offices#2] > adding entity to second-level cache: [Domain.Group#2] > Caching: Domain.Group#2 > Fetching object 'NHibernate-Cache:Domain.Group:Domain.Group#2' from > the cache. > Item was already cached: Domain.Group#2 > done materializing entity [Domain.Group#2] > resolving associations for [Domain.Group#3] > loading entity: [Domain.State#MA] > entity proxy found in session cache > creating collection wrapper:[Domain.Group.Offices#3] > adding entity to second-level cache: [Domain.Group#3] > Caching: Domain.Group#3 > Fetching object 'NHibernate-Cache:Domain.Group:Domain.Group#3' from > the cache. > Item was already cached: Domain.Group#3 > done materializing entity [Domain.Group#3] > resolving associations for [Domain.Group#4] > loading entity: [Domain.State#MA] > entity proxy found in session cache > creating collection wrapper:[Domain.Group.Offices#4] > adding entity to second-level cache: [Domain.Group#4] > Caching: Domain.Group#4 > Fetching object 'NHibernate-Cache:Domain.Group:Domain.Group#4' from > the cache. > Item was already cached: Domain.Group#4 > done materializing entity [Domain.Group#4] > resolving associations for [Domain.Group#5] > loading entity: [Domain.State#MA] > entity proxy found in session cache > creating collection wrapper:[Domain.Group.Offices#5] > adding entity to second-level cache: [Domain.Group#5] > Caching: Domain.Group#5 > Fetching object 'NHibernate-Cache:Domain.Group:Domain.Group#5' from > the cache. > Item was already cached: Domain.Group#5 > done materializing entity [Domain.Group#5] > initializing non-lazy collections > after autocommit > transaction completion > aggressively releasing database connection > located HQL query plan in cache (from Group g where groupnID <= 5) > located HQL query plan in cache (from Group g where groupnID <= 5) > find: from Group g where groupnID <= 5 > named parameters: {} > HQL: from Domain.Group g where groupnID <= 5 > SQL: select group0_.GroupNID as GroupNID2_, group0_.GroupStatusID as > GroupSta2_2_, group0_.Name as Name2_, group0_.Address1 as Address4_2_, > group0_.Address2 as Address5_2_, group0_.City as City2_, group0_.Zip > as Zip2_, group0_.Phone as Phone2_, group0_.Fax as Fax2_, > group0_.ContactPerson as Contact10_2_, group0_.Comments as Comments2_, > group0_.LastModifiedDTM as LastMod12_2_, group0_.GoLiveDate as > GoLiveDate2_, group0_.be_OID as be14_2_, group0_.StateID as StateID2_ > from tblGroup group0_ where (groupnID<=5 ) > Opened new IDbCommand, open IDbCommands: 1 > Building an IDbCommand object for the SqlString: select > group0_.GroupNID as GroupNID2_, group0_.GroupStatusID as GroupSta2_2_, > group0_.Name as Name2_, group0_.Address1 as Address4_2_, > group0_.Address2 as Address5_2_, group0_.City as City2_, group0_.Zip > as Zip2_, group0_.Phone as Phone2_, group0_.Fax as Fax2_, > group0_.ContactPerson as Contact10_2_, group0_.Comments as Comments2_, > group0_.LastModifiedDTM as LastMod12_2_, group0_.GoLiveDate as > GoLiveDate2_, group0_.be_OID as be14_2_, group0_.StateID as StateID2_ > from tblGroup group0_ where (groupnID<=5 ) > select group0_.GroupNID as GroupNID2_, group0_.GroupStatusID as > GroupSta2_2_, group0_.Name as Name2_, group0_.Address1 as Address4_2_, > group0_.Address2 as Address5_2_, group0_.City as City2_, group0_.Zip > as Zip2_, group0_.Phone as Phone2_, group0_.Fax as Fax2_, > group0_.ContactPerson as Contact10_2_, group0_.Comments as Comments2_, > group0_.LastModifiedDTM as LastMod12_2_, group0_.GoLiveDate as > GoLiveDate2_, group0_.be_OID as be14_2_, group0_.StateID as StateID2_ > from tblGroup group0_ where (groupnID<=5 ) > select group0_.GroupNID as GroupNID2_, group0_.GroupStatusID as > GroupSta2_2_, group0_.Name as Name2_, group0_.Address1 as Address4_2_, > group0_.Address2 as Address5_2_, group0_.City as City2_, group0_.Zip > as Zip2_, group0_.Phone as Phone2_, group0_.Fax as Fax2_, > group0_.ContactPerson as Contact10_2_, group0_.Comments as Comments2_, > group0_.LastModifiedDTM as LastMod12_2_, group0_.GoLiveDate as > GoLiveDate2_, group0_.be_OID as be14_2_, group0_.StateID as StateID2_ > from tblGroup group0_ where (groupnID<=5 ) > Obtaining IDbConnection from Driver > Opened IDataReader, open IDataReaders: 1 > processing result set > result set row: 0 > returning '1' as column: GroupNID2_ > result row: EntityKey[Domain.Group#1] > result set row: 1 > returning '2' as column: GroupNID2_ > result row: EntityKey[Domain.Group#2] > result set row: 2 > returning '3' as column: GroupNID2_ > result row: EntityKey[Domain.Group#3] > result set row: 3 > returning '4' as column: GroupNID2_ > result row: EntityKey[Domain.Group#4] > result set row: 4 > returning '5' as column: GroupNID2_ > result row: EntityKey[Domain.Group#5] > done processing result set (5 rows) > running NHybridDataReader.Dispose() > Closed IDataReader, open IDataReaders :0 > Closed IDbCommand, open IDbCommands: 0 > aggressively releasing database connection > Closing connection > total objects hydrated: 0 > initializing non-lazy collections > after autocommit > transaction completion > aggressively releasing database connection > > > On Jun 2, 10:57 am, Jozef Sevcik <[email protected]> wrote: > > can you post output from log4net from NHibernate.Caches (and NCache > > provider if possible) > > what's in log when you are trying to fetch cached objects again ? > > > > 2009/6/2 Max Badrak <[email protected]>: > > > > > > > > > > > > > Can you please be a bit more specific? I did read this post, as well > > > as others written by Ayende on this subject, as well as relevant > > > chapters of "Nhibernate in Action", but still apparently don't > > > understand something... > > > > > On Jun 2, 9:00 am, Fabio Maulo <[email protected]> wrote: > > >>http://ayende.com/Blog/archive/2009/04/24/nhibernate-2nd-level-cache.. > .. > > >> 2009/6/2 Max Badrak <[email protected]> > > > > >> > I have a piece of code that, in essense, does: > > > > >> > Session.CreateCriteria(typeof (TDomainObject)).SetCacheable > > >> > (true).List<TDomainObject>(); > > > > >> > Where Session is NH session, of course. The second level cache is > > >> > configured and turned on. hibernate.cache.use_query_cache is set to > > >> > true. The TDomainObject has caching enabled in the mapping file. > When > > >> > I run the program with log level cranked up to DEBUG, I can see NH > > >> > putting data into the cache, and I can see the objects in my second > > >> > level cache (to be specific, I am using NCache -- an out of proc, > > >> > cluster-level cache). The problem is that when the program is run > > >> > repeatedly, it always hits the database, regardless of whether the > > >> > results are already in the cache or not. If I run > Session.Load(typeof > > >> > (TDomainObject), 1) when there's something in the cache -- it works > as > > >> > expected -- i.e. NH checks the cache, and returns the object out of > > >> > memory. > > > > >> > Am I missing something trivial, or is the scenario above just not > > >> > supported? What would I need to do to make NH hit the cache? Create > an > > >> > explicit query in xml, enable caching for it, and use it instead > > >> > of .List()? > > > > >> -- > > >> Fabio Maulo > > > > -- > > Jozef > > > -- Fabio Maulo --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nhusers?hl=en -~----------~----~----~----~------~----~------~--~---
