Session
Session in NHibernate is thread insecure and has some impact on performance each time a database operation requests a session creation. Thread safety can be easily implemented through the [threadstatic] attribute in Windows applications, while in the web you can implement thread safety by using the session with the request HttpContext binding, and the user currently requests only one session. The code is as follows:
ISessionManage.cs
Using System;
Using NHibernate;
Namespace Commercial.Jwsoft.Framework.Persistence.SessionManage
{
///<summary>
///function: Mainly used to encapsulate the session class of the third party class library operation database, which is mainly used to encapsulate the session in NHibernate
///</ SUMMARY>
public interface Isessionmanage
{
///<summary>
///Get an instance of the session
///</summary>
/// <returns> returns the class </returns> that implements the Nhibernate.isession interface;
ISession get ();
<summary>
Set an instance of the session
</summary>
<param name= "Session" > Implementation of Nhibernate.isession Interface class </param>
void Set (ISession session);
}
}
--------------------------------------------
WebNHSession.cs
Using System;
Using System.Web;
Using NHibernate;
Namespace Commercial.Jwsoft.Framework.Persistence.SessionManage
{
<summary>
Function: This class is used for Web applications, and the session provided by NHibernate has two defects:
On the one hand, the thread is unsafe, and on the other hand each database operation creates a session that has an impact on program performance.
So by binding the session to the HttpContext, each user has a unique one, and
Closes the session after the user's request ends and releases itself.
</summary>
public class Webnhsession:isessionmanage
{
Public Webnhsession ()
{
}
<summary>
Gets the class instance of the implementation nhibernate.isession interface stored in the HttpContext
</summary>
<returns> implements a class instance of the Nhibernate.isession interface that returns null</returns> when the user does not call the Set method
Public ISession Get ()
{
Return (ISession) httpcontext.current.items[sessionconfigmanage.sessionsourceitemname];
}
<summary>
Store the class instance that implements the Nhibernate.isession interface to the HttpContext
</summary>
<param name= "Session" > Implementation of Nhibernate.isession Interface class instance </param>
public void Set (ISession session)
{
if (session!= NULL)
{
HTTPCONTEXT.CURRENT.ITEMS.ADD (Sessionconfigmanage.sessionsourceitemname, session);
}
Else
{
HttpContext.Current.Items.Remove (Sessionconfigmanage.sessionsourceitemname);
}
}
}
}
---------------------------------------------
WinFormNHSession.cs
Using System;
Using NHibernate;
Namespace Commercial.Jwsoft.Framework.Persistence.SessionManage
{
<summary>
Function: This class is used for Windows applications, and the session provided by NHibernate has two defects:
On the one hand, the thread is unsafe, and on the other hand each database operation creates a session that has an impact on program performance.
Therefore, a thread variable is used to get a multiple thread-safe instance of the NHibernate session, and the thread variable is released after it is in use.
</summary>
public class Winformnhsession:isessionmanage
{
[ThreadStatic]
private static ISession _threadsession = null;
Public Winformnhsession ()
{
}
<summary>
Gets the class instance that implements the Nhibernate.isession interface stored in the thread variable
</summary>
<returns> a thread-safe class instance that implements the Nhibernate.isession interface that returns null</returns> when the user does not call the Set method
Public ISession Get ()
{
if (_threadsession!= null)
{
if (_threadsession.isconnected)
{
_threadsession.reconnect ();
}
}
return _threadsession;
}
<summary>
Store the class instance that implements the Nhibernate.isession interface to the thread variable
</summary>
<param name= "Session" > Implementation of Nhibernate.isession Interface class instance </param>
public void Set (ISession session)
{
if (_threadsession.isconnected)
{
Session. Disconnect ();
}
_threadsession = session;
}
}
}
----------------------------------------------
SessionFactory.cs
Using System;
Using System.Runtime.Remoting;
Using NHibernate;
Namespace Commercial.Jwsoft.Framework.Persistence.SessionManage
{
<summary>
Function: Manage multiple class factories implementing Isessionmanage interface, dynamically create session based on read class name information to be loaded
</summary>
public class Sessionfactory
{
private static ISession session = NULL;
private static Isessionmanage sessionmanage = null;
Static Sessionfactory ()
{
Init ();
}
<summary>
Gets the session instance that implements the Nhibernate.isession interface
</summary>
<returns> returns a class instance that implements the Nhibernate.isession interface </returns>
public static ISession getsession ()
{
Session = Sessionmanage.get ();
if (session = NULL)
{
Session = Nhibernatesession.getnhibernatesession ();
Sessionmanage.set (session);
}
return session;
}
private static void Init ()
{
System.Reflection.Assembly ass = System.Reflection.Assembly.Load (sessionconfigmanage.assemblyname);
Sessionmanage = (isessionmanage) ass. CreateInstance (Sessionconfigmanage.sessionsourceitemname);
}
}
}
----------------------------------------------
NHibernateSession.cs
Using System;
Using System.Data;
Using System.Collections.Generic;
Using System.Text;
Using NHibernate;
Using Nhibernate.cfg;
Namespace Commercial.Jwsoft.Framework.Persistence.SessionManage
{
<summary>
Function: Load Embedded Resource (XML configuration file), open a sessionfactory, get NHibernate Session instance
</summary>
public class Nhibernatesession
{
private static Configuration cfg = null;
private static isessionfactory sessionfactory = null;
Static Nhibernatesession ()
{
CFG = new Configuration (). Configure ();
Sessionfactory = cfg. Buildsessionfactory ();
}
<summary>
Get the session instance of NHibernate
</summary>
<returns></returns>
public static ISession Getnhibernatesession ()
{
return Sessionfactory.opensession ();
}
}
}
---------------------------------------------
SessionConfigManage.cs
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Configuration;
Namespace Commercial.Jwsoft.Framework.Persistence.SessionManage
{
<summary>
Function: Dynamically create class instances depending on the application environment of the class library (Windows application or Web application)
Date: 2006-08-24
Author: Kuo Shaohong
</summary>
public class Sessionconfigmanage
{
Private Const string session_item_name = "Sessionitemname";
private static Object _locker = new Object ();
private static string _sessionitemname = String. Empty;
private static string _AssemblyName = String. Empty;
Static Sessionconfigmanage ()
{
String configstring = Configurationmanager.appsettings[session_item_name];
string[] arr = configstring.split (', ');
_sessionitemname = arr[0];
_AssemblyName = arr[1];
}
<summary>
Gets the information named Session_item_name configuration section in the configuration file, the name of the class for the sessionmanage to be loaded
</summary>
<returns> the name of the class that implements the Isessionmanage interface </returns>
public static string Sessionsourceitemname
{
Get
{
Lock (_locker)
{
return _sessionitemname;
}
}
}
<summary>
Gets the information named Session_item_name configuration section in the configuration file, the name of the class for the sessionmanage to be loaded
</summary>
<returns> the assembly name of the class implementing the Isessionmanage interface </returns>
public static string AssemblyName
{
Get
{
Lock (_locker)
{
return _assemblyname;
}
}
}
}
}
The configuration section in the Web.config file is as follows:
<appSettings>
<!---->
<!--the class name that gets the session loaded in the Web application-->
<add key= "Sessionitemname" value= "Commercial.Jwsoft.Framework.Persistence.SessionManage.WebNHSession, Jwframework "/>
<!--the class name to get the session loaded in Windows application-->
<!--<add key= "Sessionitemname" value= " Commercial.jwsoft.framework.persistence.sessionmanage.winformnhsession,jwframework "/>-->
</appSettings>
Add the following code to the Global.asax:
<summary>
Connection to turn off user requested session when a user disconnects a request
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
void Session_End (object sender, EventArgs e)
{
Nhibernate.isession session = Commercial.Jwsoft.Framework.Persistence.SessionManage.SessionFactory.GetSession ();
if (session!= NULL)
{
Session. Close ();
}
}