8 Replies Latest reply: Nov 26, 2012 2:03 AM by 943589 RSS

    Stateless vs Stateful beans

    970232
      Hi,

      I am new to Java EE and EJB 3, so many concepts are not clear to me.

      I've read somewhere that Stateless beans in Java do not keep their state between two calls from the client, unlike Stateful session beans which do keep state.

      I created my first bean, I first used @Stateless annotation and then @stateful annotation, and it seems it is having the same behavior.

      This is the session bean:
      @Stateless
      public class HelloWorldBean implements IHelloWorldLocal {
          private int number = 1;
      
          @Override
          public int add(Integer number) {
              this.number += number;
              return this.number;
          }
      
      }
      I wrote a servlet to invoke session bean add method this way:

                  out.println("The result is " + helloWorld.add(2));
                  out.println("The result is " + helloWorld.add(3));
                  out.println("The result is " + helloWorld.add(4));
      The results after these three invocations are 3, 6, 10. As far as I am concerned, results should be 3, 4 and 5 based on the concept that the Stateless beans don't keep state.

      If I switch this bean to be a Stateful one, I get the same results.

      What have I missed here?

      Thanks in advance for your time and help.
        • 1. Re: Stateless vs Stateful beans
          gimbal2
          967229 wrote:
          What have I missed here?
          The fundamental understanding of it. Anyone can produce some code and make things do stuff. But you still don't understand it.

          I recommend the book Enterprise Javabeans 3.1 to actually gain the understanding you need. EJB is a tough subject matter comfortably hidden under a seemingly simple API layer. You gain more by understanding and learning what problem you actually solve than by investigating what the technology can do.
          • 2. Re: Stateless vs Stateful beans
            970232
            Hi,

            Thanks for your answer.

            I think that this behavior has to do with the bean pooling.

            I guess that the EJB container is returning the same bean instance and that's why it seems that the bean is keeping its state like a stateful session bean, but it is a instance state what is being kept here.

            What's the author of the book Enterprise Javabeans 3.1?

            Thanks a lot.
            • 3. Re: Stateless vs Stateful beans
              EJP
              I wrote a servlet to invoke session bean add method this way:
              Invoke it how? As an EJB. Or as a normal Java Object? Servlets generally don't have the wherewithal to invoke EJBs.
              • 4. Re: Stateless vs Stateful beans
                970232
                As an EJB using the @EJB annotation and then calling session bean add method.
                • 5. Re: Stateless vs Stateful beans
                  DrClap
                  I know basically nothing about EJB, but it's obvious to me that it wouldn't be possible (or at least it would be very difficult) for Java code to enforce that "stateless" annotation. So I have to conclude that it's there so that you can tell the EJB container whether your bean is stateless or stateful, and so that the container can handle the beans appropriately. It's up to you to write code which matches the annotation.
                  • 6. Re: Stateless vs Stateful beans
                    943589
                    from the example codes given, the stateless and stateful EJB will have the same execution thread during the first invokation. Hence, the first execution you can get the same result.

                    Have you try to reload the servlet call to the statefull EJB? I suppose you can get different answer after more than one reload.
                    • 7. Re: Stateless vs Stateful beans
                      EJP
                      from the example codes given, the stateless and stateful EJB will have the same execution thread during the first invokation. Hence, the first execution you can get the same result.
                      Agreed that you should get the same result, but why 'hence'? What does the thread have to do with it?
                      Have you try to reload the servlet call to the statefull EJB? I suppose you can get different answer after more than one reload.
                      Reload a call? What does that mean?
                      • 8. Re: Stateless vs Stateful beans
                        943589
                        >
                        Have you try to reload the servlet call to the statefull EJB? I suppose you can get different answer after more than one reload.
                        Reload a call? What does that mean?
                        I mean the servlet that execute the EJB method