Asp. Session management of NHiernate in Net

  • 2020-05-05 11:07:35
  • OfStack

Session in NHibernate seems to me to be the equivalent of a database connection. Because it also has Open/Close method, I did not study the source code of NHibernate, do not know whether this understanding is wrong? I searched a lot about the Internet Session management, mostly in my need of database operations, is OpenSession (), after the operation is CloseSession (). It's a little bit quasi as we just begin to learn ADO NET, object to Connection Open (), data processing and then Close (). But it has brought a drawback, because the frequent switch Connection is very consumption of system resources. I remember when I was making a data entry interface before, because this entry interface had many data elements, and many DropDownList needed to read and bind data in the database.

In this way, in Page_Load of this page, we need to call the corresponding object's methods -- one to retrieve the data binding DropDownList from the database. Since the methods of these objects all use separate Connection, they have their own Connection, Open and Close. As a result, it takes a long time to open the page, which is relatively slow. Later, we processed the data that needed to be bound to DropDownList into an DataSet through one data, and bound DataTable in DataSet to DropDownList. This only requires once Connection Open/Close. The page is much faster.

Therefore, I think the above Session management method is not very appropriate.

Later, I looked at his Session management in the Cuyahoga open source project, and he used the "session-per-request" model. Literally, he creates an Session for each Request until the request is destroyed, and the Session is Close. What Cuyahoga does is a little different from session-per-request in that he creates an CoreRepository object for each Request, and CoreRepository is the class of data processing services that the system needs. He did this by first creating HttpModule (NHSessionModule) to create CoreRepository objects and destroy CoreRepository objects, as follows:

private void Context_BeginRequest(object sender, EventArgs e)
  {
    // Create the repository for Core objects and add it to the current HttpContext.
    CoreRepository cr = new CoreRepository(true);
    HttpContext.Current.Items.Add("CoreRepository", cr);
  }
  private void Context_EndRequest(object sender, EventArgs e)
  {
    // Close the NHibernate session.
    if (HttpContext.Current.Items["CoreRepository"] != null)
    {
      CoreRepository cr = (CoreRepository)HttpContext.Current.Items["CoreRepository"];
      cr.CloseSession();
    }
  }

In this way, CoreRepository objects are automatically created at each request, and when the request is completed, CloseSession (), HttpContext.Current.Items ["CoreRepository"] can be retrieved from the program through HttpContext.Current.Items ["CoreRepository"].

In this way, Session in NHibernate is managed in a disguised way, and the mode of "session-per-request" is achieved.

Detailed explanation: initialize Nhibernate Session

by implementing IHttpModule

This way than the above operation is that every time you need to create Session, performance and speed should be improved, and then I thought, create Session every request, if we can create Connection Pool, also create a Session Pool, every time this request is not create Session directly, but in our Session Pool of already created good Session, so efficiency is not better? !

 


Related articles: