NHibernate中Session是線程不安全的
ISessionManage
using System;
using NHibernate;
namespace Commercial
{
/// <summary>
/// 功能
/// </summary>
public interface ISessionManage
{
/// <summary>
/// 獲取Session的一個實例
/// </summary>
/// <returns>返回實現NHibernate
ISession Get();
/// <summary>
/// 設置Session的一個實例
/// </summary>
/// <param name=
void Set(ISession session);
}
}
--------------------------------------------
WebNHSession
using System;
using System
using NHibernate;
namespace Commercial
{
/// <summary>
/// 功能
/// 一方面是線程不安全的
/// 因此通過將Session綁定到HttpContext上
/// 在用戶的請求結束後關閉Session並自己釋放掉
/// </summary>
public class WebNHSession : ISessionManage
{
public WebNHSession()
{
}
/// <summary>
/// 獲取存儲到HttpContext中的實現NHibernate
/// </summary>
/// <returns>實現NHibernate
public ISession Get()
{
return (ISession)HttpContext
}
/// <summary>
/// 存儲實現NHibernate
/// </summary>
/// <param name=
public void Set(ISession session)
{
if (session != null)
{
HttpContext
}
else
{
HttpContext
}
}
}
}
---------------------------------------------
WinFormNHSession
using System;
using NHibernate;
namespace Commercial
{
/// <summary>
/// 功能
/// 一方面是線程不安全的
/// 因此通過線程變量獲取一個NHibernate Session的多個線程安全的實例
/// </summary>
public class WinFormNHSession : ISessionManage
{
[ThreadStatic]
private static ISession _threadSession = null;
public WinFormNHSession()
{
}
/// <summary>
/// 獲取存儲到線程變量中的實現NHibernate
/// </summary>
/// <returns>實現NHibernate
public ISession Get()
{
if (_threadSession != null)
{
if (_threadSession
{
_threadSession
}
}
return _threadSession;
}
/// <summary>
/// 存儲實現NHibernate
/// </summary>
/// <param name=
public void Set(ISession session)
{
if (_threadSession
{
session
}
_threadSession = session;
}
}
}
SessionFactory
using System;
using System
using NHibernate;
namespace Commercial
{
/// <summary>
/// 功能
/// </summary>
public class SessionFactory
{
private static ISession session = null;
private static ISessionManage sessionManage = null;
static SessionFactory()
{
Init();
}
/// <summary>
/// 獲取實現NHibernate
/// </summary>
/// <returns>返回實現NHibernate
public static ISession GetSession()
{
session = sessionManage
if (session == null)
{
session = NHibernateSession
sessionManage
}
return session;
}
private static void Init()
{
System
sessionManage = (ISessionManage)ass
}
}
}
----------------------------------------------
NHibernateSession
using System;
using System
using System
using System
using NHibernate;
using NHibernate
namespace Commercial
{
/// <summary>
/// 功能
/// </summary>
public class NHibernateSession
{
private static Configuration cfg = null;
private static ISessionFactory sessionFactory = null;
static NHibernateSession()
{
cfg = new Configuration()
sessionFactory = cfg
}
/// <summary>
/// 獲取NHibernate的Session實例
/// </summary>
/// <returns></returns>
public static ISession GetNHibernateSession()
{
return sessionFactory
}
}
}
---------------------------------------------
SessionConfigManage
using System;
using System
using System
using System
namespace Commercial
{
/// <summary>
/// 功能
/// 日期
/// 作者
/// </summary>
public class SessionConfigManage
{
private const string SESSION_ITEM_NAME =
private static object _locker = new object();
private static string _sessionItemName = string
private static string _assemblyName = string
static SessionConfigManage()
{
string configString = ConfigurationManager
string[] arr = configString
_sessionItemName = arr[
_assemblyName = arr[
}
/// <summary>
/// 獲取配置文件中名為SESSION_ITEM_NAME配置節的信息
/// </summary>
/// <returns>實現ISessionManage接口的類的名稱</returns>
public static string SessionSourceItemName
{
get
{
lock (_locker)
{
return _sessionItemName;
}
}
}
/// <summary>
/// 獲取配置文件中名為SESSION_ITEM_NAME配置節的信息
/// </summary>
/// <returns>實現ISessionManage接口的類的程序集名稱</returns>
public static string AssemblyName
{
get
{
lock (_locker)
{
return _assemblyName;
}
}
}
}
}
在Web
<appSettings>
<!
<!
<add key=
<!
<!
</appSettings>
在Global
/// <summary>
/// 當用戶斷開請求時用來關閉用戶請求的Session的連接
/// </summary>
/// <param name=
/// <param name=
void Session_End(object sender
{
NHibernate
if (session != null)
{
session
}
}
From:http://tw.wingwit.com/Article/program/net/201311/13675.html