Free nHibernate Slow Startup Time

I use Fluent NHibernate and I like it! I have a small problem: 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 in a stream.

Can anyone tell about this? And answer with modified code to improve performance?

I saw something like this: 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 Fluent nHibernate.

My code looks 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; } 
+6
fluent-nhibernate
source share
3 answers

Phil has the correct answer, but to take a quick look at http://nhibernate.info/blog/2009/03/13/an-improvement-on-sessionfactory-initialization.html to serialize the NHibernate configuration for the file, so you do not need to rebuild its every time you launch the application. It may or may not be a little faster depending on various factors (mainly, the number of mappings you display) - according to this, is there any launch data for NHibernate vs Fluent NHibernate?

To emphasize (based on some of your subsequent qns in the answers to the answers), you should serialize the configuration object (NHibernate.Cfg.) And not SessionFactory.

Then you use the Fluently.Configure(Configuration cfg) overload Fluently.Configure(Configuration cfg) to inject the configuration when you create the FluentConfiguration (instead of automatically creating one for you).

+2
source share

You only need to create the configuration once. You are currently creating a new configuration every time you get a session.

+5
source share

First of all, do not mess with the priority of the thread if something that you do will make it slower.

Secondly, as Phill said, you need to cache your SessionFactory, or you will rebuild the configuration every time you need a session object.

You can do something like this or move the code in if to the static constructor of the class:

 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(); } 
+2
source share

All Articles