multithreading - 在NHibernate中同时打开多个 session

标签 multithreading nhibernate

我终于弄清楚了我的代码出了什么问题,但是我不确定如何解决它。我有一些在单独的线程上运行的后台进程,这些进程执行一些数据库维护任务。这是正在发生的事的一个例子:

//Both processes share the same instance of ISessionFactory
//but open separate ISessions

//This is running on it's own thread
public void ShortRunningTask()
{
    using(var session = _sessionFactory.OpenSession())
    {
        //Do something quickly here
        session.Update(myrecord);
    }
}

//This is running on another thread
public void LongRunningTask()
{
    using(var session = _sessionFactory.OpenSession())
    {
        //Do something long here
    }
}

假设我先开始LongRunningTask。当它运行时,我在另一个线程上启动ShortRunningTaskShortRunningTask完成并关闭其 session 。一旦LongRunningTask完成,它将尝试对它创建的 session 执行某些操作,但是会抛出一个错误消息,指出该 session 已经关闭。

显然,正在发生的事情是ISessionFactory.OpenSession()不尊重我已经打开了2个单独的 session 的事实。关闭在ShortRunningTask中打开的 session 也会在LongRunningTask中关闭 session 。如何解决此问题?请帮忙!

谢谢!

更新

因此,显然每个人都认为我的解决方案是完全错误的。所以这是我正在使用的配置:
_sessionFactory = Fluently.Configure()
                  .Database(
                  FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008
                  .ConnectionString(db => db.Is(
WikipediaMaze.Core.Properties.Settings.Default.WikipediaMazeConnection)))
                  .Mappings(m => m.FluentMappings.AddFromAssemblyOf<IRepository>())
                  .BuildSessionFactory();

我没有在xml文件中进行配置。应该有吗?我想念的是什么。这是打开多个 session 失败的另一个示例:
public void OpenMultipleSessionsTest()
{
    using(var session1 = _sessionFactory.OpenSession())
    {
        var user = session1.Get<Users>().ById(1);
        user.Name = "New Name";

        using(var session2 = _sessionFactory.OpenSession())
        {
            //Do some other operation here.
        }

        session1.Update(user);
        session1.Flush(); // Throws error 'ISession already closed!'
    }
}

最佳答案

我想出了解决问题的方法。我将SessionFactory设置为单例,使其成为[ThreadStatic],如下所示:

[ThreadStatic]
private ISessionFactory _sessionFactory;

[ThreadStatic]
private bool _isInitialized;

public ISessionFactory SessionFactory
{
    get
    {
        if(!_isInitialized)
        {
            //Initialize the session factory here
        }
    }
}

问题的症结在于在同一ISessionFactory的不同线程上创建 session 是有问题的。 ISessionFactory不喜欢同时打开多个ISession。关闭一个,将自动关闭所有其他打开的窗口。使用[ThreadStatic]属性为每个线程创建一个单独的ISessionFactory。这使我可以在每个线程上打开和关闭ISession,而不会影响其他线程。

关于multithreading - 在NHibernate中同时打开多个 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1580087/

相关文章:

java - 并发问题 - 从 Java 多线程服务器下载的同一文件的大小不同

c# - 如何使用 NH 生成 "select count(*)as y from (select distinct x, y from tableName) as t"?

nhibernate - Fluent NHibernate Syscache2 缓存过期

asp.net - 如何强制 NHibernate 识别不是通过 NHibernate 进行的数据库更改

c# - 无法将类型 'System.Collections.ArrayList' 的对象强制转换为类型 'System.Collections.Generic.IEnumerable'

java - 等待传感器数据 android - 我们总是需要一个新线程吗?

c# - 线程大量线程

c++ - 没有重载函数 "AfxBeginThread"的实例与参数列表匹配

Java线程中断仅等待、加入和 sleep

c# - NHibernate:在集合上调用 .RemoveAll