title: Why does NHibernate Session.Save locks the database?
body: <p>I've built a small ASP.NET MVC web app with NHibernate as ORM. On every HTTP request beginning I create a session and open a transaction and close it at the end of the request. </p>
  
  <pre><code>// Begin request
  
  session = StaticSessionManager.OpenSession();    
  session.BeginTransaction();
  
  // End of request
  
  currentSession.Transaction.Commit();
  currentSession.Flush();
  currentSession.Close();
  </code></pre>
  
  <p>Now I'm also using <a href="http://www.dotnetopenauth.net/" rel="nofollow">DotNetOpenAuth</a> API for Facebook/Google Login. In order for me to register a new user the API expects me to update a Table(say UserProfile) with some user information. This I'm doing via Nhibernate's Save()</p>
  
  <pre><code>session.Save(new UserProfile { UserName = userName });
  </code></pre>
  
  <p>In the same web request the DotNetOpenAuth API expects to read the above inserted data from that table(UserProfile).</p>
  
  <p>Now this is leading to a deadlock scenario as NHibernate's Save applied some sort of lock on the database. Even when I manually try to read data off that table using SQL server Management studio, it seems to wait until the web request times out. </p>
  
  <p>Now I've tried explicit flushing after Save</p>
  
  <pre><code>session.Flush();
  </code></pre>
  
  <p>but even this doesn't get rid of the lock. I want my DotNetOpenAUth API to be able to read the most updated data.</p>
  
  <p>Currently I'm comiting the Transaction after Save and re-opening one more after. I'm not sure if this is the best practice/has any drawbacks.</p>