NHibernate in the session, in my understanding seems to be equivalent to the connection in the database. Because it also has open/close method, I did not study the source code of NHibernate, do not know this understanding is wrong? I search on the internet a lot of the management of the session, most of the time I need the database operation, on the Opensession (), after the operation of the CloseSession (). It's kind of like when we first started to learn ado.net, we had to connection the object open (), and Close () when the data was processed. But here is a disadvantage, because the frequent switches of the connection are very consuming system resources. I remember in the past when making a data entry interface, because this input interface data elements are more, and many DropDownList need to read data in the database and binding.
This way, the method of calling the object in the Page_Load of the page retrieves the data binding DropDownList from the database. Because the way we object is to use separate connection, we have our own connection open and close. As a result, this page will need to wait a long time to open, rather slowly. We then dropdownlist the data that needs to be bound to a dataset and binds the DataTable in the DataSet to the DropDownList. It only takes one connection open/close. The page is much faster.
Therefore, I think that the management of the above session is not very appropriate.
Later, I saw the Cuyahoga Open source project in his session management, he used the "session-per-request" this mode. The literal understanding is that he creates a session for each request until the request is destroyed, and then the session is close. and Cuyahoga his approach and session-per-request a bit different is that he created a Corerepository object for each request, Corerepository is the class of data processing services required by the system. His approach was to 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 ();
}
}
This will automatically create the Corerepository object at each request, and when the request is complete, closesession (), and httpcontext.current.items["Corerepository" in the program. You can get the Corerepository object.
This also disguised as the management of the NHibernate in the session, it reached the "session-per-request" of this model.
Detailed explanation: Through the implementation of IHttpModule initialization nhibernate session
This is a better way to create a session for each operation than the one above, and the performance and speed should improve a lot, and then I think that every request creates a session, whether we can create a session pool like the connection pool, In this way, instead of creating a session directly at the time of the request, we take the session that has already been created in our session pool, which is not a better efficiency?!