How to use NHibernate CurrentSessionContext within a Timer
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.
精彩评论