0

Example 1, the following insert results in 2 batches regardless of your configuration:

INSERT INTO <b>entitytable1</b> (someInt) VALUES (1)
INSERT INTO <b>entitytable2</b> (someInt) VALUES (3)
-- Notice tables are <b>different</b>
-- Results in <b>2</b> round trips to the db
-- C# code that can generate such statements:
   session.Save(typeof(entitytable<b>1</b>), new entitytable<b>1</b>{someInt = 1})
   session.Save(typeof(entitytable<b>2</b>), new entitytable<b>2</b>{someInt = 3})
   transction.Commit();

However, in example 2, below, the inserts result in 1 batch:

INSERT INTO <b>entitytable1</b> (someInt) VALUES (1)
INSERT INTO <b>entitytable1</b> (someInt) VALUES (3)
-- Notice tables are the <b>same</b>
-- Results in <b>1</b> round trip to the db
-- C# code that can generate such statements:
   session.Save(typeof(entitytable<b>1</b>), new entitytable<b>1</b>{someInt = 1})
   session.Save(typeof(entitytable<b>1</b>), new entitytable<b>1</b>{someInt = 3})
   transction.Commit();

Is it possible to get example 1 to work as 2, that is, to send different inserts batched together to the db (e.g. 1 roundtrip)?

Batching definition, for the few: To send multiple statements/inserts grouped together, up to Ado_Batch_size, to the database in one round trip.

The sql shown above is generated by Nhibernate of course!. It is what you would see on NHProfiler.

Version: Nhibernate 3.2

Newbie
  • 7,031
  • 9
  • 60
  • 85
  • maybe i'm missing something, but what is the difference in 1 and 2? looks identical. – nathan gonzalez Dec 10 '11 at 03:54
  • @nathangonzalez the tables names. They are 2 completely different inserts. – Newbie Dec 10 '11 at 04:46
  • i see now. what does "batch" mean to you? and how does this relate to nhibernate? that's just sql. – nathan gonzalez Dec 10 '11 at 05:01
  • @nathangonzalez See my updated question. I guess I assumed too much. You know Nhibernate has built-in IDbCommand batchers don't you? They seem to not be able to batch different insert-IDbCommands together. – Newbie Dec 10 '11 at 19:46
  • Why are you issuing SQL rather than using instances of your entity classes? This appears to be defeating the point of an O/RM. – TrueWill Dec 11 '11 at 04:04
  • @TrueWill See my updated question. The sql is generated by nhibernate on session.flush/transaction.commit. No hand written sql here. – Newbie Dec 11 '11 at 04:12
  • 1
    Please remember that we cannot read your mind. You might consider posting the C# code as well. Statements like "of course", "for the few", and "don't you?" could be taken as condescending. Remember, most of the people answering questions are volunteers. – TrueWill Dec 11 '11 at 04:18
  • @TrueWill Acknowledged. Also, please, see my updated question. – Newbie Dec 11 '11 at 05:07
  • @Newbie, when formatting code on SO, don't use the
     tag.  Just indent with four spaces.
    –  Dec 11 '11 at 05:17
  • @Inuyasha how do I bold some text if I only use four spaces? Only with pre I've been able to do this. With you change, now all the code is off. see the tags. – Newbie Dec 11 '11 at 05:51
  • @Inuyasha - actually the pre was my doing. There were some initial subtleties that caused misunderstandings, and I wanted to emphasize those with b tags as the OP mentioned. Also note that syntax highlighting adds little benefit when the code is mixed SQL, comments, and C#. – TrueWill Dec 11 '11 at 16:02

1 Answers1

1

As per nHibernate batch insert doesn't work with associations (which the original poster already found), it appears that this is not supported by the current version of NHibernate.

The article Profiling NHibernate Batching seems to confirm this, and suggests using stateless sessions (which unfortunately ignore associations).

Alternately, this answer suggests using HQL.

Caveat: I am not an NHibernate user. Be sure to check the actual behavior with NHProfiler.

Community
  • 1
  • 1
TrueWill
  • 25,132
  • 10
  • 101
  • 150