Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4345180
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:00:00+00:00 2026-05-21T12:00:00+00:00

what is the best approach to load a collection and update all items with

  • 0

what is the best approach to load a collection and update all items with NHibernate. The current code loads 50 objects and processes each in its own transaction (if 1 fail others are OK).

NH Profiler says that there are too many sql queries per session.

After all, what do you think about this code?

using (var session = sessionFactory.OpenSession())
        {
            var myCollection =
                (from obj in session.Query<MyObject>()
                 select obj).Take(50);

            foreach (var item in myCollection)
            {
                using (var tx = session.BeginTransaction())
                {
                    try
                    {
                        //  Do some stuff...
                        session.Update(item);
                        tx.Commit();
                    }
                    catch (Exception)
                    {
                        tx.Rollback();
                    }
                }
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T12:00:01+00:00Added an answer on May 21, 2026 at 12:00 pm

    From NHibernate: Streaming large result sets :

    NHibernate is meant to be used in an
    OLTP system, as such, it is usually
    used in cases where we want to load a
    relatively small amount of data from
    the database, work with it and save it
    back. For reporting scenarios, there
    are better alternatives, usually (and
    before you ask, any reporting package
    will do. Right tool for the job, etc).

    But there are cases where you want to
    do use NHibernate in reporting
    scenarios nonetheless. Maybe because
    the reporting requirements aren’t
    enough to justify going to a separate
    tool, or because you want to use what
    you already know. It is in those cases
    where you tend to run into problems,
    because you violate the assumptions
    that were made while building
    NHibernate.

    using (IStatelessSession s = sessionFactory.OpenStatelessSession())
    {
        var books = new ActionableList<Book>(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.

    Basically, use a stateless session and batching. Read also: NHibernate Perf Tricks

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best approach to managing NHibernate transaction using Autofac within web application?
What's the best approach to save and load an flowchart on jsPlumb ?
What is the best approach to render a large number of 2D graphical elements
What is a best approach to make a function or set of statements thread
What is the best approach, simple string concatenation or string.format ? For instance, what
What's the best approach (industry standard) when another business needs to upload an XML
What is the best approach to storing a group of global settings for a
How should I best approach drawing a scatter plot diagram in Cocoa?
What is the best approach in using a timer. Use a System.Timer.Timer class or
What is the best approach to do PreferencePage in eclipse?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.