什么CurrentSessionContext用于任务

时间:2014-04-14 12:45:51

标签: c#-4.0 nhibernate

将CurrentSessionContext与Tasks一起使用时,应该使用什么上下文?

我一直在使用ThreadStatic上下文,偶尔会收到来自NHProf的关于在多个线程中使用的相同会话的警告。

1 个答案:

答案 0 :(得分:0)

如果您使用的是ThreadStatic,那应该没问题。如果使用得当,每个线程应该使用不同的会话。我通常使用类似下面的内容来使用会话上下文:

// Open a session if there is not one opened yet.
if (!CurrentSessionContext.HasBind(sessionFactory))
{
    CurrentSessionContext.Bind(sessionFactory.OpenSession());
}

var currentSession = sessionFactory.GetCurrentSession();