NHibernate shares connection with 2 different sessions

i have a MVC web api application that uses nhibernate as ORM library, i follow session per request, repository and unit of work pattern.
the flow is as follows

1- client make api request

2- new unit of work object is created with a new nhibernate ISession object

3- repository is created using the unit of work object, and a query is done.

this works fine as long i send a single request, but when i sent 2 requests, i keep receiving errors regarding the internal datareader is still open or is already closed, after little investigation i found that each unit of work is creating a new instance of nhibernate ISession, but the 2 different ISession of each request have the same connection object
i watched this 2 properties to findout

1- //this has different across requests


2- //this has the same value for the 2 requests


this is how session is created in UnitOfWork object

public UnitOfWork(string connectionName, Assembly mappingAssembly)
//sessionFactories is a container for each connection session factory object, 
    lock (sessionFactories)
        if (!sessionFactories.ContainsKey(connectionName))
            sessionFactories.Add(connectionName, SessionFactoryConfigurator.CreateConfiguration(mappingAssembly, connectionName));

    Session = sessionFactories[connectionName].OpenSession();


Source: New Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.