NHibernate Current Session Context Problem

15,864

Try this:

try
{
    if (NHibernate.Context.CurrentSessionContext.HasBind(sessionFactory))
    {
        session = sessionFactory.GetCurrentSession();
    }
    else
    {
        session = sessionFactory.OpenSession(this.dateTimeInterceptor);
        NHibernate.Context.CurrentSessionContext.Bind(session);
    }
}
catch (Exception ex)
{
    Debug.Write(ex.Message);
    throw;
}
Share:
15,864
BenCr
Author by

BenCr

Silverlight Developer/Martyr

Updated on June 14, 2022

Comments

  • BenCr
    BenCr almost 2 years

    I've recently moved from using an ISession directly to a wrapped ISession, Unit-of-Work type pattern.

    I used to test this using SQL Lite (in-memory). I've got a simple helper class that configures my SessionFactory, created an ISession and then built the schema using SchemaExport and then returned my ISession and the schema lived until I closed the session. I've changed this slightly so that I now configure a SessionFactory, create an ISession, build the schema, and pass the factory to my NHibernateUnitOfWork and return that to my test.

    var databaseConfiguration =
                    SQLiteConfiguration.Standard.InMemory()
                    .Raw("connection.release_mode", "on_close")
                    .Raw("hibernate.generate_statistics", "true");
    
                var config = Fluently.Configure().Database(databaseConfiguration).Mappings(
                    m =>
                    {
                        foreach (var assembly in assemblies)
                        {
                            m.FluentMappings.AddFromAssembly(assembly);
                            m.HbmMappings.AddFromAssembly(assembly);
                        }
                    });
    
                Configuration localConfig = null;
                config.ExposeConfiguration(x =>
                    {
                        x.SetProperty("current_session_context_class", "thread_static"); // typeof(UnitTestCurrentSessionContext).FullName);
                        localConfig = x;
                    });
    
                var factory = config.BuildSessionFactory();
                ISession session = null;
    
                if (openSessionFunction != null)
                {
                    session = openSessionFunction(factory);
                }
    
                new SchemaExport(localConfig).Execute(false, true, false, session.Connection, null);
    
                UnitTestCurrentSessionContext.SetSession(session);
    
                var unitOfWork = new NHibernateUnitOfWork(factory, new NHibernateUTCDateTimeInterceptor());
                return unitOfWork;
    

    Internally, NHibernateUnitOfWork needs to get the ISession which was used to create the schema or the in-memory database won't actually have a schema, so this is the method it calls to get the ISession.

    private ISession GetCurrentOrNewSession()
            {
                if (this.currentSession != null)
                {
                    return this.currentSession;
                }
    
                lock (this)
                {
                    if (this.currentSession == null)
                    {
                        // get an existing session if there is one, otherwise create one
                        ISession session;
                        try
                        {
                            session = this.sessionFactory.GetCurrentSession();
                        }
                        catch (Exception ex)
                        {
                            Debug.Write(ex.Message);
                            session = this.sessionFactory.OpenSession(this.dateTimeInterceptor);
                        }
    
                        this.currentSession = session;
                        this.currentSession.FlushMode = FlushMode.Never;
                    }
                }
    

    The problem is that this.sessionFactory.GetCurrentSession always throws an exception saying that the ICurrentSessionContext isn't registered.

    I've tried loads of different ways to set the property and different values (as you can see above, "thread_static" and my own ICurrentSessionContext) but none seem to work.

    Anyone got any advice

  • BenCr
    BenCr about 13 years
    This seems to have worked, I'll confirm properly later today and accept. Thanks
  • CyberMonk
    CyberMonk over 12 years
    You are missing a parenthesis at the end of the 'if' statement.