NHibernate - Retrieving Lots of Data Becomes Exponentially Slow
I have an issue when I retrieve lots of data in NHibernate (such as when producing a report) the page becomes exponentially slower the more data it has to retrieve. I found the following article:
http://nhforge.org/blogs/nhibernate/archive/2008/10/30/bulk-data-operations-with-nhibernate-s-stateless-sessions.aspx
It explains how doing bulk data operations in NHibernate is slow since the first level cache grow开发者_JAVA技巧s too large and how you should use the IStatelessSession instead. The trouble I have is that I don't wish to tie my application to NHibernate so I've added a wrapper around ISession. I then use Linq as my query mechanism but IStatelessSession does not support Linq (it may do in NHibernate 3 but the Linq provider is not stable as it stands at the moment).
I then read that you could do a clear after so many iterations to clear out the first level cache. The problem now is that you can't use lazy loading. The linq provider doesn't allow you to override the mapping defined (or eagerly fetch the additional data) so whenever I grab data which is lazy loaded after I have cleared the session an exception is thrown.
I'm completely lost on what do now. I like the ease of producing reports with linq but the limitations of the inbuilt linq provider in NHibernate seem to be holding me back.
I'd really appreciate it if someone could show me an alternative approach. Thanks
I'm pretty sure that this isn't supported by ICriteria or Linq methods; however, you could consider using hql named queries for this. Setting the "read-only"
attribute on the mapping will prevent the entities from being tracked in the session. Further information can be found here.
As this pertains to your usage, I wouldn't worry about not being able to do everything in the context of linq-to-nhibernate. Although it's great for handling most scenarios, specialized usages like reporting are often better left to methods such as hql or even native sql. Don't be afraid to have a few .GetEntitySummaryReport()
methods on your repositories, it's not the end of the world.
Update:
If adding methods to your repository feels wrong, I'd suggest having a look at using an Enhanced Query Object which provides a great way to encapsulate a specialized query in a provider-agnositc fashion.
精彩评论