开发者

NHibernate + Fluent long startup time

am new to NHibernate. When performing below test took 11.2 seconds (debug mode) i am seeing this large startup time in all my tests (basically creating the first session takes a ton of time)

setup = Windows 2003 SP2 / Oracle10gR2 latest CPU / ODP.net 2.111.7.20 / FNH 1.0.0.636 / NHibernate 2.1.2.4000 / NUnit 2.5.2.9222 / VS2008 SP1

using System;
using System.Collections;
using System.Data;
using System.Globalization;
using System.IO;
using System.Text;
using System.Data;
using NUnit.Framework;
using System.Collections.Generic;
using System.Data.Common;
using NHibernate;
using log4net.Config;
using System.Configuration;
using FluentNHibernate;

[Test()]
        public void GetEmailById()
        {

            Email result;

            using (EmailRepository repository = new EmailRepository())
            {
                results = repository.GetById(1111);
            }

            Assert.IsTrue(results != null);
        }

//In my Repository

   public T GetById(object id)
        开发者_如何学Go{
            using (var session = sessionFactory.OpenSession())
            using (var transaction = session.BeginTransaction())
            {
                try
                {
                    T returnVal = session.Get<T>(id);
                    transaction.Commit();
                    return returnVal;
                }
                catch (HibernateException ex)
                {
                    // Logging here
                    transaction.Rollback();
                    return null;
                }
            }
        }

The query time is very small. The resulting entity is really small. Subsequent queries are fine.

Its seems to be getting the first session started.

Has anyone else seen something similar?

edit1 :

public RepositoryBase()
{ 
  config = Fluently.Configure()
    .Database(OracleClientConfiguration.Oracle10 
    .ConnectionString(c => c.FromConnectionStringWithKey("DBCONSTRING"))
    .Driver<NHibernate.Driver.OracleDataClientDriver>().ShowSql()) 
    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MYASSEM>()) 
    .BuildConfiguration(); 

  sessionFactory = config.BuildSessionFactory(); 
}


You may take a look at this. Basically, it's about persisting your configuration for the first time, then deserialize it for reuse later.


You shouldn't be newing up a SessionFactory every time you new up a repository.

The SessionFactory should only be created once per application run (including unit tests). It is a very time consuming operation.

If you make that change, your performance should go back to normal/expected performance.


Are you using log4net at the DEBUG level? The NHibernate appender will alsolog at that level unless you configure it differently. It logs a lot of info at the DEBUG level and that's a very common cause of slow stat-up times. Try changing the level for the NHibernate appender only, e.g.:

   <log4net>
    <root>
      <appender-ref ref="SqlServerAppender" />
      <level value="DEBUG" />
    </root>
    <logger name="NHibernate">
      <level value="ERROR"/>
    </logger>
   </log4net>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜