NHibernate Configuration "current_session_context_class" possible values and descriptions

16,067

Solution 1

Here is my attempt to explain these (Any additional input would be welcome):

Difference between CallSessionContext, ThreadLocalSessionContext and ThreadStaticSessionContext

There is a section on these in the standard NH documentation but I don't think they do a very good job explaining it or giving any examples on how exactly to use it. Here is the NH documentation link.

http://nhibernate.info/doc/nhibernate-reference/architecture.html#architecture-current-session

There are several decent articles on StackOverflow about how you would use this:
What is the best NHibernate session management approach for using in a multithread windows service application?
NHibernate.HibernateException: No session bound to the current context

Solution 2

"managed_web", "call", "thread_static", and "web" are possible values. Configured like this in nhibernate configuration:

<property name="current_session_context_class">call</property>

Once this is configured, you can use SessionFactory.GetCurrentSession(). You have to bind and unbind session yourself. One sample implementation:

if (CallSessionContext.HasBind(_sessionFactory))
            {
                session = SessionFactory.GetCurrentSession();
            }
            else
            {
                session = SessionFactory.OpenSession();
                CallSessionContext.Bind(session);
            }

Instead of CallSessionContext, you can also use ManagedWebSessionContext or ThreadStaticSessionContext.

ManagedWebSessionContext - Suitable for Asp.Net application. Session is attached to current HttpContext (supplied as parameter while binding).

ManagedWebSessionContext.Bind(HttpContext.Current,session)

ThreadStaticSessionContext - Session is attached to current thread (I wont encourage using this as threads keep switching abruptly and your attached session could be lost).

CallSessionContext - Suitable for non-web applications. Session is attached to CallContext. I could be wrong but I imagine this as session attached to SessionFactory itself. As long as you have one SessionFactory for entire application, this approach will ensure you will never get concurrent active sessions.

Share:
16,067

Related videos on Youtube

johnofcross
Author by

johnofcross

Updated on June 04, 2022

Comments

  • johnofcross
    johnofcross almost 2 years

    I currently have this Fluent NHibernate configuration:

    public class NHibernateConfig
    {
        public static Configuration Configure()
        {
            var cfg = Fluently.Configure()
                .Database(Config.PersistenceConfiguration)
                .Mappings(m =>
                              {
                                  m.FluentMappings.AddFromAssemblyOf<SomeAssembly>();
                                  m.FluentMappings.Conventions.AddFromAssemblyOf<EnumConvention>();                              })
                .ExposeConfiguration(x => x.SetProperty("current_session_context_class", "thread_static"))
                .BuildConfiguration();
            return cfg;  
    
         }
    }
    

    My question is about the exposed property "current_session_context_class." I know of two values for this: thread_static or web. A colleague of mine pointed out another value, which is call. Are there any known documentation for values of this property? If not, are there any good descriptions for these values? I've scoured Google for hours for some explanations with no valid result.

  • johnofcross
    johnofcross over 12 years
    Your links actually provide the most documentation I've seen for this. Thanks. It's not the all-encompassing doc, but I guess that doesn't exist.
  • johnofcross
    johnofcross about 11 years
    What's the difference between MangedWeb and Web? Or are those the same?
  • Francois Botha
    Francois Botha over 8 years
    The NHForge link is broken. Anyone have an updated link?
  • Cole W
    Cole W over 8 years
    @FrancoisBotha I fixed the link
  • Scott Blasingame
    Scott Blasingame over 7 years
    @johnofcross, managedweb is deprecated as of 4.0.0.GA: * Removed ManagedWebSessionContext. Any configuration files which use the "managed_web" session context should now use "web"