Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent nHibernate Slow Startup Time

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;
    }
like image 454
Tony Avatar asked Dec 07 '10 23:12

Tony


2 Answers

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

like image 96
Phill Avatar answered Sep 25 '22 01:09

Phill


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();
}
like image 32
joshperry Avatar answered Sep 27 '22 01:09

joshperry