What is difference between stateless and stateful session beans?

In this post, we will see the differences b/w stateless and stateful session beans.

Stateless Session Bean:

  • Stateless session bean do not maintain state across method calls and transactions
    The EJB server transparently reuses instances of the Bean to service different clients at the per-method level (access to the session bean is serialized and is 1 client per session bean per method.
  • Used mainly to provide a pool of beans to handle frequent but brief requests. The EJB server transparently reuses instances of the bean to service different clients.
  • Do not retain client information from one method invocation to the next. So many require the client to maintain on the client side which can mean more complex client code.
  • Client passes needed information as parameters to the business methods. Performance can be improved due to fewer connections across the network.

Stateful Session Bean :

  • A stateful session bean holds the client session’s state.
  • It is an extension of the client that creates it.
  • Its fields contain a conversational state on behalf of the session object’s client.
    This state describes the conversation represented by a specific client/session object pair.
  • lifetime of these are controlled by the client and cannot be shared between clients.