Criteria query doesn't use 1st level cache

46 views
Skip to first unread message

Ili

unread,
Jul 28, 2010, 12:00:40 PM7/28/10
to nhusers
For some reason queries created using the Criteria api dont seem to
use the session cache and I am not sure if it is by design or if I am
missing something

If I have these 2 statements in my code, 2 queries are run on the
database -
var countryFromCriteria =
session.CreateCriteria<Country>().Add(Expression.Eq("CountryId",
43)).SetCacheable(true).UniqueResult();
var countryFromCriteria1 =
session.CreateCriteria<Country>().Add(Expression.Eq("CountryId",
43)).SetCacheable(true).UniqueResult();

However, if I replace the 2nd query by a session.Get like this -

var countryFromCriteria =
session.CreateCriteria<Country>().Add(Expression.Eq("CountryId",
43)).SetCacheable(true).UniqueResult();
var country = session.Get<Country>(43);
only 1 database query is run and the Get uses the cached results.

Also, setting the CacheMode for the query doesnt seem to have any
effect.

Thanks,
Ili

Fabio Maulo

unread,
Jul 28, 2010, 12:07:33 PM7/28/10
to nhu...@googlegroups.com
What should do NH ?


--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.




--
Fabio Maulo

Fabio Maulo

unread,
Jul 28, 2010, 1:14:45 PM7/28/10
to nhu...@googlegroups.com
In short...
When you run a query NH should translate the query, understand that you are querying just one entity by its ID, then look in the session if there is that entity instance, then go to DB if the instance is not there.

To query an instance by its ID NH gives you 2 specific methods (Get<T> Load<T>) in this way NH does not have to waste time translating a query to understand what you are querying with it.

The only way to avoid the hit to DB using a query is using the Cache (aka second-level-cache) but, as you probably know, is not the same of session-cache (aka first-level-cache aka persistence-context).

On Wed, Jul 28, 2010 at 1:00 PM, Ili <iliasa...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.




--
Fabio Maulo

Jason Meckley

unread,
Jul 28, 2010, 1:40:01 PM7/28/10
to nhusers
what you see is by design. criteria is designed to pull back a set of
results (even if the set is a single instance) while Get and Load are
designed to pull back a single instance of an entity. Get and Load
take advantage of the 1st level cache because the id of the entity is
known, criteria does not have the identity so it cannot look up the
entity against the 1st level cache.

CacheMode is part of 2nd level cache. that is a whole different system
than 1st level cache. it requires a fair amount of configuration, both
on the session factory, the criteria, and the caching implementation.
if not configured properly you can actually hurt preformance rather
than enhance it. I'll spare the details of who 2nd level cache works,
sufficed to say it's not a feature you just implement without serious
thought to how you want to apply the cache.

personally i only use 2nd level cache as a very last resort. there are
other ways of boosting preformance without in-memory cache.
for more information on the topic read the NH docs on 2nd level cache.
> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
> > .

Ili

unread,
Jul 29, 2010, 4:18:27 AM7/29/10
to nhusers
Understood.Thanks for the clarifications guys.

-Ili
Reply all
Reply to author
Forward
0 new messages