NHibernate的会话和多线程 [英] nHibernate session and multithreading

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

问题描述

我有很多是采用了NHibernate的会议上,它的工作持久性调用的方法,是好的。但我需要重构这个方法,提取循环中有一个内容,多线程的原因的方法。然后,我创建了一个类用这种方法。它就像一个正常重构,但这种方法调用中NHibernate会话坏了,没有上下文,我没有在任何时刻定稿。 的NHibernate的问题,多线程?即使我只有一个多线程执行,我也有同样的问题。

I had a method with a lot of persistence calls that used a nHibernate session, it worked, was alright. But I needed to refactor this method, extracting a method from a content inside a loop, for multithread reasons. Then I created an class with this method. It is like a normal refactoring, but the nHibernate session inside this method call is broken, without context, I didn't finalize it at any moment. Has nHibernate problems with multithreading? Even when I have only one more thread executing, I have the same problem.

我使用NHibernate的会议通过SessionFactory并Facade模式,这意味着,会话不是这些对象的一个​​领域,它是在SessionFactory的全球性的。

I use nHibernate Session through a SessionFactory and Façade pattern, it means, the session is not a field of these objects, it is global at SessionFactory.


使得它更有点清楚的:

BEFORE:

Method()
{
... persistence calls
foreach(Thing..)
{
...persistence calls for each thing (1)
}
...
}

AFTER:

Method()
{
... persistence calls
foreach(Thing..)
{
create a thingResolver object with some data
open a new thread with thingResolver.Method (1)
starts this thread
}
.. waits for finishing threads and continues
}


我们的NHibernate的会议厂是线程感知和存储/每个线程检索的nHibernate会话。现在工作很好;)

Our nHibernate Session Factory is thread-aware, and stores/retrieves nHibernate session per thread. It is working nicely now ;)

推荐答案

会话为不是线程安全在NHibernate的按设计
所以应该没问题,只要你有一个的仅由一个线程

Sessions are not thread safe in NHibernate by design. So it should be ok as long as you have a session used by only one thread.

我不知道是什么你thingResolver做,但如果这样做对的相同的你在原始线程创建的会话持久性的一些电话 - 这很可能是你的问题的原因,您可以创建一个单独的会话你新的线程,因此,如果我的假设是真的,那将是每个线程的会话。

I'm not sure what you're thingResolver does, but if it does some persistance calls on the same session you've created in the originating thread - this most probably the cause of your problems, you could create a separate session in your new thread so that it would be a session per thread if my assumption is true.

NHibernate的参考有它在10.2节

NHibernate reference has it in section 10.2

http://nhibernate.info/doc/nh/en/index html的#交易

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

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