2

If I somehow manage to configure NHibernate sessions to be per thread, will that work when I process data in threads coming from a thread pool?

Threads from the pool are "recycled" (yes?) so if a thread was already used once and it gets called into action again, will its session be no loner useable?

rae1
  • 6,066
  • 4
  • 27
  • 48
getit
  • 623
  • 2
  • 8
  • 30

1 Answers1

3

The correct handling of the ISession is fully in your hands, you are responsible. Not the ThreadPool. So, if you use session instance per thread, you still should know (and manage) when it is created, and that it is explicitly closed and disposed.

If that ISession management is in place, do not worry about issues with a Thread pool. On the other hand, it also means, that opening and keeping a session in [ThreadStatic] field - is not correct:

short extract:

The thread pool (by design) keeps the threads alive between calls. This means that the ThreadStatic variables will persist between calls to QueueUserWorkItem.

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335