nHibernate performance issue when loading large collections

后端 未结 3 1223
没有蜡笔的小新
没有蜡笔的小新 2020-12-12 02:37

(just to make clear: my app isn\'t really about employees and departments. I just use these terms for example\'s sake).
Each department has an employees collection, whic

3条回答
  •  庸人自扰
    2020-12-12 03:18

    I would use a StatelessSession and batch optimization.

    The session will keep track of all the loaded objects, and if we load a lot of data, it will eventually blow out with an out of memory exception. Luckily, NHibernate has a ready made solution for this, the stateless session. The code now looks like this:

    using (IStatelessSession s = sessionFactory.OpenStatelessSession())
    {
        var books = new ActionableList(book => Console.WriteLine(book.Name));
        s.CreateQuery("from Book")
            .List(books);
    
    }
    

    The stateless session, unlike the normal NHibernate session, doesn’t keep track of loaded objects, so the code here and the data reader code are essentially the same thing.

    For batch optimization and more: NHibernate performance tricks.

提交回复
热议问题