Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate Configuration "current_session_context_class" possible values and descriptions

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.

like image 351
johnofcross Avatar asked Aug 18 '11 20:08

johnofcross


People also ask

How do I configure NHibernate?

If you are using XML-based configuration, you can use a hibernate. cfg. xml file, which is just a standalone xml file using the NHibernate schema, or you can embed that NHibernate specific configuration inside of your app or web.


2 Answers

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

like image 173
Cole W Avatar answered Sep 29 '22 01:09

Cole W


"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.

like image 30
love kumar Avatar answered Sep 29 '22 02:09

love kumar