-->
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.  [ 11 posts ] 
Author Message
 Post subject: 2nd l. cache usage symptoms...
PostPosted: Sun Jul 01, 2007 3:39 pm 
Regular
Regular

Joined: Fri Feb 18, 2005 3:34 am
Posts: 88
Location: Poland/Wrocław
Hi,

How can I recognize that the query used 2nd l. cache instead of direct DB access? I've begun to suspect that my application does not use that cache...

_________________
Please rate this post if you've found it helpfull
Roland


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 02, 2007 3:22 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hi,

You should check the NHibernate logs. If the second level cache is used, you'll find some similar statements in the log:

Code:
2007-07-02 09:22:33,593 [10] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - loading [User#22]
2007-07-02 09:22:33,593 [10] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - attempting to resolve [User#22]
2007-07-02 09:22:33,593 [10] DEBUG NHibernate.Cache.NonstrictReadWriteCache [(null)] <(null)> - Cache lookup: User#22
2007-07-02 09:22:33,593 [10] DEBUG NHibernate.Cache.NonstrictReadWriteCache [(null)] <(null)> - Cache hit
2007-07-02 09:22:33,593 [10] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - resolved object in second-level cache [User#22]


Note that the second-level cache is only used in a couple of distinct scenario's.

_________________
Please rate this post if it helped.

X.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 2:30 am 
Regular
Regular

Joined: Fri Feb 18, 2005 3:34 am
Posts: 88
Location: Poland/Wrocław
xasp wrote:
Note that the second-level cache is only used in a couple of distinct scenario's.


It looks like some (but just few classes) objects are being taken from the cache. But I was counting that I can cache entire collections of entities but it seems that the collection gets returned via regular query...

What are these scenarios you've mentioned? Where can I find more details? The official documentation does not provide too much details...

_________________
Please rate this post if you've found it helpfull
Roland


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 2:36 am 
Regular
Regular

Joined: Fri Feb 18, 2005 3:34 am
Posts: 88
Location: Poland/Wrocław
rolandz wrote:
xasp wrote:
Note that the second-level cache is only used in a couple of distinct scenario's.


It looks like some (but just few classes) objects are being taken from the cache. But I was counting that I can cache entire collections of entities but it seems that the collection gets returned via regular query...

What are these scenarios you've mentioned? Where can I find more details? The official documentation does not provide too much details...


I've just noticed that it gets objects from session cache instead of 2nd l. c. Does this mean the 2nd c. is not being used at all?

Code:
NHibernate.Impl.SessionImpl: DEBUG NHibernate.Impl.SessionImpl - loading [Employee2#bab]
NHibernate.Type.DateTimeType: DEBUG NHibernate.Type.DateTimeType - returning '2006-09-29' as column: wr2_3_
NHibernate.Impl.SessionImpl: DEBUG NHibernate.Impl.SessionImpl - attempting to resolve [Employee2#bab]
NHibernate.Type.StringType: DEBUG NHibernate.Type.StringType - returning 'bab' as column: wr3_3_
NHibernate.Impl.SessionImpl: DEBUG NHibernate.Impl.SessionImpl - resolved object in session cache [TC.Net.Core.Domain.Employee2#bab]
NHibernate.Type.Int32Type: DEBUG NHibernate.Type.Int32Type - returning '6593' as column: wr4_3_
NHibernate.Type.SingleType: DEBUG NHibernate.Type.SingleType - returning '8' as column: wr5_3_
NHibernate.Impl.SessionImpl: DEBUG NHibernate.Impl.SessionImpl - loading [Task2#6593]
NHibernate.Type.StringType: DEBUG NHibernate.Type.StringType - returning null as column: wr6_3_
NHibernate.Type.SingleType: DEBUG NHibernate.Type.SingleType - returning '0' as column: wr7_3_
NHibernate.Type.StringType: DEBUG NHibernate.Type.StringType - returning null as column: wr8_3_
NHibernate.Type.StringType: DEBUG NHibernate.Type.StringType - returning null as column: wr9_3_
NHibernate.Type.DateTimeType: DEBUG NHibernate.Type.DateTimeType - returning null as column: wr10_3_
NH

_________________
Please rate this post if you've found it helpfull
Roland


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 3:29 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hi,
the second-level cache is only used when querying directly with the id of the object using Get() or Load() (according to the documentation, List() and Enumerable() should also use the second-level cache). So the number of cases you'll manage to hit the second-level cache explicitely is not very high.
In my case, I almost never hit the second level cache explicitely because my id's are not meaningfull and so I only know the id of an object once I retrieved the object based on some other criteria (and bypassing the second-level cache this way).

To me, the main advantage of the second-level cache is for associations that are retrieved implicitely when retrieving an entity. In this case, the system retrieves the main entity, knows the id of the associated entity and can use that id to perform a second-level cache lookup. Note that this requires the association being marked as fetch="select" (if it's marked as fetch="join" the system will again retrieve the data directly from the database undoing the advantage of the second-level cache).

How to test the second-level cache?
1) Create a session and retrieve an entity that is supposed to be stored in the second level cache
2) Close the session
3) Create a new session and retrieve the same entity using Get() and the id. of the entity you retrieved the first time
4) Check the logs, you should notice a second-level cache lookup & hit

Additionally : try retrieving the same entity by using HQL, you'll notice that the second-level cache is not being used, even when you're querying the id-property.

About your second post : there are 2 caches, the session-cache (aka first-level cache) and the second-level cache. The hits you're seeing in the logs are from the session-cache.
In the example from above: if you don't close the session and just re-use it for retrieving the entity the second time, you'll see the object is found in the session cache and NHibernate won't go look in the second-level cache (there's no reason to).

_________________
Please rate this post if it helped.

X.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 10:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Queries are not cached by the second level cache by default, have a look at the documentation here for instructions to cache queries:
http://www.hibernate.org/hib_docs/nhibe ... querycache

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 12:41 pm 
Regular
Regular

Joined: Fri Feb 18, 2005 3:34 am
Posts: 88
Location: Poland/Wrocław
karlchu wrote:
Queries are not cached by the second level cache by default, have a look at the documentation here for instructions to cache queries:
http://www.hibernate.org/hib_docs/nhibe ... querycache


Does it mean that the following configuration is not enough?

Code:
<add key="hibernate.cache.use_query_cache" value="true" />


BTW - shouldn't the entities be cached (in the 2nd l. c.) anyway?

_________________
Please rate this post if you've found it helpfull
Roland


Last edited by rolandz on Tue Jul 03, 2007 12:48 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 12:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Correct. You also need to call IQuery.SetCacheable(true) for the queries that you want to cache.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 12:49 pm 
Regular
Regular

Joined: Fri Feb 18, 2005 3:34 am
Posts: 88
Location: Poland/Wrocław
karlchu wrote:
Correct. You also need to call IQuery.SetCacheable(true) for the queries that you want to cache.


But shouldn't the entities be cached anyway? In the 2nd l. c.?

_________________
Please rate this post if you've found it helpfull
Roland


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 1:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Caching of the query and caching of the entities are somewhat separate. If you call this:
Code:
MySession.CreateQuery("from Person").SetCacheable(true).List();

Assuming this query returned three Persons, four things are cached:
    The IDs of the entities returned by the query (Key: from Person)
    The Person entity with ID 1 (Key: Person#1)
    The Person entity with ID 2 (Key: Person#2)
    The Person entity with ID 3 (Key: Person#3)

When you issue the same query again, the result of the query is retrieved from cache; so the cached IDs are returned (1, 2, and 3). These entities are then returned from cache (assuming these individual entities are still in the second level cache and are not expired).

If the query is not cached, NHibernate will always do a SELECT from the database regardless of whether the individual entities are in cache or not.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 04, 2007 5:12 am 
Regular
Regular

Joined: Fri Feb 18, 2005 3:34 am
Posts: 88
Location: Poland/Wrocław
karlchu wrote:
If the query is not cached, NHibernate will always do a SELECT from the database regardless of whether the individual entities are in cache or not.


Thnx a lot. Telling the queries to cache helped - now I have access time being just a fraction of initial value :) This really increases a look and feel for AJAX callbacks in the paging.

_________________
Please rate this post if you've found it helpfull
Roland


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.