Fluent nHibernate Slow Startup Time

▼魔方 西西 提交于 2019-12-30 06:42:06

问题


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;
    }

回答1:


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).




回答2:


You only need to build the configuration once. At the moment you're building a new configuration every time you get a session.




回答3:


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:

private static SessionFactory _factory = null;
public static ISession ObterSessao()        
{
    if(_factory == null) {
        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()));

        _factory = config.BuildSessionFactory();
    }

    return _factory.OpenSession();
}


来源:https://stackoverflow.com/questions/4382870/fluent-nhibernate-slow-startup-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!