Is NHibernate SessionFactory thread safe
According to documentation the SessionFactory should be thread safe:
An ISessionFactory is threadsafe, many threads can access it concurrently and request ISessions.
I'm looking at the implementation and I can't understand how e.g. OpenSession()开发者_C百科 can be thread safe:
http://nhibernate.svn.sourceforge.net/viewvc/nhibernate/trunk/nhibernate/src/NHibernate/Impl/SessionFactoryImpl.cs?revision=5233&view=markup
There are few lock() calls but nothing in OpenSession(). All the lock free thread safe implementations I was able to find were basically singletons.
OpenSession just returns a new object and passes in fields from the SessionFactoryImpl. Those fields seems to not be modified any place (bar the constructor) so this seems thread safe. The prerequisite is the settings.CacheProvider.NextTimestamp()
and sessionLocalInterceptor.SetSession(session);
are also thread safe as these are potentioally altered in some of the overloaded OpenSession calls.
There's room for some race conditions atleast on .Dispose/.Close though.
精彩评论