How to process and use the configuration file of nhib.pdf multi-database multi-layer architecture (Part 1)

Source: Internet
Author: User
Supplementary reason: session management is a headache!

Handling of nhib.pdf configuration files and using multi-database multi-layer architecture ideas (I)The underlying idea is only applicable to single-thread situations!For example, such a simple MIS system: one or more clients are installed on their respective machines to connect to the same or multiple databases. Because only one session object is cached for a database.Multithreading is obviously not applicable!For example, for ASP. NET and. Net remoting, the above structure is not applicable! Even in a simple MIS system, we try to turn off the session when it is used up (Disconnect (), and re-open (reconnect () when necessary, which is determined by the isconnected attribute of the session. I did not do this in (1) the underlying entitycontrol class and session-related methods. But this supplement is complete!

In addition, you may encounter this exception: a different object with the same Identifier value was already associated with the session: 2, of Class :...... this is because the session is always saved globally. If addentity (OBJ) is used first, it contains an object OBJ, and later uses updateentity (obj1, obj1.id ); and obj1.id and obj. ID is the same, and OBJ and obj1 do not point to the same object. Solution: either ensure that obj1 and OBJ point to the same object (that is, the object is first retrieved from the session and then assigned a value to its attribute), or use session in the middle of the two steps. clear ().

What if I want to solve the multithreading problem? The main problem is managing session objects!

Although the local variables of the session can be declared in each method in the entitycontrol class in (1), and then the sessionfactory class provides the creation of a new session object, this solution can constitute a kill, single thread and multi-thread are applicable, but the efficiency is low, which is not suitable for our needs.If we manage session objects, we only need to rewrite the public isession opensession (); method for the sessionfactory class in the previous thought. Other changes are related to the management of session objects.That is to say, only one or two classes at the underlying layer have changes, and other layers do not have any changes, which does not affect the multi-layer architecture.

Specific circumstances have been taken! (In fact, I don't want to write it down, because there are a lot of good articles. For example, "session management of Nhibernate" has included the two types of situations I will list now! It is only because I wrote (1), (2), and (3) in the past that I still have the responsibility to clearly explain what is not clearly stated, more importantly, you can take notes for yourself to view,If there are any errors, you can leave them out for you to point out. In addition, I am different from them in that I operate on multiple databases..)

Handle two situations:

First case: ASP. NET.

It is best to provide a session for each user. Obviously, it is impossible to keep an independent session for each user. Therefore, it is not good to provide a session for each active thread of the request service, because each request can involve multiple threads, and everyone uses the same session, why should we provide separate sessions for multiple involved threads? Now the situation is clear. One session is provided for each request, and the session is released at the end of the request.

There are already many articles on the Internet to solve this problem. I just want to mention: In each request mode, a session is created (not in context .. Creates a session at beginrequest. The session is released at the end of request mode (context. endrequest.

(1) Configure web. config

<Httpmodules>
<Add name = "nhibernatemodule" type = "nhib.pdf. helper. module, nhib.pdf. helper"/>
</Httpmodules>

(2) create a class to end with the request mode.

// Configure //-----------------------------------------------------------------------------------------
// Reference address: http://blogs.intesoft.net/simon/articles/16.aspx
// Configure //-----------------------------------------------------------------------------------------
Using system;
Using system. Web;

Namespace nhib.pdf. helper
{
Public sealed class module: ihttpmodule
{
Public void Init (httpapplication context)
{
Context. endrequest + = new eventhandler (endrequest );
}

Public void dispose (){}

Public void endrequest (Object sender, eventargs E)
{
Sessionfactory. closesession ();
}
}
}

(3) create a class that contains all operations. Some code is omitted. This is a long time ago, so I am too embarrassed to write too much.

// Configure //-----------------------------------------------------------------------------------------
// Reference address: http://blogs.intesoft.net/simon/articles/16.aspx
// Configure //-----------------------------------------------------------------------------------------
Using system;
Using system. Web;
Using system. configuration;
Using nhib.pdf;
Using nhib.pdf. cfg;

Namespace nhib.pdf. helper
{
Public sealed class sessionfactory
{
Private Static readonly configuration = new configuration ();

Private Static readonly isession session = sessionfactory. opensession ();

Private Static readonly isessionfactory sessionfactory = configuration. Configure (httpcontext. Current. Request. mappath (configurationsettings. configurettings ["Nhibernate. config"]). buildsessionfactory ();

[Clscompliant (false)]
Public static isession session
{
Get
{
Isession session;
If (httpcontext. Current = NULL)
{
Session = sessionfactory. Session;
}
Else
{
If (httpcontext. Current. Items. Contains (sessionkey ))
{
Session = (isession) httpcontext. Current. items [sessionkey];
}
Else
{
Session = sessionfactory. sessionfactory. opensession ();
Httpcontext. Current. items [sessionkey] = session;
}
}
Return session;
}
}

[Clscompliant (false)]
Public static void closesession ()
{
If (httpcontext. Current = NULL)
{
Sessionfactory. session. Close ();
}
Else
{
If (httpcontext. Current. Items. Contains (sessionkey ))
{
Isession session = (isession) httpcontext. Current. items [sessionkey];
Session. Close ();
Httpcontext. Current. Items. Remove (sessionkey );
}
}

}
}

Other four addresses:
Handling of nhib.pdf configuration files and using multi-database multi-layer architecture (Part 1)
Http://blog.csdn.net/scucj/archive/2007/05/16/1610858.aspx
Handling of nhib.pdf configuration files and using multi-database multi-layer architecture (Part 2)
Http://blog.csdn.net/scucj/archive/2007/05/16/1610860.aspx
Handling of nhib.pdf configuration files and using multi-database multi-layer architecture (part 3)
Http://blog.csdn.net/scucj/archive/2007/05/16/1610861.aspx
Handling of nhib.pdf configuration files and using multi-database multi-layer architecture (Part 2)
Http://blog.csdn.net/scucj/archive/2007/05/16/1612323.aspx

References:
(1) session management of nhib.pdf
Http://blog.csdn.net/thebesghost/archive/2006/08/23/1107151.aspx
(2) What are precautions for winform development under multiple threads?
Http://www.cnblogs.com/Cajon/archive/2007/03/21/Multi_Thread_Win_Form.html
(3) nhib.pdf. helper project:
Http://blogs.intesoft.net/simon/articles/16.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.