.NET Parallel.ForEach可能会混淆NHibernate上下文会话

时间:2021-02-02 13:50:26

I have several report objects as follows : BaseReport, ChildReport1, ChildReport2, and ChildReport3. A report can implement IComplexParameterSupport interface. ChildReport1,2 and 3 all inherit from BaseReport.

我有几个报告对象如下:BaseReport,ChildReport1,ChildReport2和ChildReport3。报告可以实现IComplexParameterSupport接口。 ChildReport1,2和3都继承自BaseReport。

My logic is as follows

我的逻辑如下

  1. Open NHibernate context using the SessionFactory
  2. 使用SessionFactory打开NHibernate上下文
  3. I use recursion to generate a tree from the report types to generate a sort of menu. This is from a Parent/Child hierarchy - calling the database several times, validating each time the report type and doing specific actions related to the report definitions.
  4. 我使用递归从报告类型生成树以生成一种菜单。这来自父/子层次结构 - 多次调用数据库,每次验证报告类型并执行与报告定义相关的特定操作。
  5. Then After, I do checks if the report object is IComplexParameterSupport.
  6. 然后,我会检查报表对象是否为IComplexParameterSupport。

In some cases when I know that the report implements IComplexParameterSupport, when I do the casting, the report object stays null. It is like the context of NHibernate has been lost.

在某些情况下,当我知道报表实现了IComplexParameterSupport时,当我执行转换时,报表对象保持为null。这就像NHibernate的上下文已经丢失了。

    if(report as IComplexParameterisedSupport != null)

    {
       // logic here
    }

This happens only when I previously use the Parallel.ForEach, i.e on STEP 2. I am using a Web Application on .NET 4.0. Does anyone have similar problems with parallelism and NHibernate context sessions?

只有当我以前使用Parallel.ForEach时,即在STEP 2上,才会发生这种情况。我在.NET 4.0上使用Web应用程序。有没有人在并行和NHibernate上下文会话中遇到类似的问题?

1 个解决方案

#1


1  

Does this mean that the session can be accessed at the same time by multiple threads? If so, this is the problem, since NHibernate's Session is not thread safe. See NHibernate thread safety with session

这是否意味着多个线程可以同时访问会话?如果是这样,这就是问题,因为NHibernate的Session不是线程安全的。通过session查看NHibernate线程安全性

#1


1  

Does this mean that the session can be accessed at the same time by multiple threads? If so, this is the problem, since NHibernate's Session is not thread safe. See NHibernate thread safety with session

这是否意味着多个线程可以同时访问会话?如果是这样,这就是问题,因为NHibernate的Session不是线程安全的。通过session查看NHibernate线程安全性