NHibernate Current Session Context Problem

心不动则不痛 提交于 2019-12-01 01:30:06

问题


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


回答1:


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;
}


来源:https://stackoverflow.com/questions/5914405/nhibernate-current-session-context-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!