Solution for nhib.pdf session management in web projects

Source: Internet
Author: User
Session management of Nhibernate has always been a problem. In system development

If lazy = "true" exists and the session is not managed, the following error is thrown:

Code: Failed to lazily initialize a collection-No session [copy to clipboard]

In the WEB Project, the solution is to open the session in the application_beginrequest method and put it into httpcontext, and disable sesssion in the application_endrequest method.

Fortunately, nhibernate1.2 has provided related support, as shown below:

Add the following to Web. config:CodeCode:<Httpmodules>
<Add name = "currentsessionmodule" type = "nhib.pdf. example. Web. currentsessionmodule"/>
</Httpmodules> [copy to clipboard]

ProgramAs follows:

Code: Using system;
Using system. Web;
Using nhib.pdf. context;

Namespace nhib.pdf. example. Web
{
Public class currentsessionmodule: ihttpmodule
{
Public void Init (httpapplication context)
{
Context. beginrequest + = new eventhandler (application_beginrequest );
Context. endrequest + = new eventhandler (application_endrequest );
}

Public void dispose ()
{
}

Private void application_beginrequest (Object sender, eventargs E)
{
Managedwebsessioncontext. BIND (httpcontext. Current, exampleapplication. sessionfactory. opensession ());
}

Private void application_endrequest (Object sender, eventargs E)
{
Isession session = managedwebsessioncontext. Unbind (httpcontext. Current, exampleapplication. sessionfactory );
If (session! = NULL)
{
If (session. transaction. isactive)
{
Session. transaction. rollback ();
}

If (session! = NULL)
{
Session. Close ();
}
}
}
}
========================================================== ================================ In addition, I also found a session management template, please refer to the code

Using system;
Using system. Collections. Generic;
Using system. text;

Using nhib.pdf;
Using nhib.pdf. cfg;

Namespace nhib.pdf. generated. Base
{
Public interface inhibernatesessionmanager: idisposable
{
// Methods
Inhibernatesession createsession ();
Isession createisession ();

// Properties
Inhibernatesession {Get ;}
}

/// <Summary>
/// A singleton that creates and persits a single sessionfactory for the to program to access globally.
/// This uses the. NET callcontext to store a session for each thread.
///
/// This is heavely Based on 'nhib1_best practices with ASP. net' by Billy McCafferty.
// Http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx
/// </Summary>
Public class nhibernatesessionmanager: inhibernatesessionmanager
{
# Region static content

Private Static inhibernatesessionmanager _ nhibernatesessionmanager = NULL;
/// <Summary>
/// Set method is exposed so that the inhibernatesessionmanager can be swapped out for unit testing.
/// Note: cannot set instance after it has been initialized, and calling get will automatically intialize the instance.
/// </Summary>
Public static inhibernatesessionmanager instance
{
Get
{
If (_ nhibernatesessionmanager = NULL)
_ Nhibernatesessionmanager = new nhibernatesessionmanager ();
Return _ nhibernatesessionmanager;
}
Set
{
If (_ nhibernatesessionmanager! = NULL)
Throw new exception ("cannot set instance after it has been initialized .");
_ Nhibernatesessionmanager = value;
}
}

# Endregion

# Region declarations

Private isessionfactory _ sessionfactory = NULL;
Private const string _ sessioncontextkey = "nhibernatesession-contextkey ";

# Endregion

# region constructors & finalizers

///


// This will load the Nhibernate settings from the app. config.
// note: this can/shocould be expanded to support multiple databases.
//
private nhibernatesessionmanager ()
{< br> _ sessionfactory = new nhib.pdf. cfg. configuration (). configure (). buildsessionfactory ();
}< br> ~ Nhibernatesessionmanager ()
{< br> dispose (true);
}

# Endregion

# Region idisposable

private bool _ isdisposed = false;
Public void dispose ()
{< br> dispose (false );
}< br> private void dispose (bool finalizing)
{< br> If (! _ Isdisposed)
{< br> // close sessionfactory
_ sessionfactory. Close ();
_ sessionfactory. Dispose ();

// Flag as disposed.
_ Isdisposed = true;
If (! Finalizing)
GC. suppressfinalize (this );
}
}

# Endregion

# Region methods

Public inhibernatesession createsession ()
{
Return new nhibernatesession (createisession ());
}
Public isession createisession ()
{
Isession;
Lock (_ sessionfactory)
{
Isession = _ sessionfactory. opensession ();
}
Return isession;
}

# Endregion

# Region Properties

Public inhibernatesession
{
Get
{
Inhibernatesession session = contextsession;

// If the thread does not yet have a session, create one.
If (session = NULL)
{
Session = createsession ();

// Save to callcontext.
Contextsession = session;
}

Return session;
}
}
Private inhibernatesession contextsession
{
Get
{
If (iswebcontext)
Return (nhibernatesession) system. Web. httpcontext. Current. items [_ sessioncontextkey];
Else
Return (nhibernatesession) system. runtime. remoting. messaging. callcontext. getdata (_ sessioncontextkey );
}
Set
{
If (iswebcontext)
System. Web. httpcontext. Current. items [_ sessioncontextkey] = value;
Else
System. runtime. remoting. messaging. callcontext. setdata (_ sessioncontextkey, value );
}
}
Private bool iswebcontext
{
Get {return (system. Web. httpcontext. Current! = NULL );}
}

# Endregion
}
}

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.