Fluent nHibernate 启动时间慢
我正在使用 Fluent NHibernate,我喜欢它! 我遇到一个小问题:启动时间约为 10 秒,我不知道如何优化 Fluent nHibernate 为了减少启动时间的问题,我将其放在一个线程上。
有人可以告诉这个问题的解决方案吗?并回复修改以下代码以提高性能?
我在上面看到过这样的事情: http://nhforge.org /blogs/nhibernate/archive/2009/03/13/an-improvement-on-sessionfactory-initialization.aspx 但我不知道如何使其与 Fluent nHibernate 一起工作。
我的代码是这样的:
public static ISession ObterSessao()
{
System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest;
string ConnectionString = ConfigurationHelper.LeConfiguracaoWeb("EstoqueDBNet"); // My Connection string goes here
var config = Fluently.Configure()
.Database(FluentNHibernate.Cfg.Db.MySQLConfiguration.Standard.ConnectionString(ConnectionString));
config.Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()));
var session = config
.BuildSessionFactory()
.OpenSession();
System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
return session;
}
I am using Fluent NHibernate and I like it !
I am having a little issue: The startup time is about 10 seconds and I don´t know how to optimize Fluent nHibernate
To make this startup time less problematic, I put it on a Thread.
Could somebody tell a solution to this? And reply with the code below modified to make the performance improvement?
I saw something like this on:
http://nhforge.org/blogs/nhibernate/archive/2009/03/13/an-improvement-on-sessionfactory-initialization.aspx
but I don´t know how to make this work together with Fluent nHibernate.
My code is like this:
public static ISession ObterSessao()
{
System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest;
string ConnectionString = ConfigurationHelper.LeConfiguracaoWeb("EstoqueDBNet"); // My Connection string goes here
var config = Fluently.Configure()
.Database(FluentNHibernate.Cfg.Db.MySQLConfiguration.Standard.ConnectionString(ConnectionString));
config.Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()));
var session = config
.BuildSessionFactory()
.OpenSession();
System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
return session;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需构建一次配置。目前,每次获得会话时,您都会构建新的配置。
You only need to build the configuration once. At the moment you're building a new configuration every time you get a session.
首先,不要弄乱线程优先级,如果你所做的任何事情都会使它变慢。
其次,就像 Phill 所说,您需要缓存 SessionFactory,否则每次需要会话对象时都将重建配置。
您可以执行类似的操作,或者将
if
中的代码移至类的静态构造函数中:First of all, don't mess with the thread priority, if anything what you're doing will make it slower.
Second, like Phill said, you need to cache your SessionFactory or you'll be rebuilding the configuration every time you need a session object.
You could do something like this, or move the code in the
if
into the class' static constructor:菲尔有正确的答案,但要进一步了解一下 http://nhibernate.info/blog/2009/03/13/an-improvement-on-sessionfactory-initialization.html 用于将 NHibernate 配置序列化到文件中,因此您不需要不必每次启动应用程序时都重建它。这可能会快一点,也可能不会快一点,具体取决于各种因素(主要是您拥有的映射数量) - 据此,有关于 NHibernate 与 Fluent NHibernate 启动性能的数据吗?
只是强调一下(基于您回答中的一些后续问题) -comments),您应该序列化 (NHibernate.Cfg.)Configuration 对象,而不是 SessionFactory。
然后,您可以在创建 FluentConfiguration 时使用
Fluently.Configure(Configuration cfg)
重载来注入配置(而不是让它自动为您构建配置)。Phill has the right answer, but to take it a little further take a look at http://nhibernate.info/blog/2009/03/13/an-improvement-on-sessionfactory-initialization.html for serializing the NHibernate configuration to a file, so you don't have to rebuild it every time you start the app. This may or may not be a little faster depending on various factors (principally, number of mappings you have) - pursuant to this, Is there any data on NHibernate vs Fluent NHibernate startup performance?
Just to emphasize (based on some of your followup qns in answer-comments), you should be serializing the (NHibernate.Cfg.)Configuration object, not the SessionFactory.
Then, you use the
Fluently.Configure(Configuration cfg)
overload to inject the config when creating your FluentConfiguration (instead of having it automatically build one for you).