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>");
执行结果为:


作者:jillzhang
出处:http://jillzhang.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted @ 2007-04-01 17:11 Robin Zhang 阅读(3747) 评论(6)  编辑 收藏 网摘 所属分类: ORM

  回复  引用    
#1楼2007-04-01 18:06 | ofei[未注册用户]
不错不错 期待下更多篇
没dottrace的注册码 55555

  回复  引用  查看    
#2楼2007-04-01 19:35 | ^-^Roping.Zong      
ISessionFactory这个最好在web程序对于每一个页面结束请求后都关闭Session!
using System;
using System.Web;

namespace NHibernateHelper
{
/// <summary>
/// NHHttpModule 的摘要说明。
/// </summary>
public class NHHttpModule : IHttpModule
{
public void Dispose(){}

public void Init(HttpApplication context)
{
context.EndRequest += new EventHandler(EndRequest);
}

public void EndRequest(Object sender, EventArgs e)
{
DB.CloseSession();
}
}
}

  回复  引用  查看    
#4楼2007-04-01 21:32 | Zhongkeruanjian      
呵呵,有道理,我现在在研究NH的2级缓存,如果2级缓存用不上,NH的性能还是没办法上去。由于诸多原因,我现在希望要实体在更新时将此实体的第二级缓存失效的策略,但是NH好像没有这样的配置,默认是更新缓存。。。现在只是找到了Persister的一个 IsCacheInvalidationRequired属性,让它默认为True,这样是更新缓存时失效,但是还没有太多的测试,不知有没有问题。



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 686079




相关文章:

相关链接: