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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Pycharm入门使用教程(for python)「建议收藏」

    Pycharm入门使用教程(for python)「建议收藏」1.新建项目打开2.选择解释器选择了合适自己python代码的解释器,编译才不会出错settings-project-Interpreter-选择合适自己的python3.x或者python2.x3.安装模块settings-project-Interpreter-右边‘+’-选择自己想要安装的模块4.设置字体settings-Appearance&Behaviorsettings-Editor-colorscheme-scheme-选择自己想要的工作区域风格5.设置编码可解

    2022年8月28日
    2
  • Latex如何插入图片[通俗易懂]

    Latex如何插入图片[通俗易懂]在写报告或论文的过程中,几乎不可避免的要插入一些图片,并且根据不同情况及要求进行排版,例如如何插入单个图片、一行插入两张图片、插入两行两列图片等等。在此,汇总一下各种插入图片的方法。插入单个图片这种情况是最简单的了,当然使用latex排版时也要注意一些问题,比如相关宏包的引用、图片存放路径、图片尺寸及位置调整等,下面给出一例子。代码:\documentclass{article}\use…

    2022年6月10日
    52
  • android之存储篇_ContentProvider存储

    ContentProvider是安卓平台中,在不同应用程序之间实现数据共享的一种机制。一个应用程序如果需要让别的程序可以操作自己的数据,即可采用这种机制。并且此种方式忽略了底层的数据存储实现,ContentProvider提供了一种统一的通过Uri实现数据操作的方式。其步骤为:  1. 在当前应用程序中定义一个ContentProvider。  2. 在当前应用程序的AndroidMani

    2022年3月10日
    52
  • ubuntu过期版本软件源[通俗易懂]

    ubuntu过期版本软件源[通俗易懂]ubuntu10.04软件源国内源,参考:https://blog.csdn.net/snaking616/article/details/52966634debhttp://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu/lucidmainrestricteduniversemultiversedebhttp://mi…

    2022年10月14日
    3
  • ssl通关的概念(一个)

    ssl通关的概念(一个)

    2022年1月6日
    45
  • pycha2021.5激活码[在线序列号][通俗易懂]

    pycha2021.5激活码[在线序列号],https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月20日
    58

发表回复

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

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