NHibernate当前会话上下文问题

时间:2011-05-06 16:38:05

标签: nhibernate fluent-nhibernate nhibernate-3

我最近已经从使用ISession直接转移到包装的ISession,工作单元类型模式。

我曾经使用SQL Lite(内存中)测试它。我有一个简单的帮助器类,它配置我的SessionFactory,创建一个ISession,然后使用SchemaExport构建模式,然后返回我的ISession,模式一直存在,直到我关闭会话。我稍微改了一下,以便我现在配置一个SessionFactory,创建一个ISession,构建模式,并将工厂传递给我的NHibernateUnitOfWork并将其返回到我的测试中。

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;

在内部,NHibernateUnitOfWork需要获取用于创建模式的ISession,或者内存数据库实际上不具有模式,因此这是它调用以获取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;
                }
            }

问题是this.sessionFactory.GetCurrentSession总是抛出异常,说明ICurrentSessionContext未注册。

我尝试了很多不同的方法来设置属性和不同的值(如上所示,“thread_static”和我自己的ICurrentSessionContext),但似乎都没有。

任何人都有任何建议

1 个答案:

答案 0 :(得分:15)

试试这个:

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;
}
相关问题