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;
}
You only need to build the configuration once. At the moment you're building a new configuration every time you get a session.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With