开发者

How to use NHibernate CurrentSessionContext within a Timer

开发者 https://www.devze.com 2022-12-10 13:25 出处:网络
I build a repository class that use ISessionFactory.GetCurrentSession() to find the current session that its methods should work with and used CurrentSessionContext.Bind to bind a session within my ap

I build a repository class that use ISessionFactory.GetCurrentSession() to find the current session that its methods should work with and used CurrentSessionContext.Bind to bind a session within my application. Inside the NHibernate config I used thread_static as my current_session_context_class.

This worked perfectly so far. On each thread I created a new session that will be used by the repository instance.

But when I use a System.Threading.Timer the callback will be called on any thread within the ThreadPool. So that I can't bi开发者_运维技巧nd a session beyond the execution of one callback, because the next callback could be executed on a different thread.

How should sessions be handled (with or without CurrentSessionContext) within a System.Threading.Timer if one session should be used on two or more executions of the callback?


A session should be tied to a unit of work. It sounds like you just need a new session on every call back. Sessions are very light weight to create.

0

精彩评论

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

关注公众号