starlight54
starlight54

Reputation: 1091

Why is NHibernate cascading before executing a query?

So I'm using the Nhibernate.Linq API to run a query. The query itself only takes about 50 ms, but the total time spent by NHibernate to return the result is around 500ms.

The query is something like as follows, session.Query<T>().Where(i => i.ForeignKey == someValue).Take(5).ToList().AsQueryable(). And this query executes in less than ~50ms from the debug log of creating the HQLQueryPlan, through setting up params, opening the connection, hydrating the C# objects and finishing.

However, before it even began getting that far, the debug log shows that NHibernate spent ~400ms cascading saves and updates, and then reports a bundle of Collection found: logs which appear to be Collection properties of the objects that I'm asking for. I should add the ISession being used is read only, and has absolutely not modified or created any entity. After spending these 400ms, it then logs that it flushed 0 changes.

Why is NHibernate cascading a load of save/update commands during execution of a .Query<T>(), is it trying to make sure that the correct data is retrieved? Is this the result of some configuration that I have set?

Upvotes: 0

Views: 75

Answers (1)

Roman Artiukhin
Roman Artiukhin

Reputation: 2357

Such behavior is possible for queries executed only inside transaction and when:

  • You have set FlushMode to FlushMode.Always
  • Or NHibernate detected that your query involves tables that are modified in current session. This behaviour can be disabled by setting FlushMode to values less than FlushMode.Auto (that's default) (so it's FlushMode.Commit or FlushMode.Manual)

You can change FlushMode for your session or specify default flush mode in default_flush_mode configuration setting. See spec for details

Upvotes: 1

Related Questions