我终于弄清楚了我的代码出了什么问题,但是我不确定如何解决它。我有一些在单独的线程上运行的后台进程,这些进程执行一些数据库维护任务。这是正在发生的事的一个例子:
//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
。当它运行时,我在另一个线程上启动ShortRunningTask
。 ShortRunningTask
完成并关闭其 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/