views:

187

answers:

3

Hi all, 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)
        {
            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(); 
}
+1  A: 

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>
Jamie Ide
i was using log4.net (just for testing purposes), i disabled it however it had little effect on the times, thanks anyway
PaRa
+1  A: 

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

vbedegi
I eventually persisted the configuration to disk similar to mentioned in your link. This decreased the load times down to about 2 seconds (from 11.2). Thank you.
PaRa
+1  A: 

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.

Michael Maddox
Your right, thank you this helped
PaRa