tags:

views:

117

answers:

2

I have a c# library for my web application where I am using nhibernate. The web application has the hibernate.cfg.xml file where I set:

current_session_context_class = web

I am using a nhibernateHelper class and a httpmodule to open and close the session on a per request basis.

I now need to use this library in a console application, what should I set the current_session_context_class value to be now?

+2  A: 

There is a "thread" option that you can use. That will bind the current session to the thread.

For more on session management in desktop applications: http://stackoverflow.com/questions/90530/what-is-your-session-management-strategy-for-nhibernate-in-desktop-applications

Ayendes MSDN article on NHibernate and desktop applications: http://msdn.microsoft.com/en-us/magazine/ee819139.aspx

Torkel
+1  A: 

I've used nHibernate for both web and console apps, but never use this particular config setting.

I did some google-ing and found this:
Contextual Sessions

Excerpts:

Out-of-the-box, NHibernate 2.0.0 comes with several implementations of this interface:

  • NHibernate.Context.ManagedWebSessionContext -current sessions are tracked by HttpContext. However, you are responsible to bind and unbind an ISession instance with static methods on this class, it never opens, flushes, or closes an ISession itself.

  • NHibernate.Context.CallSessionContext -current sessions are tracked by CallContext. You are responsible to bind and unbind an ISession instance with static methods of class CurrentSessionContext .

  • NHibernate.Context.ThreadStaticSessionContext -current session is stored in a thread-static variable. This context only supports one session factory. You are responsible to bind and unbind an ISession instance with static methods of class CurrentSessionContext.

  • NHibernate.Context.WebSessionContext - analogous to ManagedWebSessionContext above, stores the current session in HttpContext. You are responsible to bind and unbind an ISession instance with static methods of class CurrentSessionContext.
    .....
    ...however, there are corresponding short names: "managed_web", "call", "thread_static", and "web", respectively.

o.k.w