Domanda

I have a function which gets called many times in one session. To my understanding with NHibernate first level cache, exact query in the same session will execute only once irrespective of the number of calls.

Such is not the case though. Below is the snippet and I can see the query being executed in NHProfiler as many times the function is called.

    public List<CustomerType> GetAllActiveCustomerTypes()
    {
        return _unitOfWork.CurrentSession.QueryOver<CustomerType>().Where(x => x.Active).List();
    }

Am I missing something in understanding NHibernate here?

Thanks

È stato utile?

Soluzione

Your experience is correct. Queries are not cached by a sessions first-level cache. To extend the understanding read this:First and Second Level caching in NHibernate

The extract of the first-level cache:

...The first level cache is also called the identity map and is used not only to reduce the number of round trips to the database to improve the speed of an application but also to guarantee that there do not exist two distinct instances of an object having the very same id...

and

...When using NHibernate the first level cache is automatically enabled as long as one uses the standard session object... When NHibernate is loading an entity by its unique id from the database then it is automatically put into the so called identity map...

As said, it serves for entity caching purposes.

The Second level cache extract:

  • ... can cache individual entities or whole aggregates in the 2nd level cache.
  • ... can also cache (complex and/or time consuming) queries in the 2nd level cache.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top