Announcement Announcement Module
Collapse
No announcement yet.
Detecting availability of session scope Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Detecting availability of session scope

    Is there a way to detect whether a session-scoped bean is available? I've got some logging infrastructure that pulls information from a session-scoped bean, but I want to use it in other places without the 'session scope not available' exception.

    Is there a way to either a) ask the context whether session scope is available (other than, I guess, catching that exception), b) arrange for a different implementation of a bean to be available in 'non-session' scope?

    Grateful.

  • #2
    What do you mean by "other places"? A session scoped bean is tied to an http session. If yours is a web application, you have exactly one http session for each user that is currently logged in to your application. If by "other places" you mean using a session scoped bean outside of a web application, you can't. You should use prototype scope instead.

    Comment


    • #3
      Thanks for replying. To be more clear, I have a web app that's got a bunch of command objects. The commands talk with a session-scoped bean, logging the user that initiated the command. So far, so good.

      I also want to use these command objects outside of any particular http session, using some quartz-scheduled jobs. Of course, no session is available.

      Is there a way to reuse these commands in both contexts without having to a) define them twice (once with and once without the reference to the session-scope dependency) or b) catch session-not-available exceptions within the commands?

      Comment


      • #4
        In the method that retrieves the logged user (I suppose all the commands call a single method to obtain the user, and that the code is not repeated in all commands) you should check if there is an active session or not. If there is, obtain the session scoped bean and obtain the user from that; else, return a fixed string ("Batch" for example).

        Also, I'd use aop for logging instead of doing it in every command or service class. In that case, the aspect itself should check for session availability.

        Comment


        • #5
          Originally posted by Enrico Pizzi View Post
          you should check if there is an active session or not
          My question really is this simple! How do you check if there's an active session?

          Originally posted by Enrico Pizzi View Post
          Also, I'd use aop for logging instead of doing it in every command or service class.
          Yes, good advice - the only reason I'm not doing this is because the commands are all logging command-specific information.

          Comment


          • #6
            How do you check if there's an active session?
            Probably the easiest way would be to configure a session listener for your web application, then use it in your commands. I can't think right now of a Spring-specific solution, altough I suspect there must be one.

            Comment


            • #7
              Just as a follow-up, seems you can tell if you're in a request context by calling RequestContextHolder.getRequestAttributes() - the result will be non-null if you're in the context of a request.

              Comment

              Working...
              X