6 Replies Latest reply: Oct 22, 2012 4:49 AM by 859967 RSS

    Store stateful session bean in EJB module?

    859967
      Hi,

      I wanna use Stateful Session Bean (SFSB) in my EJB module. But it seems that i can't use it because i should not inject my SFSB to any managed bean, or message driven bean (because if i do, the SFSB will be shared to all clients)

      I found the suggested solution is to store SFSB in HttpSession.

      I am trying to do this way, but i got null pointer exception.
      HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true); 
      So, how can i do this in EJB module?

      thanks,
        • 1. Re: Store stateful session bean in EJB module?
          gimbal2
          CardM wrote:
          Hi,

          because i should not inject my SFSB to any managed bean, or message driven bean (because if i do, the SFSB will be shared to all clients)
          And why do you think that is the case? I think you're confusing them with servlets.
          I am trying to do this way, but i got null pointer exception.
          HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true); 
          So, how can i do this in EJB module?
          You don't. The EJB layer is completely detached from any kind of web environment, and as such you don't have (reliable) access to the faces context in there. When you're using JSF you should be working with the session in a managed bean.
          • 2. Re: Store stateful session bean in EJB module?
            859967
            ah right, it is shared with the case of servlet. Then have you got any ideas to use SFSB in EJB module? Keep it in a singleton bean and store client session in a table to access appropriate SFSB instance ?

            p/s: my client is a JMS call.

            -HL

            Edited by: CardM on Oct 15, 2012 6:49 PM
            • 3. Re: Store stateful session bean in EJB module?
              gimbal2
              CardM wrote:
              ah right, it is shared with the case of servlet. Then have you got any ideas to use SFSB in EJB module? Keep it in a singleton bean and store client session in a table to access appropriate SFSB instance ?
              I really don't get your question. It sounds like you simply have no clue how stateful beans really work (not really strange, its far from easy) and you want someone to explain to you in a forum post what is explained in whole chapters of books. It ain't gonna happen.

              Let me pose a counter question: why do you think you need a stateful bean? Why do you think you need to keep state in memory?
              • 4. Re: Store stateful session bean in EJB module?
                859967
                I am just curious how people use SFSB in EJB module or whether they do use SFSB in EJB module. And if it is discussed here then i may figure out a good approach of programming it. Anyway, thanks for your reply. The question is answered by myself.
                • 5. Re: Store stateful session bean in EJB module?
                  gimbal2
                  Well me: hardly. Generally there is a web layer involved with a perfectly fine session (or conversation) in which state can be maintained.

                  Alas you don't want to seem to really say what's on your mind with or without my prying so I'll just offer a hunch I have. A stateless bean only remains "active" as long as you keep an active connection to it; IE. the reference you lookup or inject in your client code. The "storing the bean in the session" you refer to in your first post is the act of keeping the bean alive; as long as the reference is stored in the session you can keep using it for the same client without it being cleaned up by the container. However: when working from a web layer there isn't much use for a stateful bean as you can simply use the session itself directly to hold onto stuff. Its personal preference if you use them or not really.

                  Where you were going wrong in your initial post is that you don't fetch the object from the HTTP session in your EJB code. But you probably already figured that out for yourself.