7.7.1. Stateful Session Beans

Stateful session beans are intended to represent stateful conversations with clients of a component application. A stateful session bean looks like an object with a business interface (EJB 3.0 and above) or an object with a remote interface and a factory with a home interface (EJB 2.1 and below).

public interface ASessionBeanHome extends javax.ejb.EJBHome
{
  public ASessionBean createOneWay (int iArgument)
    throws RemoteException, CreateException;
  public ASessionBean createAnotherWay (int iArgument, String sArgument)
    throws RemoteException, CreateException;
}

public interface EJBHome extends Remote
{
  public void remove (Handle handle) throws RemoteException, RemoveException;
  ...
}
public interface ARemoteInterface extends javax.ejb.EJBObject
{
  public void myMethodOne (int iArgument) throws RemoteException { ... }
  public int myMethodTwo (Object oArgument) throws RemoteException { ... }
}
public class ASessionBean implements javax.ejb.SessionBean
{
  // Method that provides reference to standard session context object
  public void setSessionContext (SessionContext sessionContext) { ... };

  // Method that is called after construction
  public void ejbCreateOneWay (int iArgument)
  throws RemoteException, CreateException
  { ... }
  public void ejbCreateAnotherWay (int iArgument, String sArgument)
  throws RemoteException, BadAccountException, CreateException
  { ... }

  // Method that is called before destruction
  public void ejbRemove () { ... }

  // Methods that are called after activation and before passivation
  public void ejbActivate () { ... }
  public void ejbPassivate () { ... };

  // Some business methods ...
  public void myMethodOne (int iArgument) { ... }
  public int myMethodTwo (Object oArgument) { ... }
}
@Stateful public class ASessionBean implements ABusinessInterface
{
  // Injected reference to standard session context object
  @Resource public SessionContext sessionContext;

  // Method that is called after construction or activation
  @PostConstruct @PostActivate
  public void myInitMethod () { ... }

  // Method that is called before passivation or destruction
  @PreDestroy @PrePassivate
  public void myDoneMethod () { ... }

  // Some business methods ...
  public void myMethodOne (int iArgument) { ... }
  public int myMethodTwo (Object oArgument) { ... }

  // Business method that removes the bean instance
  @Remove public void myRemovalMethod () { ... }

  // Interceptor method that can also be in separate interceptor class
  @AroundInvoke
  public Object myInterceptor (InvocationContext inv)
  throws Exception
  {
    ...
    Object result = inv.proceed ();
    ...
    return (result);
  }
}

Lifecycle of a stateful session bean from client point of view. (EJB 3.0 and above) Created when a reference is obtained, a business method to initialize the state, a method designated as a Remove method to discard the state. (EJB 2.1 and below) Created when a createXxx method is called on home interface, delivered as an ejbCreateXxx method to initialize the state, a remove method to discard the state.

Lifecycle of a stateful session bean from container point of view. Activation and passivation, preserves conversational state as transitive closure of field values using serialization.