Try fast search NHibernate

22 March 2011

NHibernate 3.2 batching improvement

This post is about an old missed feature in NHibernate batching. I have mentioned this feature in this post but again I forgot it. How old is it ? what about 2007-09-15 ? yes, so old.

The domain

InsertOrdering

The little test

using (ISession s = OpenSession())
using (s.BeginTransaction())
{
    for (int i = 0; i < 12; i++)
    {
        var user = new User {UserName = "user-" + i};
        var group = new Group {Name = "group-" + i};
        s.Save(user);
        s.Save(group);
        user.AddMembership(group);
    }
    s.Transaction.Commit();
}

 

The behavior

With NHibernate 3.1, and before, giving a batch-size set to 10 we have 26 roundtrips because, in practice, due to cascade the batcher is used only to insert memberships; we have 12 roundtrips to store users, 12 roundtrips to store groups and only 2 roundtrips to store memberships.

With NHibernate 3.2, and above, giving a batch-size set to 10 we have 6 roundtrips; 2 roundtrips to store the 12 users, 2 roundtrips to store the 12 groups and 2 roundtrips to store the 12 memberships.

Starting with NHibernate 3.2 the new behavior is by default.

6 comments:

  1. This is awsome. When is your plan release of NH 3.2?

    ReplyDelete
  2. I've always had a doubt with NHibernate, is valid to use a code like this:

    Session s = OpenSession();
    for (int i = 0; i < 12; i++) {
    var user = new User {UserName = "user-" + i};
    var group = new Group {Name = "group-" + i};
    s.Save(user);
    s.Save(group);
    user.AddMembership(group);
    }
    s.BeginTransaction();
    s.Transaction.Commit();

    ReplyDelete
  3. With FlushMode=Auto, no it isn't.

    ReplyDelete
  4. NH always rock, now, NH 3.2 is a Tsunami of new features!!!!!!

    ReplyDelete
  5. Very nice work Fabio. Would it be possible to add the feature of returning the generated primary keys using the 'returning into ...' feature of the oracle dialect (AddIdentifierOutParameterToInsert) for the OracleDataClientDriver? This would make it possible to use batching with sequences as primary keys and return the primary keys in a single database round trip.

    ReplyDelete
  6. Its really a great feature and all other features of NH3.2 awesome! I am still looking for some features related to DB backup and restore features [can be make NH more robust] OR I am not aware if these are already there :)

    ReplyDelete