title: Speed up bulk insert operations with NHibernate
body: <p>I want to speed up bulk <code>insert</code> operations with NHibernate 3.2 on Oracle 11g. To do this I tried</p>
  
  <pre><code>Session.Save(entity);
  Session.Flush();
  Session.Clear();
  </code></pre>
  
  <p>... in my <code>foreach</code> loop but got an exception caused by objects missing in the Session:</p>
  
  <blockquote>
    <p>failed to lazily initialize a collection of role: MyClass.PropertyX, no session or session was closed</p>
  </blockquote>
  
  <p>Another attempt was to set the batch size:</p>
  
  <pre><code>&lt;hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"&gt;
    &lt;session-factory&gt;
      &lt;property name="connection.provider"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/property&gt;
      &lt;property name="connection.driver_class"&gt;NHibernate.Driver.OracleClientDriver&lt;/property&gt;
      &lt;property name="connection.connection_string"&gt;xxx&lt;/property&gt;
      &lt;property name="dialect"&gt;NHibernate.Dialect.Oracle10gDialect&lt;/property&gt;
      &lt;property name="adonet.batch_size"&gt;50&lt;/property&gt;
      &lt;property name="query.substitutions"&gt;true=1, false=0&lt;/property&gt;
      &lt;property name="proxyfactory.factory_class"&gt;NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate&lt;/property&gt;
    &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>additionally I set <code>Session.SetBatchSize(50)</code> in my code an got the following exception:</p>
  
  <blockquote>
    <p>No batch size was defined for the session factory, batching is
    disabled. Set adonet.batch_size = 1 to enable batching.</p>
  </blockquote>
  
  <p>The only location where this exception is thrown is <a href="https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/AdoNet/NonBatchingBatcher.cs#L63" rel="noreferrer">NonBatchingBatcher</a>, so it looks like my session has the wrong batcher.</p>
  
  <p>What is wrong here? How can I speed up batch inserts with NHibernate (without using statlese sessions)?</p>