views:

32

answers:

1

I have a Windows.Forms application and we need to use one session and keep it opened, because our application is a legacy product that we're moving to NH.

Here is the code that gets a session:

public static ISession GetCurrentSession()
{
    ISession session;

    if ( CurrentSessionContext.HasBind( SessionFactory ) )
    {
        session = SessionFactory.GetCurrentSession();
    }
    else
    {
        _Log.Info( "Unable to get a current session. Opening a new one." );

        session = OpenSession();

        CurrentSessionContext.Bind( session );
    }

    return session;
}

The hibernate.cfg.xml has this property:

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

We are using one Thread in application.

The problem is that often it doesn't have a session in CurrentSessionContext so it re-opens a new session which breaks lazy loading for lots of our properties.

Please, tell me why it Unbinds session from the Context and how to avoid that?

Cheers, Alex

A: 

You could try thread_static context instead.

However, using a single session for a Winforms application is incorrect and will lead to problems.

Diego Mijelshon