EJB Handle stores Stateful session bean's State?


EJB design: EJB Handle stores Stateful session bean's State?

  1. Hi,
    Does a EJB Handle for a stateful Session Bean store the session bean's state also? or does it just serialize and store the remote reference of a Stateful Bean without its state? In the later case, wot happens to the bean's state when the bean times out? The handle will just point to an empty reference or will it be able to generate the state even after the session bean's time is out? Could someone help me out?

  2. Hi,
     The EJBHandle is serializable and it also serializes the entire session state so that you can ressurect the entire serialized state and do not get the remote reference . I am enclosing code for serializing Handle and then recreating the stateful session bean.


    // To save state
    private void suspend(String filename) {
    ObjectOutputStream stream = null;

    try {
    stream = new ObjectOutputStream(
    new FileOutputStream(filename));
    for (int i=0; i < components.size(); i++) {
    Component comp = (Component) components.elementAt(i);// Component is a Stateful Session Bean
    Handle handle = comp.getHandle();

    System.out.println("Game saved.");
    catch (Exception e) {

    * Resumes a the state EJB object handles.
    private void resume(String filename) {


    ObjectInputStream stream = null;

    try {
    stream = new ObjectInputStream(
    new FileInputStream(filename));

    while (true) {
    Handle handle = (Handle) stream.readObject();

    (Component) handle.getEJBObject());
    catch (EOFException e) {
    System.out.println("Game loaded.");
    catch (Exception e) {
    finally {
    if (stream != null) {
    try { stream.close(); } catch (Exception e) {}

  3. Hi Ishtmeet,
    thanks for your response. I think the program which you have given does not store the state of the session bean. It just stores the remote reference of the stateful session bean in a persistence storage using ejbHandle. Moreover, in Richard Monson Haefel's book, he has given that a stateful session bean will be lost and the handle will be useless if the EJB Server goes down or if it crashes. He has also given that if stateful session bean times out, the ejbHandle will be useless as it just stores the reference to the bean and NOT the state of the bean. This statement contradicts your explanation. Could you please explain?
  4. Hi Karthik,
    A stateful session bean is created per client. If the client persists the ejbHandle, the bean will either be in ready pool or will get passivated depending on the poolsize. When the ejbHandle is activated and call is made using that ejbHandle, the container will bring the bean from the ready pool or activate the bean to server the client.

    When ejb server goes down or it crashes, there is no bean attached to the EJBObject for which the client is holding the reference. Also if the session timeout happens the bean will be destroyed by the container.

    I think the exception thrown for such cases is java.rmi.NoSuchObjectException.

  5. Hi[ Go to top ]

    I agree with you Hemant..it does depends upon the container state..if it has crashed, then it will loose the bean..

    if not..the container will pull out the bean from the pool to serve you :)