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


相关推荐

  • android textwatcher 获取当前控件,android api解析之TextWatcher

    开发android有几年了,但是从来没有整理过,一直是写写写.从今天起开始慢慢整理,总结之处如有错误请指出,谢谢TextWatcher在什么时候会被调用?TextWatcher在edittext内容发生变化时会被调用TextWatcher一共有三个方法beforeTextChanged(CharSequences,intstart,intcount,intafter)在文本变化前调用…

    2022年4月7日
    65
  • mybatis–面向接口编程

    mybatis–面向接口编程

    2021年12月4日
    32
  • 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的「建议收藏」

    【LeetCode】Symmetric Tree 推断一棵树是否是镜像的

    2022年2月1日
    49
  • python爬虫 完整代码

    python爬虫 完整代码这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML图表FLowchart流程图导出与导入导出导入欢迎使用Markdown编辑器你好!这是你第一次使用Markdown编辑器所展示的欢迎页。如果你想学习如何使用Mar

    2022年6月6日
    51
  • CACL联赛第一赛季第一轮比赛排名公布!

    CACL联赛第一赛季第一轮比赛排名公布!亲爱的同学们,CACL联赛第一赛季第一轮,“波士顿房价预测”比赛结束啦!本轮比赛共计31支队伍提交了有效结果。一、比赛结果第一名:浙江大学AI俱乐部第二名:中国海洋大学智能数据分析俱乐部第三名:重庆邮电大学人工智能协会恭喜同学们获得好名次,也非常感谢同学们的热情参与。排名前五的结果代码已在T-CCP社区公布。点击查看>>>另外排名前五的战队会颁发获奖证书,第六名及以后…

    2025年6月26日
    2
  • js动画效果_js动画函数

    js动画效果_js动画函数一、setTimeoutVS.requestAnimationFrame传统js动画实现一般使用setTimeout/setInterval等定时方式执行一个动画更新操作,但这种方式在使用中存在一些问题。动画帧间隔interval问题大部分显示器的刷新频率是16.7ms,如果setTimeout的interval小于这个值,就会出现绘制的帧无法在显示器上展现的问题,好像被吞掉了一样。另

    2022年10月15日
    4

发表回复

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

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