开发者

Is NHibernate SessionFactory thread safe

开发者 https://www.devze.com 2023-02-05 14:24 出处:网络
According to documentation the SessionFactory should be thread safe: An ISessionFactory is threadsafe, many threads can access it concurrently and request ISessions.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号