4 Replies Latest reply on Aug 24, 2002 10:01 PM by Noel Hebert

    Confusion in Concept - Stateless/Statefull

    Muraly Newbie

      Hi,
      I have cretaed a bean. Pls find the code below

      package com.test.ejb;

      import javax.ejb.SessionBean;
      import javax.ejb.SessionContext;

      public class WhoAmIBean implements SessionBean
      {
      String whoAmI = "SpiderMan!";

      public String getWhoAmI()
      {
      System.out.println("Stateless :- " + whoAmI);
      return whoAmI;
      }

      public void setWhoAmI(String whoAmI)
      {
      this.whoAmI = whoAmI;
      }
      /*public void ejbCreate(String whoAmI)
      {
      this.whoAmI = whoAmI;
      }*/
      public void ejbCreate(){}
      public void setSessionContext(SessionContext sc){}
      public void ejbPostCreate(){}
      public void ejbRemove(){}
      public void ejbActivate(){}
      public void ejbPassivate(){}
      }

      Problem
      --------

      If i deploy this bean as a stateless bean, then the state of the variable, is maintained b/w the methods set*** and get***.
      What I theoritically learned about a Stateless Bean is that it cant maintain its state b/w method calls. I thought it can be done only by statefull beans.

      I am calling this methods in consecutive lines. DOes it have any impact. I checked thoruoughly with the deployment descriptors, I have mentioned "Stateless".

      Can anyone throw some light on to same.

        • 1. Re: Confusion in Concept - Stateless/Statefull
          Juha Lindfors Master

          It could happen that you get the same instance back from the pool on consequtive calls if there is only one client calling the server (ie. no concurrency). Depends on the internal data structure of the pool.

          Create more threads on the client (with different state) and you should start seeing your program breaking down.

          • 2. Re: Confusion in Concept - Stateless/Statefull
            Muraly Newbie

            Hi juha,
            Yes I tried with multiple clients calling the same instance. But for each client it it returning the values which is set by the set*** methods.

            I think eventhough a instance variable is declared in a stateless bean it is useless coz the bean cant maintain the state.

            This is possible only with statefull beans.

            Or is my concept wrong.

            • 3. Re: Confusion in Concept - Stateless/Statefull
              Juha Lindfors Master

              There's no guarantee that you get the same instance back from the pool with consecutive calls when using stateless session beans. Before the invocation an instance is retrieved from the pool and after each invocation the bean instance is returned to the pool. This is why storing state that spans more than one remote method invocation doesn't work.

              • 4. Re: Confusion in Concept - Stateless/Statefull
                Noel Hebert Newbie

                Muraly,

                I also answered this in much the same way in the FAQ
                forum as Juha as done here.

                JBoss will only create as many instances of a bean to
                meet the arrival rate of requests for the bean.

                Having two, five or even ten client connections does not
                necessarily mean that JBoss will create a new instance
                for each one.

                Your bean is very simple and each method invocation
                probably only takes 10s of milliseconds. With that
                throughput even ten client connections might not be
                enough to get a new instance.

                You need to create enough *concurrent" load so that the
                arrival rate is faster than a single bean can handle.

                If you can get say five requests to arrive at essentially
                the same time, then more instances will be created to
                handle the requests.

                Hope this helps.

                Cheers,

                Noel.