nHibernate session and multithreading

前端 未结 2 2004
面向向阳花
面向向阳花 2020-12-10 04:29

I had a method with a lot of persistence calls that used a nHibernate session, it worked, was alright. But I needed to refactor this method, extracting a method from a conte

相关标签:
2条回答
  • 2020-12-10 04:32

    Sessions are not thread safe in NHibernate by design. So it should be ok as long as you have a session used by only one thread.

    I'm not sure what you're thingResolver does, but if it does some persistance calls on the same session you've created in the originating thread - this most probably the cause of your problems, you could create a separate session in your new thread so that it would be a session per thread if my assumption is true.

    NHibernate reference has it in section 10.2

    http://nhibernate.info/doc/nh/en/index.html#transactions

    0 讨论(0)
  • 2020-12-10 04:41

    You can have one NHibernate SessionFactory for multiple threads as long as you have a separate NHibernate session for each thread.

    here is an example that will give exceptions because it uses the same session for each thread:

    https://forum.hibernate.org/viewtopic.php?p=2373236&sid=db537baa5a57e3968abdda5cceec2a24

    The solution is to store sessions on the LocaldataStoreSlot, that way you can have a session-per-request model.

    0 讨论(0)
提交回复
热议问题