StateLess Vs StateFul - Interesting


EJB programming & troubleshooting: StateLess Vs StateFul - Interesting

  1. StateLess Vs StateFul - Interesting (5 messages)


    I have written a bean method like this:

    int counter = 0; // Instance Variable
    public String getName(){
        return "Sabyasachi = "+ new Integer(counter).toString();

    I have deployed this bean both as a stateless and stateful session bean.

    The client code is something like this:

    Properties props = new Properties();
    Context ctx = new InitialContext(props);
    Object obj = ctx.lookup("ejb/SF/MyAppEjb");
    MyAppHome home = (MyAppHome)PortableRemoteObject.narrow(obj,MyAppHome.class);
    MyAppRemote remote = home.create();
    System.out.println("In Client " +remote.getName());
    System.out.println("In Client " +remote.getName());
    System.out.println("In Client " +remote.getName());

    When I call the StateLess session bean twice by running this client code the output is

    In Client Sabyasachi = 1
    In Client Sabyasachi = 2
    In Client Sabyasachi = 3


    In Client Sabyasachi = 4
    In Client Sabyasachi = 5
    In Client Sabyasachi = 6

    But when I call this as a StateFul session bean twice the output being:

    In Client Sabyasachi = 1
    In Client Sabyasachi = 2
    In Client Sabyasachi = 3


    In Client Sabyasachi = 1
    In Client Sabyasachi = 2
    In Client Sabyasachi = 3

    My question is why it is so that in case of a stateless sesion bean the instance variable state is maintained and in case of stateful session bean the state is reinisialized?

  2. Stateless Vs Stateful Bean[ Go to top ]

    It is the behaviour of EJB.

    For Stateless session bean, there will be some few instatnces created when the server started and will be used by the server to serve the client REQUESTS. So, when you are making the second call, if the client is contacting the same bean instance, then it will get the result like shown by your mail. If it is calling other instance, which was not called first, then it will return as stateful session bean.

    In the stateful session bean for every client request a instance will be created. So, the values won't be REPEATED. Even for stateless, if the second call is diverted to different bean instance by the server, then it will behave as like as Stateful session bean.

    I think you are cleared now.

    Is n't it?
  3. The Scope of the Stateless bean will be over at the end of user request. But for Statefull session the Bean scope exists till the end of user session. In this case the scope of the Stateless bean should be over once it is called at the first attempt but still the Stateless bean object exists even in the second attempt also.

    I also got confused by this result. Any comments from others.
  4. Nothing strange at all. The behaviour for instance variables between calls is UNDEFINED. The server may throw away the stateless bean immediately, reuse if for another client or keep giving it to you. A stateful bean on the other hand is reserved for your use.

    You may certainly use instance variables in a stateless bean, but you can only rely on their values when you are running inside a method invocation (unless they are constant and initialized on startup - they will not magically change between calls, but if there are multiple beans running on the server you don't know which bean you will end up in).

    In summary stateless beans should give more or less random results (depending on the appserver you are running and how much load it is under) whereas you can count on your stateful beans to maintain state between calls.
  5. Yes, Both Paneer and Erik are right.

    Stateless session beans do not have the responsibility of maintaining the state of a client. That is why it keeps swapping the instances between different cleints. In my Example for Stateless session bean after the first call is over the instance is free to serve another client and that is why we get such a result for the instance variable.

    In case of a statefull session bean the client state is maintained so each bean instance is dedicated to each cleint and that is why during multiple calls in my example it takes them as multiple clients and assign them new instances and hence the instance variable is initialised.

    In short that is the main difference between the stateless and statefull session beans.

    Thanks to all.
  6. The Output you got for the statefull session Bean (2nd case) is guaranteed. Where as the output of the stateless session bean is not guaranteed. !!