C# nHibernate 会话和多线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/242961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:32:25  来源:igfitidea点击:

nHibernate session and multithreading

c#multithreadingnhibernatesession

提问by Victor Rodrigues

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 会话的持久性调用,它有效,没问题。但是由于多线程原因,我需要重构这个方法,从循环内的内容中提取一个方法。然后我用这个方法创建了一个类。就像是正常的重构,但是这个方法调用里面的nHibernate session坏了,没有上下文,我也没有任何时候敲定。多线程有 nHibernate 问题吗?即使我只有一个线程在执行,我也有同样的问题。

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.

我通过 SessionFactory 和 Fa?ade 模式使用 nHibernate Session,这意味着,会话不是这些对象的字段,它在 SessionFactory 是全局的。



Making it a little bit more clear:

让它更清楚一点:

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
}


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

我们的 nHibernate 会话工厂是线程感知的,并且存储/检索每个线程的 nHibernate 会话。它现在运行良好;)

采纳答案by axk

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

根据设计,会话在 NHibernate中不是线程安全的。所以只要你有一个只有一个线程使用会话就可以了

I'm not sure what you're thingResolver does, but if it does some persistance calls on the samesession 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.

我不确定你是 whatResolver 做什么,但如果它在你在原始线程中创建的同一个会话上执行一些持久性调用- 这很可能是你问题的原因,你可以在你的新线程中创建一个单独的会话线程,以便如果我的假设成立,它将是每个线程的一个会话。

NHibernate reference has it in section 10.2

NHibernate 参考在 10.2 节中有它

http://nhibernate.info/doc/nh/en/index.html#transactions

http://nhibernate.info/doc/nh/en/index.html#transactions

回答by Hace

You can have one NHibernate SessionFactory for multiple threads as long as you have a separate NHibernate session for each thread.

只要每个线程都有一个单独的 NHibernate 会话,您就可以为多个线程使用一个 NHibernate SessionFactory。

here is an example that will give exceptions because it uses the same session for each thread:

这是一个将给出异常的示例,因为它对每个线程使用相同的会话:

https://forum.hibernate.org/viewtopic.php?p=2373236&sid=db537baa5a57e3968abdda5cceec2a24

https://forum.hibernate.org/viewtopic.php?p=2373236&sid=db537baa5a57e3968abdda5cceec2a24

The solution is to store sessions on the LocaldataStoreSlot, that way you can have a session-per-request model.

解决方案是将会话存储在 LocaldataStoreSlot 上,这样您就可以拥有一个 session-per-request 模型。