title: nhibernate : executing updates in batches
body: <p>I am trying to do batch updates using NHibernate, but it is not doing batch updates, its doing individual writes for all the rows. I have to write around 10k rows to db.</p>
  
  <pre><code>        using (var session = GetSessionFactory().OpenStatelessSession())
          {
              session.SetBatchSize(100);
              using (var tx = session.BeginTransaction())
              {
                  foreach (var pincode in list)
                  {
                      session.Update(pincode);
                  }
                  tx.Commit();
              }
          }
  </code></pre>
  
  <p>I am tried setting batch size to 100 using <code>session.SetBatchSize(100);</code> but that does not help. Also tried setting batch size using <code>cfg.SetProperty("adonet.batch_size", "100");</code> but thats also not helping.</p>
  
  <p>I am using GUID primary keys, hence I dont understand the reason for batch update failure. This is exactly the solution explained <a href="http://blogs.lessthandot.com/index.php/desktopdev/mstech/nhibernate-making-updates-and-inserts-mu/" rel="nofollow">here</a>. But its not working for me.</p>
  
  <p><strong>NOTE</strong> I have version field for optimistic concurrency mapped on all the entities. can that be the culprit for not having batch updates??</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>i tried using  state-ful session but that also did not help</p>
  
  <pre><code>        //example 2
          using (var session = GetSessionFactory().OpenSession())
          {
              session.SetBatchSize(100);
              session.FlushMode = FlushMode.Commit;
              foreach (var pincode in list)
              {
                  session.Update(pincode);
              }
              session.Flush();
          }
  
         //example 3
          using (var session = GetSessionFactory().OpenSession())
          {
              session.SetBatchSize(100);
              using (var tx = session.BeginTransaction())
              {
                  foreach (var pincode in list)
                  {
                      session.Update(pincode);
                  }
                  tx.Commit();
              }
          }
  </code></pre>
  
  <p><code>example 2</code> for some reason is causing double round trips.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>after further research I found that, each session.Update is actually updating the db</p>
  
  <pre><code>        using (var session = SessionManager.GetStatelessSession())
          {
              session.SetBatchSize(100);
              foreach (var record in list)
              {
                  session.Update(record);
              }
          }
  </code></pre>
  
  <p>how can I avoid that.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>tried with flush mode as well, but thats also not helping</p>
  
  <pre><code>        using (var session = SessionManager.GetNewSession())
          {
              session.FlushMode = FlushMode.Never;
              session.SetBatchSize(100);
              session.BeginTransaction();
              foreach (var pincode in list)
              {
                  session.SaveOrUpdate(pincode);
              }
              session.Flush();
              session.Transaction.Commit();
          }
  </code></pre>
  
  <p><strong>EDIT 4</strong></p>
  
  <p>even below one is not working, given i am fetching all entities in same session and updating and saving them in that session only...</p>
  
  <pre><code>        using (var session = SessionManager.GetSessionFactory().OpenSession())
          {
              session.SetBatchSize(100);
              session.FlushMode = FlushMode.Commit;
              session.Transaction.Begin();
              var list = session.QueryOver&lt;Pincode&gt;().Take(1000).List();
              list.ForEach(x =&gt; x.Area = "Abcd" + DateTime.Now.ToString("HHmmssfff"));
              foreach (var pincode in list) session.SaveOrUpdate(pincode);
              session.Flush();
              session.Transaction.Commit();
          }
  </code></pre>