Fluent nHibernate Slow Startup Time

后端 未结 3 874
野性不改
野性不改 2021-01-05 15:13

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

相关标签:
3条回答
  • 2021-01-05 15:35

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

    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
  • 2021-01-05 15:39

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

    0 讨论(0)
提交回复
热议问题