1

我看过很多帖子回复:上述问题(无法找到持久性)。但是我们遇到的问题是这个错误在我们的 Web 应用程序(ASP.Net MVC)中随机发生。此外,当我们重新启动应用程序池时,问题就消失了。

大多数错误发生在缓存的实体上。请参阅下面的映射。

  <class name="Privilege" table="PRIVILEGE" lazy="false">
<cache usage="nonstrict-read-write"/>
<id name="Id" />
<property name="Description" column="DESCRIPTION" not-null="true" />
<set name="RoleCollection" table="PRIVILEGE_ROLE">
  <cache usage="nonstrict-read-write"/>
  <key column="PRIVILEGE_ID" foreign-key="PRIVILEGE_ROLE_FK1" />
  <many-to-many class="Role" column="ROLE_ID" foreign-key="PRIVILEGE_ROLE_FK2" />
</set>

<class name="Role" table="ROLE" lazy="false" >
<cache usage="nonstrict-read-write"/>
<id name="Id"  />
<property name="Description" column="DESCRIPTION" not-null="true" />
<set name="PrincipalCollection" table="ROLE_PRINCIPAL">
  <cache usage="nonstrict-read-write"/>
  <key column="ROLE_ID" foreign-key="ROLE_PRINCIPAL_FK1" />
  <many-to-many class="Principal" column="PRINCIPAL_ID" foreign-key="ROLE_PRINCIPAL_FK2" />
</set>

这可能与 NHb 会话损坏有关吗?如果有人至少可以指出解决此问题的方向,我们将不胜感激,因为也无法始终如一地重现该问题。(我们使用 NHb 3.1.0.4000)

4

1 回答 1

1

很抱歉迟到提供更新。

我已经找到了原因,这恰好是 Nhibernate Session Factory 的初始化方式。

  1. Nhb 会话工厂是惰性启动的。即在第一个需要数据库会话的 HTTP 请求时。(即仅在主动需要时创建)
  2. 一旦创建它就会被重复使用,因为它是一个单例
  3. 我们已将应用程序池配置为每天(凌晨 4 点)回收,我们注意到异常的第一次出现也大致在同一时间范围内。
  4. 当用户 (HTTP) 请求落在应用程序池回收窗口内时,该问题看起来像是 IIS 多线程和 NHb 会话工厂实现的组合。

解决方案

  1. 回到急切的初始化。即 NHb 会话工厂的初始化现在发生在 Application_Start。
  2. 因此,HTTP 用户请求“始终”有一个现成的会话因素可供使用。

Not only is this approach fixed the issue, I think it's a better option from a performance point of view. Eagerly doing the costly operation of Session Factory initialization rather than putting more weight on a particular user request is more reflective of the problem at hand.

We are running this modified version in prod for a month now without any issues.

于 2012-07-30T08:59:20.423 回答