3 Replies Latest reply on Jul 22, 2011 1:04 PM by haye

    Lifecycle\scope of Stateful Session Beans

    roflchap

      Hi,


      If I am not wrong, a SFSB stays dedicated to one client (i.e. session) during its life. This means that if a client updates the state of a SFSB in one request, it will be preserved during the subsequent requests.


      Now what about this managed SFSB:


      @Stateful
      @Model
      public class GreetingBean 
      {
              public String getGreeting()
              {
                      return "Hello world";
              }
      }



      On one hand, it's a SFSB so technically it should not be swapped between different requests in the same session. On the other hand it's @RequestScoped so we are declaring that a new instance should be created between different requests, hence it won't preserve its state during a session.


      Or is it possible to modify the lifecycle/scope of a SFSB in EJB 3.1, so by definition a session won't be necessarily tied to a particular instance of a SFSB anymore?


      Thanks in advance.

        • 1. Re: Lifecycle\scope of Stateful Session Beans
          lightguard

          I'm actually not sure what to say on this one. Maybe someone else has an answer. Of course the best way is to try it and find out :)

          • 2. Re: Lifecycle\scope of Stateful Session Beans
            swd847

            Session in this case does not mean Session in terms of the web session, but rather a session between the client and the ejb.


            This bean will live through the request, and then be destoryed.

            • 3. Re: Lifecycle\scope of Stateful Session Beans
              haye

              Stateful session beans can belong to CDI contexts. The container takes control over its construction/destruction. For a request-scoped SFSB, a new instance is created per request and destroyed at the end of the request (the container will call the @Removes method). The same applies to session-scoped and conversation-scoped SSBS. That is, when the session times out or when conversation.end() is called, the container destroys the SFSB and creates a new instance for each new session or conversation.