title: Optimizing nhibernate session factory, startup time of webApp really slow
body: <p>I have implement testing app. which uses fluent nhibernate mapping to db object inside mssql db. Since I want to learn fine tune nhib. mvc3 applications, I'm using this app. for testing purposes which have only one simple entity with 10 enum properties and one string property.
  So, it is really lightwave, yet startup time according to nhibernate profiler is 4.37 sec. Which is really slow for rendering one entity with few lines checked/unchecked property.</p>
  
  <p>Code is the following.
  <strong>Domain.SessionProvider.cs</strong></p>
  
  <pre><code>public static ISessionFactory CreateSessionFactory()
  {
     var config = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008
            .ConnectionString(c =&gt; c.FromConnectionStringWithKey("myConnection")))
            .Mappings(m =&gt; m.FluentMappings.Add&lt;FeaturesMap&gt;())
            .ExposeConfiguration(p =&gt; p.SetProperty("current_session_context_class", "web"))
            .BuildConfiguration();
  
            return config.BuildSessionFactory();            
  }
  </code></pre>
  
  <p><strong>Global.asax</strong></p>
  
  <pre><code>public class MvcApplication : System.Web.HttpApplication
  {   
     //SessionPerWebRequest is ommited here as well as other content
     public static ISessionFactory SessionFactory =
                 SessionProvider.CreateSessionFactory();
  
      protected void Application_Start()
      {
         SessionFactory.OpenSession();
      }
  }
  </code></pre>
  
  <p>Inside myController I have following:</p>
  
  <pre><code>public ActionResult Index()
  {
     return View(GetData());
  }
  
  private IList&lt;FeaturesViewModel&gt; GetData()
  {
       List&lt;Features&gt; data;
       using (ISession session = MvcApplication.SessionFactory.GetCurrentSession())
       {
            using (ITransaction tx = session.BeginTransaction())
            {
                data = session.Query&lt;Features&gt;().Take(5).ToList();
                tx.Commit();
  
                var viewModelData = FeaturesViewModel.FromDomainModel(data);
                return viewModelData;
             }
        }
  }
  </code></pre>