NHibernate - Random occurrences of “Unable to locate persister”

心不动则不痛 提交于 2019-12-24 16:15:05

问题


I've seen many posts Re: the above problem (Unable to locate persister). However the problem we are having is that this error occurs randomly in our web application (ASP.Net MVC). Also when we restart the app pool the problem goes away.

Mostly the error happens for an entity that is cached. See mappings below.

  <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>

Could this be related to a NHb Session corruption? We'd highly appreciate if someone could at least point us in a direction of troubleshooting this as it's not possible to consistently reproduce the problem either. (We use NHb 3.1.0.4000)


回答1:


Sorry for being late to provide an update.

I've figured out the reason for this and it happens to be the way the Nhibernate Session Factory was being initialized.

  1. The Nhb Session Factory was lazy initiated. i.e at the very first HTTP request requiring a DB session. (i.e Create only when it's being actively required)
  2. Once created it's re-used since it's a singleton
  3. We have configured the app pool to recycle every day (4 AM) and we noticed that the first occurrence of the exception also falls roughly at the same time frame.
  4. The issue looked like a combination of IIS multi-threadeding and NHb session factory implementation, when a user (HTTP) request falls within the window of a app pool recycle.

Solution

  1. Go back to eager initialization. i.e the initialization of the NHb session factory is now happening at Application_Start.
  2. So a HTTP-user request 'always' have a ready made session factor to use.

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.



来源:https://stackoverflow.com/questions/10099018/nhibernate-random-occurrences-of-unable-to-locate-persister

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!