asp.net - nhibernate 配置和构建sessionfactory 时间

标签 asp.net nhibernate initialization

我使用 Nhibernate 作为 ASP.NET 应用程序的 OR/M 工具,但启动性能确实令人沮丧。部分问题肯定是我缺乏理解,但我已经尝试了相当多的努力(理解肯定在提高),但仍然一无所获。

目前 ANTS 分析器显示,Configure() 需要 13-18 秒,BuildSessionFActory() 需要大约 5 秒。根据我的阅读,这些时间实际上可能相当不错,但他们通常谈论数百个映射实体......这个项目只有 10 个。

我已将所有映射文件合并到一个 hbm 映射文件中,这确实有所改善,但仅限于上面提到的时间...

我想,是否有任何“年轻玩家的陷阱”经常被错过...明显的“我做了这个/你启用了那个/排除文件 x/将文件 y 标记为 z”等等...

我将尝试序列化配置以避免Configure()阶段,但我认为对于那么多实体来说该部分不应该那么长,因此本质上会隐藏当前的问题......

如果有必要,我会发布源代码或配置,但我不确定到底要放入什么......

非常感谢!

编辑(更多信息)
我还要补充一点,一旦完成,每个页面都非常快......

配置代码-hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration"
    type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <property name="connection.connection_string_name">MyAppDEV</property>
      <property name="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache</property>
      <property name="cache.use_second_level_cache">true</property>
      <property name="show_sql">false</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
      <property name="current_session_context_class">managed_web</property>

      <mapping assembly="MyApp.Domain"/>
    </session-factory>
  </hibernate-configuration>
</configuration>

我的 SessionManager 类在每个请求的 HttpModule 中绑定(bind)和解除绑定(bind)

Imports NHibernate
Imports NHibernate.Cfg

Public Class SessionManager

    Private ReadOnly _sessionFactory As ISessionFactory

    Public Shared ReadOnly Property SessionFactory() As ISessionFactory
        Get
            Return Instance._sessionFactory
        End Get
    End Property

    Private Function GetSessionFactory() As ISessionFactory
        Return _sessionFactory
    End Function

    Public Shared ReadOnly Property Instance() As SessionManager
        Get
            Return NestedSessionManager.theSessionManager
        End Get
    End Property

    Public Shared Function OpenSession() As ISession
        Return Instance.GetSessionFactory().OpenSession()
    End Function

    Public Shared ReadOnly Property CurrentSession() As ISession
        Get
            Return Instance.GetSessionFactory().GetCurrentSession()
        End Get
    End Property

    Private Sub New()
        Dim configuration As Configuration = New Configuration().Configure()
        _sessionFactory = configuration.BuildSessionFactory()
    End Sub

    Private Class NestedSessionManager
        Friend Shared ReadOnly theSessionManager As New SessionManager()
    End Class

End Class

编辑 2(log4net 结果)

将发布中间有部分时间的内容,并删除其余部分...

2010-03-30 23:29:40,898 [4] INFO  NHibernate.Cfg.Environment [(null)] - Using reflection optimizer
2010-03-30 23:29:42,481 [4] DEBUG NHibernate.Cfg.Configuration [(null)] - dialect=NHibernate.Dialect.MsSql2005Dialect

...

2010-03-30 23:29:42,501 [4] INFO  NHibernate.Cfg.Configuration [(null)] - Mapping resource: MyApp.Domain.Mappings.hbm.xml
2010-03-30 23:29:43,342 [4] INFO  NHibernate.Dialect.Dialect [(null)] - Using dialect: NHibernate.Dialect.MsSql2005Dialect
2010-03-30 23:29:50,462 [4] INFO  NHibernate.Cfg.XmlHbmBinding.Binder [(null)] - Mapping class: 

...

2010-03-30 23:29:51,353 [4] DEBUG NHibernate.Connection.DriverConnectionProvider [(null)] - Obtaining IDbConnection from Driver
2010-03-30 23:29:53,136 [4] DEBUG NHibernate.Connection.ConnectionProvider [(null)] - Closing connection

最佳答案

尝试更改 NHibernate 记录器的日志记录级别。看起来您已将其设置为 DEBUG,这可能适合您的应用程序。但是会导致 NHibernate 执行大量日志记录。

<log4net>
  ....
  <logger name="NHibernate">
      <level value="ERROR"/>
  </logger>
</log4net>

关于asp.net - nhibernate 配置和构建sessionfactory 时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2544799/

相关文章:

c# - 如何从 Entity Framework 中的存储过程中获取数据作为数据集

c# - 防止 "Edit"用户控件上的 CSS 更改?

wpf - NHibernate的可共混性/MVVM

c++ - C++中指针初始化

c++ - `a?b:c` 的操作数可以有不同的类型吗?最好的选择是什么?

asp.net - 如何在数据库中保存多张图像?

c# - 我们可以在一个表单中有多个提交类型按钮吗?

c# - 如何查询NHibernate的特定类型?

c# - 人是聚合根吗?

Python For循环在While循环中,以特定值开始for循环