title: nhibernate deadlocks
body: <p>I'm using the following code in an ASP.NET page to create a record, then count the records to make sure I haven't exceeded a set limit and rollback the transaction if I have.</p>
  
  <pre><code>using (var session = NhibernateHelper.OpenSession())
  using (var transaction = session.BeginTransaction())
  {
      session.Lock(mall, LockMode.None);
  
      var voucher = new Voucher();
      voucher.FirstName = firstName ?? string.Empty;
      voucher.LastName = lastName ?? string.Empty;
      voucher.Address = address ?? string.Empty;
      voucher.Address2 = address2 ?? string.Empty;
      voucher.City = city ?? string.Empty;
      voucher.State = state ?? string.Empty;
      voucher.Zip = zip ?? string.Empty;
      voucher.Email = email ?? string.Empty;
      voucher.Mall = mall;
      session.Save(voucher);
  
      var issued = session.CreateCriteria&lt;Voucher&gt;()
          .Add(Restrictions.Eq("Mall", mall))
          .SetProjection(Projections.Count("ID"))
          .UniqueResult&lt;int&gt;();
  
      if (issued &gt;= mall.TotalVouchers)
      {
          transaction.Rollback();
          throw new VoucherLimitException();
      }
  
      transaction.Commit();
      return voucher;
  }        
  </code></pre>
  
  <p>However, I'm getting a ton of deadlocks. I presume this happens because I'm trying to count the records in a table I just performed an insert on and a lock is still held on the inserted row, causing the deadlock.</p>
  
  <ul>
  <li>Can anyone confirm this?</li>
  <li>Can anyone suggest an fix?</li>
  </ul>
  
  <p>I've tried calling SetLockMode(LockMode.None) on the final query, but that results in a NullReferenceException that I cannot figure out.</p>
  
  <p>Edit: If I run the query before I save the object, it works, but then I'm not accomplishing the goal of verifying that my insert didn't somehow go over the limit (in the case of concurrent inserts).</p>
  
  <p>Edit: I found that using IsolationLevel.ReadUncommited in the session.BeginTransaction call solves the problem, but I'm no database expert. Is this the appropriate solution to the problem or should I adjust my logic some how?</p>