NHibernate SessionFactory Thread Safe Release

So here is the problem. I have a shared class library that stores all repositories, domain and mapping files, so the library can be reused in other web applications. Now in this class library there is a piece of code that allows itself to create a factory session that will be used in its repositories. The code looks something like this.

private static ISessionFactory sessionFactory; private static Configuration configuration; public static Configuration Configuration() { if (configuration == null) { configuration = new Configuration().Configure(); } return configuration; } private static ISessionFactory SessionFactory { get { if (sessionFactory == null) { sessionFactory = Configuration().BuildSessionFactory(); } return sessionFactory; } } public static ISession GetCurrentSession() { if (!CurrentSessionContext.HasBind(SessionFactory)) { CurrentSessionContext.Bind(SessionFactory.OpenSession()); } return SessionFactory.GetCurrentSession(); } 

So the repository calls the GetCurrentSession () method to get the ISession. Now this works fine, but I am worried that it may not be thread safe. Can someone help me with an approach that will help me make it thread safe.

A few notes:

I thought about setting up and creating a SessionFactory in global.asax web applications at the launch event, but the problem is that the shared class library in question is used in 20 different applications, so this would mean switching to all applications and updating the global.asax file. before I do this, I wanted to ask a question to see if there are other ways I can do this. So the library of ordinary classes can independently configure its SessionFactory and nevertheless be thread safe.

Thanks for reading this huge question. Will attach any help.

+4
source share
3 answers

The factory session is streaming, the session is not. Creating a factory session needs to be protected:

  private static object lockObject = new object(); private static ISessionFactory SessionFactory { get { lock (lockObject) { if (sessionFactory == null) { sessionFactory = Configuration().BuildSessionFactory(); } return sessionFactory; } } } 

A factory session is created the first time a thread is requested. This should be a safe thread so as not to create a factory session multiple times.

Creating a session with a factory session is thread safe, so you don't need to worry about that.

+6
source

Sessions are not thread safe at NHibernate by design. Therefore, this should be normal if you have a session used by only one thread . You can have one NHibernate SessionFactory for multiple threads, if you have a separate NHibernate session for each thread

For more information see the link below:

https://forum.hibernate.org/viewtopic.php?p=2373236&sid=db537baa5a57e3968abdda5cceec2a24

0
source

I suggest using one session for each request like this:

 public ISession GetCurrentSession() { HttpContext context = HttpContext.Current; var currentSession = context.Items["session"] as ISession; if( currentSession is null ) { currentSession = SessionFactory.GetCurrentSession() context.Items["session"] = currentSession; } return currentSession; } 
0
source

All Articles