NHibernate当前会话上下文问题 [英] NHibernate Current Session Context Problem

查看:178
本文介绍了NHibernate当前会话上下文问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从一个ISession直接转到了一个包装的ISession,工作单元类型模式。

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



<$ ()
。$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$ .generate_statistics,true);数据库(databaseConfiguration).Mappings(
m =>
{
foreach(程序集中的var程序集)

$ b var config = Fluently.Configure b {
m.FluentMappings.AddFromAssembly(assembly);
m.HbmMappings.AddFromAssembly(assembly);
}
});

配置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);

$ b $新建SchemaExport(localConfig).Execute(false,true,false,session.Connection,null);

UnitTestCurrentSessionContext.SetSession(session);

var unitOfWork = new NHibernateUnitOfWork(factory,new NHibernateUTCDateTimeInterceptor());
return unitOfWork;

在内部,NHibernateUnitOfWork需要获取用于创建模式或内存数据库的ISession实际上不会有一个模式,所以这是它调用获取ISession的方法。



prerivate私人ISession GetCurrentOrNewSession()$如果(this.currentSession!= null)
{
返回this.currentSession;


lock(this)
{
if(this.currentSession == null)
{
//获取一个已经存在的会话如果有,则创建一个
ISession会话;
尝试
{
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;




$ p $问题是这个这个.sessionFactory.GetCurrentSession 总是抛出一个异常,说明 ICurrentSessionContext 没有注册。

我已经尝试了加载不同的方法来设置属性和不同的值(正如你上面看到的,thread_static和我自己的 ICurrentSessionContext


解决

  try 
{
if(NHibernate.Context .CurrentSessionContext.HasBind(sessionFactory))
{
session = sessionFactory.GetCurrentSession();
}
else
{
session = sessionFactory.OpenSession(this.dateTimeInterceptor);
NHibernate.Context.CurrentSessionContext.Bind(session);

$ b catch(Exception ex)

Debug.Write(ex.Message);
throw;
}


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

解决方案

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

这篇关于NHibernate当前会话上下文问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆