How to keep long running NHibernate Session data consistent?

不羁的心 提交于 2019-12-09 19:22:50

问题


I have NHibernate sessions cached in the ASP.NET session.

I came across a situation where a user edited an object so it's in their first level cache in the ISession. Another user then edited the same object.

At this point User1 still sees their original version of their edits where as User2 sees the correct state of the object?

What is the correct way to handle this without manually calling session.Refresh(myObj) explicitly for every single object all the time?

I also have a 2nd level cache enabled. For NHibernate Long Session should I just disable the first level cache entirely?

Edit: Adding some more terminology to what I'm looking to achieve from 10.4.1. Long session with automatic versioning the end of this section concludes with

As the ISession is also the (mandatory) first-level cache and contains all loaded objects, we can propably use this strategy only for a few request/response cycles. This is indeed recommended, as the ISession will soon also have stale data.

I'm not sure what kind of documentation this is for it to include both probably and then immediately say the session will have stale data (which is what I'm seeing). What's the solution to this right here or is there none?


回答1:


  • You can't disable the first-level cache.
  • Check out "Optimistic concurrency control" in the NHibernate docs.
  • Also take a look at NHibernate.Burrow for long conversations.



回答2:


Just use IStatelessSession instead of ISession.

Also keep in mind that NH wasn't designed to be used with long-living ISessions (as already mentioned by others). One problem is that you already mentioned. The other is that the performance drops significantly when there's a large object graph tracked by NH. Both problems could be avoided by using IStatelesSession. It gives you detached objects not being tracked by NH.

Not sure about the reasoning behind keeping sessions in the ASP.NET session. Maybe you could provide some details?

Also remember that a session is a wrapper over IDbConnection. Keeping it open can easily lead to conneciton pool starvation.




回答3:


Apparently this is a known shortcoming of NHibernate as detailed by documentation cited in my question.



来源:https://stackoverflow.com/questions/3754592/how-to-keep-long-running-nhibernate-session-data-consistent

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