Quantcast
Channel: User Alexis Abril - Stack Overflow
Viewing all articles
Browse latest Browse all 43

Open/Close NHibernate Session

$
0
0

Using FluentNHibernate in a web application, I've created a singleton SessionFactory class to have the ability of the following:

SessionFactory.Instance //returns ISessionFactory

Is it common/best practice to open/close sessions as follows?

using(ISession session = SessionFactory.Instance.OpenSession()){    using(ITransaction transaction = session.BeginTransaction())    {        //some operation    }}

The above code would live in the respective repository classes for a given entity.

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.

UPDATE

Going forward with the HttpModule, I have a similar thought:

With a repository class, I'm basically doing the following(config uses WebSessionContext):

using(ISession session = SessionFactory.Instance.GetCurrentSession()){    using(ITransaction transaction = session.BeginTransaction())    {        //some operation    }}

Viewing all articles
Browse latest Browse all 43

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>