Nhibernate学习之性能改善1

Nhibernate学习之性能改善1Nhibernate学习之性能改善1

大家好,又见面了,我是你们的朋友全栈君。

1.学习目标

    通过几天来大家对Nhiberate的反映,很多人对它的性能非常的担心,本文便着手从最直观的角度和方法中逐步改善nhiberate的性能。改善性能是需要做出很多分析和测试的,本文试图从最表层的对象入手,以后逐渐增加其他方面的性能分析。希望各位看官莫要着急。

 2. 分析:

    ISession和ISessionFactory对象的产生,使用,和销毁对性能的影响。 

    ISessionFactory对象是线程安全的,它可以被程序的任意线程所适用,但是创建它的性能开销是比较大的。所以不要频繁创建ISessionFactroy对象

    ISession对象是非线程安全的,创建它的开销比较小 

    创建一个ISessionFactory对象的主要流程有:

      
Nhibernate学习之性能改善1

 这期间,包括对多个xml文件的解析和格式验证,验证的过程还包括对对象的反射。这些对性能损失非常大。用dottrace跟踪程序执行,如下

 
Nhibernate学习之性能改善1

在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跟踪结果为:


Nhibernate学习之性能改善1

从执行时间来看

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>

);

执行结果为:


Nhibernate学习之性能改善1

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107093.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • JAVA事件适配器的作用

    JAVA事件适配器的作用适配器 其实从接口事件演变尔来的。相当于触发器,说简单点,它就是一些动作。例如说鼠标的按下点击还有键盘等等。适配器就是实现接口事件的类不过,不是真的实现,只是空实现,没有具体的方法体。适配器主要是为了方便程序员操作,避免了代码的重复性。只要一个对象或者属性添加了这个适配器,那么它就会监视这对象或属性。例如说一个按纽。按纽添加一个MouseAdap

    2022年5月4日
    53
  • 常用的JS页面跳转代码调用大全

    常用的JS页面跳转代码调用大全一、常规的JS页面跳转代码1、在原来的窗体中直接跳转用<scripttype="text/javascript">window.location.href=&q

    2022年7月1日
    65
  • 12个MySQL慢查询的原因分析「建议收藏」

    12个MySQL慢查询的原因分析「建议收藏」12个MySQL慢查询的原因分析

    2022年10月14日
    2
  • idea永久激活码(破解版激活)

    idea永久激活码(破解版激活),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月17日
    90
  • CentOS7安装详解

    CentOS7安装详解本文基于vmwareworkstations进行CentOS7安装过程展示,关于vmwareworkstations安装配置本人这里不再介绍,基本过程相当于windows下安装个软件而已。1、打

    2022年8月3日
    13
  • Windows ping TCP端口工具之tcping「建议收藏」

    Windows ping TCP端口工具之tcping「建议收藏」ping这个小工具大家都非常熟悉,但是他不能ping端口,当我们需要知道目的地址的某端口是否开放时,这时需要用到这个tcping小工具了,Windows没有自带这个小工具,需要自己下载下来,放到指定目录里面。下载地址:点击打开链接X64下载链接放到C:\Windows\System32这个文件夹下现在在测试一下…

    2022年6月23日
    147

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号