Nhibernate學習之性能改善1
1.學習目標
通過幾天來大家對Nhiberate的反映,很多人對它的性能非常的擔心,本文便著手從最直觀的角度和方法中逐步改善nhiberate的性能。改善性能是需要做出很多分析和測試的,本文試圖從最表層的對象入手,以后逐漸增加其他方面的性能分析。希望各位看官莫要著急。
2. 分析:
ISession和ISessionFactory對象的產生,使用,和銷毀對性能的影響。
ISessionFactory對象是線程安全的,它可以被程序的任意線程所適用,但是創建它的性能開銷是比較大的。所以不要頻繁創建ISessionFactroy對象
ISession對象是非線程安全的,創建它的開銷比較小
創建一個ISessionFactory對象的主要流程有:

這期間,包括對多個xml文件的解析和格式驗證,驗證的過程還包括對對象的反射。這些對性能損失非常大。用dottrace跟蹤程序執行,如下
在web應用程序里面,將ISessionFactory對象放到預緩存里面,可以避免頻繁創建ISessionFactory對象。如
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NHibernate;
using NHibernate.Cfg;


namespace WebApp
{
public sealed class NHibernateHelper
{
private const string CurrentSessionKey = "nhibernate.current_session";
private static readonly ISessionFactory sessionFactory;

static NHibernateHelper()
{
string cfgPath = @"E:\my project\nhibernate study\simle 1\NHibernateStudy1\NhibernateSample1\hibernate.cfg.xml";
sessionFactory = new NHibernate.Cfg.Configuration().Configure(cfgPath).BuildSessionFactory();
}

public static ISession GetCurrentSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;

if (currentSession == null)
{
currentSession = sessionFactory.OpenSession();
context.Items[CurrentSessionKey] = currentSession;
}

return currentSession;
}

public static void CloseSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;

if (currentSession == null)
{
// No current session
return;
}

currentSession.Close();
context.Items.Remove(CurrentSessionKey);
}

public static void CloseSessionFactory()
{
if (sessionFactory != null)
{
sessionFactory.Close();
}
}
}

}
用dottrace跟蹤結果為:

從執行時間來看
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
ISession session = NHibernateHelper.GetCurrentSession();
session.Close();
sw.Stop();
Response.Write(sw.ElapsedTicks+"<br>");
sw.Reset();
sw.Start();
session = NHibernateHelper.GetCurrentSession();
session.Close();
sw.Stop();
Response.Write(sw.ElapsedTicks + "<br>");
sw.Reset();
sw.Start();
session = NHibernateHelper.GetCurrentSession();
session.Close();
sw.Stop();
Response.Write(sw.ElapsedTicks + "<br>");
執行結果為:

通過幾天來大家對Nhiberate的反映,很多人對它的性能非常的擔心,本文便著手從最直觀的角度和方法中逐步改善nhiberate的性能。改善性能是需要做出很多分析和測試的,本文試圖從最表層的對象入手,以后逐漸增加其他方面的性能分析。希望各位看官莫要著急。
2. 分析:
ISession和ISessionFactory對象的產生,使用,和銷毀對性能的影響。
ISessionFactory對象是線程安全的,它可以被程序的任意線程所適用,但是創建它的性能開銷是比較大的。所以不要頻繁創建ISessionFactroy對象
ISession對象是非線程安全的,創建它的開銷比較小
創建一個ISessionFactory對象的主要流程有:
這期間,包括對多個xml文件的解析和格式驗證,驗證的過程還包括對對象的反射。這些對性能損失非常大。用dottrace跟蹤程序執行,如下
在web應用程序里面,將ISessionFactory對象放到預緩存里面,可以避免頻繁創建ISessionFactory對象。如
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NHibernate;
using NHibernate.Cfg;

namespace WebApp
{
public sealed class NHibernateHelper
{
private const string CurrentSessionKey = "nhibernate.current_session";
private static readonly ISessionFactory sessionFactory;
static NHibernateHelper()
{
string cfgPath = @"E:\my project\nhibernate study\simle 1\NHibernateStudy1\NhibernateSample1\hibernate.cfg.xml";
sessionFactory = new NHibernate.Cfg.Configuration().Configure(cfgPath).BuildSessionFactory();
}
public static ISession GetCurrentSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;
if (currentSession == null)
{
currentSession = sessionFactory.OpenSession();
context.Items[CurrentSessionKey] = currentSession;
}
return currentSession;
}
public static void CloseSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;
if (currentSession == null)
{
// No current session
return;
}
currentSession.Close();
context.Items.Remove(CurrentSessionKey);
}
public static void CloseSessionFactory()
{
if (sessionFactory != null)
{
sessionFactory.Close();
}
}
}
}
用dottrace跟蹤結果為:
從執行時間來看
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
ISession session = NHibernateHelper.GetCurrentSession();
session.Close();
sw.Stop();
Response.Write(sw.ElapsedTicks+"<br>");
sw.Reset();
sw.Start();
session = NHibernateHelper.GetCurrentSession();
session.Close();
sw.Stop();
Response.Write(sw.ElapsedTicks + "<br>");
sw.Reset();
sw.Start();
session = NHibernateHelper.GetCurrentSession();
session.Close();
sw.Stop();
Response.Write(sw.ElapsedTicks + "<br>");
作者:jillzhang
出處:http://jillzhang.cnblogs.com/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
出處:http://jillzhang.cnblogs.com/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。



浙公網安備 33010602011771號