Using a stored Handle after timeout of an SFSB


EJB programming & troubleshooting: Using a stored Handle after timeout of an SFSB

  1. Using a stored Handle after timeout of an SFSB (1 messages)


    I have an web appliction that stores a Stateful SessionBean's handle in a session scope attribute.

    When my servlet tries to re-use the bean by obtaining it through the Handle, it works if the bean's is still avail. in the container.

    If the bean had been removed due to a timeout for example, I can get a ref. to it via the Handle without any problems, but if I call a method on it, I get a java.rmi.NoSuchObjectException.

    Here is an idea of what the code looks like:

    Handle handle = session.getAttribute("myHandle");
    TravelAgent agent = (TravelAgent) PortableRemoteObject.narrow(handle.getEJBObject(), TravelAgent.class);
    Collection listing;

    try {
       listing = agent.getListing();
    catch (NamingException e){ ...
    } catch (NoSuchObjectException e) {
       Object objHome = ctx.lookup("ejb/TravelAgent");
       TravelAgentHome = (TravelAgentHome) PortableRemoteObject.narrow(objHome, TravelAgentHome.class);
       agent = home.create();
       listing = agent.getListing();
       session.setAttribute("myHandle", agent.getHandle());

    I understand why I get the NoSuch ObjectException but I would like to know if there is a better way to handle a NoSuchObjectException resulting from a SFSB timeout?
  2. The mechanism you is about the best you can.

    In general, though, I suggest you don't use a Stateful Session Bean at all. You can simplify your application by storing temporary data in the servlet session, and permanent data in the database (for example, but sending it to entity beans). If you take this approach, you session data will be available for as long as you servlet session is available, and the Stateful Session Bean timeout issue goes away.