Management of NHiernate Session in Asp.Net
Author:Eve Cole
Update Time:2009-06-30 16:46:38
Session in NHibernate seems to be equivalent to a database connection in my understanding. Because it also has Open/Close methods, I have not studied the source code of NHibernate. I wonder if this understanding is wrong? I searched a lot about Session management on the Internet. Most of them are OpenSession() when I need database operations, and CloseSession() after the operation. This is a bit similar to when we first started learning ADO.NET. , Open() the Connection object, and then Close() after the data is processed. But this brings a drawback, because frequent switching of Connection consumes system resources. I remember when I was making a data entry interface before, because this entry interface had many data elements, and many DropDownLists needed to read data in the database and bind it.
In this way, in the Page_Load of the page, the methods of the corresponding objects need to be called one by one to retrieve the data binding DropDownList from the database. Because the methods of these objects of ours use independent Connections, they all have their own Open and Close of the Connection. Therefore, it takes a long time to open this page, which is relatively slow. Later, we processed the data that needed to be bound to the DropDownList into a DataSet, and bound the DataTable in the DataSet to the DropDownList. In this way, only one Open/Close of Connection is required. The page is much faster.
Therefore, I feel that 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.
The literal understanding is that it creates a Session for each Request until the request is destroyed, then the Session is Closed.
Cuyahoga's approach is a little different from session-per-request in that he creates a CoreRepository object for each Request. CoreRepository is the class of data processing services required by the system.
What he did was to first create 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, the CoreRepository object will be automatically created every time a request is made. When the request is completed, CloseSession() can be used to obtain the CoreRepository object through HttpContext.Current.Items["CoreRepository"] in the program.
In this way, the Session in NHibernate is managed in disguised form, and the "session-per-request" model is achieved.
Detailed explanation: Initialize Nhibernate's Session by implementing IHttpModule
Compared with the above method, which requires creating a Session for every operation, this method should improve the performance and speed a lot.
Then I thought, every request creates a Session. Can we also create a Session Pool like the Connection Pool?
In this way, instead of creating a Session directly every time a request is made, the already created Session is taken from our Session Pool. Isn't this more efficient? !
http://maplye.cnblogs.com/archive/2006/06/26/435683.html