-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: nHibernate Session-Handling
PostPosted: Tue Nov 24, 2009 6:57 am 
Newbie

Joined: Wed Feb 11, 2009 11:04 pm
Posts: 2
Hi all!

I am having a question which is not directly related to nHibernate/Hibernate, but since i figure that the probability is very high, that somebody here has already run into the same problem, i decided to ask it here :)

I am having a 3-layered Web-application, whereas i am using ASP.NET in the presentation-layer, having all my logic in the business-layer, and my DAO's using nHibernate in the persistence-layer.
My current problem is, that i am having a long-running task (>1.5h) which i need to move from my business-layer into an external command-line application (this task is blocking the website for the time it is running, due to this, and the fact that ASP.NET was not designed for such long running tasks, i need to move it).

Now, my idea was:

Create a command-line application, which simply loads my business- and persistence-assemblies, and subsequently starts the long-running method in my business-layer.

Now here's what i am not sure about:

All the methods in my persistence-layer are using a method "GetCurrentSession" of my nHibernateHelper-Class (many of you may use one or another version of this one, floating around on the internet), which means that my upper-layers have no control over what is actually the "current session".

Relevant parts of this class look like this:

Code:
public static ISession GetCurrentSession() {
      ISession currentSession = ContextSession;

      try {
        if (currentSession == null)
          if (IsInWebContext()) {           
            currentSession = sessionFactories[HttpContext.Current.Session["currentDB"].ToString()].OpenSession();
          } else {
            currentSession = sessionFactories[currentDB].OpenSession(); ;
          }//else
        ContextSession = currentSession;
        return currentSession;
      } catch (HibernateException) {
        throw;
      }//catch
}//GetCurrentSession

private static ISession ContextSession {
      get {
        if (IsInWebContext()) {
          return (HttpContext.Current.Items[NHIBERNATE_SESSION_KEY] as ISession);
        }//if
        //We are not in a Web-context, return our static session...
        return session;
      }//get
      set {
        if (IsInWebContext())
          HttpContext.Current.Items[NHIBERNATE_SESSION_KEY] = value;
        else
          session = value;
      }//set
}//ContextSession

private static bool IsInWebContext() {
      return HttpContext.Current != null;
}//IsInWebContext


As you can see: If it is determined, that a web-app is requesting the session, i create one and store it in the context of the web-app, if not then i am returning a static session that is stored within the class.

Now the question is: If, a web-app _and_ my command-line app are requesting a session at the same time (calling GetCurrentSession at the same time), will the current implementation ensure, that the web-app thread will get its session stored in its context, and the command-line app will get the static session?

Sorry for the long introduction on this, i just wanted to make clear my motivation for all this :) Is anybody here running a similar setup and can provide any hints on this?

regards,

--qu


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.