NHibernate +流畅启动时间长
我是 NHibernate 新手。执行以下测试时花费了 11.2 秒(调试模式) 我在所有测试中都看到如此长的启动时间(基本上创建第一个会话需要大量时间)
安装 = Windows 2003 SP2 / Oracle10gR2 最新CPU / ODP.net 2.111.7.20 / FNH 1.0.0.636 / NHibernate 2.1.2.4000 / NUnit 2.5.2.9222 / VS2008 SP1
using System;
using System.Collections;
using System.Data;
using System.Globalization;
using System.IO;
using System.Text;
using System.Data;
using NUnit.Framework;
using System.Collections.Generic;
using System.Data.Common;
using NHibernate;
using log4net.Config;
using System.Configuration;
using FluentNHibernate;
[Test()]
public void GetEmailById()
{
Email result;
using (EmailRepository repository = new EmailRepository())
{
results = repository.GetById(1111);
}
Assert.IsTrue(results != null);
}
//In my Repository
public T GetById(object id)
{
using (var session = sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
try
{
T returnVal = session.Get<T>(id);
transaction.Commit();
return returnVal;
}
catch (HibernateException ex)
{
// Logging here
transaction.Rollback();
return null;
}
}
}
查询时间非常小。由此产生的实体非常小。后续查询就没问题了。
第一次会议似乎正在开始。
还有其他人见过类似的东西吗?
编辑1:
public RepositoryBase()
{
config = Fluently.Configure()
.Database(OracleClientConfiguration.Oracle10
.ConnectionString(c => c.FromConnectionStringWithKey("DBCONSTRING"))
.Driver<NHibernate.Driver.OracleDataClientDriver>().ShowSql())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<MYASSEM>())
.BuildConfiguration();
sessionFactory = config.BuildSessionFactory();
}
am new to NHibernate. When performing below test took 11.2 seconds (debug mode)
i am seeing this large startup time in all my tests (basically creating the first session takes a ton of time)
setup = Windows 2003 SP2 / Oracle10gR2
latest CPU / ODP.net 2.111.7.20 / FNH
1.0.0.636 / NHibernate 2.1.2.4000 / NUnit 2.5.2.9222 / VS2008 SP1
using System;
using System.Collections;
using System.Data;
using System.Globalization;
using System.IO;
using System.Text;
using System.Data;
using NUnit.Framework;
using System.Collections.Generic;
using System.Data.Common;
using NHibernate;
using log4net.Config;
using System.Configuration;
using FluentNHibernate;
[Test()]
public void GetEmailById()
{
Email result;
using (EmailRepository repository = new EmailRepository())
{
results = repository.GetById(1111);
}
Assert.IsTrue(results != null);
}
//In my Repository
public T GetById(object id)
{
using (var session = sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
try
{
T returnVal = session.Get<T>(id);
transaction.Commit();
return returnVal;
}
catch (HibernateException ex)
{
// Logging here
transaction.Rollback();
return null;
}
}
}
The query time is very small. The resulting entity is really small. Subsequent queries are fine.
Its seems to be getting the first session started.
Has anyone else seen something similar?
edit1 :
public RepositoryBase()
{
config = Fluently.Configure()
.Database(OracleClientConfiguration.Oracle10
.ConnectionString(c => c.FromConnectionStringWithKey("DBCONSTRING"))
.Driver<NHibernate.Driver.OracleDataClientDriver>().ShowSql())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<MYASSEM>())
.BuildConfiguration();
sessionFactory = config.BuildSessionFactory();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以查看 这个。基本上,它是关于第一次保留您的配置,然后将其反序列化以供以后重用。
You may take a look at this. Basically, it's about persisting your configuration for the first time, then deserialize it for reuse later.
您不应该在每次新建存储库时都新建 SessionFactory。
每次应用程序运行(包括单元测试)时,SessionFactory 只应创建一次。这是一个非常耗时的操作。
如果您进行更改,您的表现应该会恢复到正常/预期的表现。
You shouldn't be newing up a SessionFactory every time you new up a repository.
The SessionFactory should only be created once per application run (including unit tests). It is a very time consuming operation.
If you make that change, your performance should go back to normal/expected performance.
您是否在 DEBUG 级别使用 log4net? NHibernate 附加程序也将在该级别进行日志记录,除非您对其进行不同的配置。它在调试级别记录大量信息,这是启动时间缓慢的一个非常常见的原因。尝试仅更改 NHibernate 附加程序的级别,例如:
Are you using log4net at the DEBUG level? The NHibernate appender will alsolog at that level unless you configure it differently. It logs a lot of info at the DEBUG level and that's a very common cause of slow stat-up times. Try changing the level for the NHibernate appender only, e.g.: