stateless a little bit stateful?


EJB programming & troubleshooting: stateless a little bit stateful?

  1. stateless a little bit stateful? (3 messages)

    it appears that my stateless session beans behave like stateful session : i wrote a simple stateful bean that save the number of calls to a method and print it. When i transform it to be stateless, the state is still saved even when i connect several seconds later. i'm aware that i can't relie on a stateless to save a state, but in my mind, a stateless can't have a state...right? so when is this state cleared ? is there a cache-timeout, a max-number of calls, a max-number of beans? a max-size for the cache?
  2. stateless a little bit stateful?[ Go to top ]

    Stateless beans maintain state. Infact their state tends to outlast stateful beans because when a client calls remove() on a stateless bean it does not destroy the bean. Only the administrator can create and destroy stateless beans.

    A Stateful bean however is dedicated to a single client. Therefore a stateful bean can assume that it can keep state that is specific to a single client. It can assume that every call made on it is made by the same client. In comparison, a stateless bean may service requests from many different clients in its lifetime. That is the difference.

    Please refer to the STDs in the EJB spec for details.

    Simon Reavely
  3. stateless a little bit stateful?[ Go to top ]

    Since you are the only user(single thread) accessing the bean, only one instance of bean will be served by the container. That's why you can see the state.

    Try with couple of people accessing the bean, then you will notice it won't save the session (values of instance variables).
  4. stateless a little bit stateful?[ Go to top ]

    State in a Stateless SB is allowed, it's just not conversational and you cannot assume that you will get the same "state" on two subsequent calls since the actual bean underneath might have changed in a multi-use world.

    So, you can use stateless beans with a DB connection for instance, since that's not specific to a client.

    BUT, you can't rely on it to remember an account number correctly between calls.

    So, if you find yourself with a Stateless bean with an ejbCreate method taking parameters, then make sure it's non client specific information. In 99.9% of cases stateless beans should have no parameters in their ejbCreate() methods for this very reason.