Nhibernate学习之性能改善1

1.学习目标
    通过几天来大家对Nhiberate的反映,很多人对它的性能非常的担心,本文便着手从最直观的角度和方法中逐步改善nhiberate的性能。改善性能是需要做出很多分析和测试的,本文试图从最表层的对象入手,以后逐渐增加其他方面的性能分析。希望各位看官莫要着急。
 2. 分析:
    ISession和ISessionFactory对象的产生,使用,和销毁对性能的影响。 
    ISessionFactory对象是线程安全的,它可以被程序的任意线程所适用,但是创建它的性能开销是比较大的。所以不要频繁创建ISessionFactroy对象
    ISession对象是非线程安全的,创建它的开销比较小 
    创建一个ISessionFactory对象的主要流程有:
      
 这期间,包括对多个xml文件的解析和格式验证,验证的过程还包括对对象的反射。这些对性能损失非常大。用dottrace跟踪程序执行,如下
 
在web应用程序里面,将ISessionFactory对象放到预缓存里面,可以避免频繁创建ISessionFactory对象。如
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  NHibernate;
None.gif
using  NHibernate.Cfg;
None.gif
None.gif
None.gif
namespace  WebApp
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public sealed class NHibernateHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private const string CurrentSessionKey = "nhibernate.current_session";
InBlock.gif        
private static readonly ISessionFactory sessionFactory;
InBlock.gif
InBlock.gif        
static NHibernateHelper()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string cfgPath = @"E:\my project\nhibernate study\simle 1\NHibernateStudy1\NhibernateSample1\hibernate.cfg.xml";
InBlock.gif            sessionFactory 
= new NHibernate.Cfg.Configuration().Configure(cfgPath).BuildSessionFactory();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static ISession GetCurrentSession()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            HttpContext context 
= HttpContext.Current;
InBlock.gif            ISession currentSession 
= context.Items[CurrentSessionKey] as ISession;
InBlock.gif
InBlock.gif            
if (currentSession == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                currentSession 
= sessionFactory.OpenSession();
InBlock.gif                context.Items[CurrentSessionKey] 
= currentSession;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return currentSession;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void CloseSession()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            HttpContext context 
= HttpContext.Current;
InBlock.gif            ISession currentSession 
= context.Items[CurrentSessionKey] as ISession;
InBlock.gif
InBlock.gif            
if (currentSession == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// No current session
InBlock.gif
                return;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            currentSession.Close();
InBlock.gif            context.Items.Remove(CurrentSessionKey);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void CloseSessionFactory()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (sessionFactory != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sessionFactory.Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

用dottrace跟踪结果为:

从执行时间来看
None.gif     System.Diagnostics.Stopwatch sw  =   new  System.Diagnostics.Stopwatch();
None.gif            sw.Start();
None.gif            ISession session 
=  NHibernateHelper.GetCurrentSession();
None.gif            session.Close();
None.gif            sw.Stop();
None.gif            Response.Write(sw.ElapsedTicks
+ " <br> " );
None.gif            sw.Reset();
None.gif            sw.Start();
None.gif            session 
=  NHibernateHelper.GetCurrentSession();
None.gif            session.Close();
None.gif            sw.Stop();
None.gif            Response.Write(sw.ElapsedTicks 
+   " <br> " );
None.gif            sw.Reset();
None.gif            sw.Start();
None.gif            session 
=  NHibernateHelper.GetCurrentSession();
None.gif            session.Close();
None.gif            sw.Stop();
None.gif            Response.Write(sw.ElapsedTicks 
+   " <br> " );
执行结果为:


转载于:https://www.cnblogs.com/jillzhang/archive/2007/04/01/686079.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值