NHibernate & ASP.NET - Loading time

Hi,

I’m currently working on a project where I use the data access code generated by Visual Paradigm in an ASP.NET application.

The problem I encountered was that whenever there was some kind of serious exception, NHibernate stopped working and only a restart of ASP.NET fixed the problem.

I think this was caused by the fact that NHibernate runs as a ‘session’ inside the application domain. So all users of the ASP.NET application use this same NHibernate session. If NHibernate crashes for one user, (on some occasions) it also does this for others.

When thinking of solutions :idea: I came up with the following:

  1. Creating an NHibernate session per page request.
  2. Creating an NHibernate session per user session
  3. Find some other solution

I’m looking for help on this topic. 1 and 2 did not really work because the application became too slow. To be precise, the instantiating of a PersistentManager takes too long. It occupies the server for a few seconds. :shock: This is unworkable in a web application.

MyProjectPersistentManager persistentManager = new MyProjectPersistentManager();

So now I have the following questions:

  • Does anyone know a solution for using NHibernate generated by Visual Paradigm in ASP.NET?
  • Does anyone know how to make the PersistentManager load faster?

Hello starwave,

First, PersistantManager is singleton, you only need to initialize it for once.Also, if the session is not used, close it. This will create a new session the next time. The session can be thread based instead of single.

Best regards,
Jick

Hi Jick,

Thanks. I fixed it by putting the following in Global.asax (thanks Dennis!)

    void Application_EndRequest(object sender, EventArgs e)
    {
        MyPersistentManager.Instance().GetSession().Close();
    }