NHibernate thread safety with session

后端 未结 3 2086
有刺的猬
有刺的猬 2021-01-31 12:30

I\'ve been using NHibernate for a while now and have found from time to time that if I try to request two pages simultaniously (or as close as I can) it will occasionally error.

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-31 12:59

    I can't be certain (as I'm a Java Hibernate guy) in NHibernate but in hibernate Session objects are not thread safe by design. You should open and close a session and never allow it out of the scope of the current thread.

    I'm sure that patterns such as 'Open session view' have been implemented in .Net somewhere.

    The other interesting issue is when you put a hibernate entity in the session. The problem here is that the session that it is attached to will be closed (or should be) on the request finishing. You have to reattach the entity to the new (hibernate) session if you wish to navigate any non loaded associations. This in it's self causes a new issue if two requests try to do this at the same time as something will blow up if you try to attach an entity to two sessions.

    Hope this helps. Gareth

提交回复
热议问题