title: Open/Close NHibernate Session
body: <p>Using FluentNHibernate in a web application, I've created a singleton SessionFactory class to have the ability of the following:</p>
  
  <pre><code>SessionFactory.Instance //returns ISessionFactory
  </code></pre>
  
  <p>Is it common/best practice to open/close sessions as follows?</p>
  
  <pre><code>using(ISession session = SessionFactory.Instance.OpenSession())
  {
      using(ITransaction transaction = session.BeginTransaction())
      {
          //some operation
      }
  }
  </code></pre>
  
  <p>The above code would live in the respective repository classes for a given entity.</p>
  
  <p>I've noticed there is a theme of creating a HttpModule to open the session at the start and stop of the application, but I'm wondering if this is situational or more common.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Going forward with the HttpModule, I have a similar thought:</p>
  
  <p>With a repository class, I'm basically doing the following(config uses WebSessionContext):</p>
  
  <pre><code>using(ISession session = SessionFactory.Instance.GetCurrentSession())
  {
      using(ITransaction transaction = session.BeginTransaction())
      {
          //some operation
      }
  }
  </code></pre>