I have NHibernate (with NHibernate.Linq and Fluent NHibernate) set up with query caching. Everything works fine until I do a session.Save(new Widget()) (i.e. SQL INSERT). After that point, all queries on that type Widget miss the query cache. Queries on other entity types are cached just fine.
using (ISession session = MySessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
// this INSERT screws things up
var widget = new Widget {Name = "Foo"};
session.Save(widget);
var query = (from w in session.Query<Widget>().Cacheable()
where w.Name == "Bar"
select w);
var fetched1 = query.FirstOrDefault();
var fetched2 = query.FirstOrDefault(); // miss?!
transaction.Commit();
}
}
If I start a new Transaction, the problem persists. If I start a new Session, the problem goes away. This seems kind of strange, since my understanding was the second level cache gets reset per SessionFactory (not Session).
I don't think this matters, but I am using the HashtableCacheProvider, since I'm just testing right now.
var fetched2 = query.FirstOrDefault();) skips the query cache and directly hits the database (which is not the expected behavior). - Joel Verhagensession.Save(...), the query caching works perfectly. - Joel Verhagen