nHibernate session and multithreading nHibernate session and multithreading multithreading multithreading

nHibernate session and multithreading


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


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.