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
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();
}
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.