NHibernate makes available a number of statistics about its work; this includes, among others:
- All of the queries executed;
- Number of entities loaded, inserted, updated and deleted;
- Number of optimistic concurrency misses;
- Number of second level cache hits and misses;
- Number of transactions started and committed;
- Number of connections opened and closed;
This is available per session factory:
var statistics = sessionFactory.Statistics;
And all of these settings can be filtered per entity:
var entityStatistics = sessionFactory.Statistics.GetEntityStatistics("MyEntity");
Statistics can help us diagnose, for example, second level or query cache issues.
If we have no need for them, we can disable them before building the session factory:
Or at runtime:
sessionFactory.Statistics.IsStatisticsEnabled = false;
An advice: do switch it off while on production, it does have an effect on performance.