The Source for Java Technology Collaboration

Home » java.net Forums » GlassFish » GlassFish

Thread: GlassFish Cannot Find SFSBs After a RuntimeException

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 1 - Last Post: Feb 26, 2009 4:11 PM by: ksak
bsevindi

Posts: 9
GlassFish Cannot Find SFSBs After a RuntimeException
Posted: Feb 26, 2009 8:50 AM
  Click to reply to this thread Reply

Hello,

When an uncaught runtime exception is thrown in the middle of an SFSB operation, GlassFish cannot find that SFSB on consequent calls that are made from the same client (which is a servlet in my situation), and throws a javax.ejb.NoSuchEjbException.

I know it is acceptable because the client has lost the SFSB reference because of the runtime exception. But the problem does not disappear even when the calls are made from a new session (I assume a new session starts when I restart my browser and request the servlet again). GlassFish continues to complain of no such ejb.

The following example illustrates the problem.

@Local
public interface SimpleEjb {
  String aMethod();
  String bMethod();
}

@Stateful
public class SimpleEjbImpl implements SimpleEjb {
  public String aMethod() {
    throw new RuntimeException("Deliberately thrown");
  }

  public String bMethod() {
    return "SimpleEjb.bMethod";
  }
}


This is my ejb component. When I request a servlet that calls aMethod; RuntimeException is thrown and operation fails. After that I restart my browser and request the same servlet, this time it should call bMethod, but a NoSucEjbException is thrown. Here is my servlet implementation:

public class SimpleServlet extends HttpServlet {

  private static boolean isFirstRequest = true;

  @EJB private SimpleEjb simpleEjb;

  protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    String msg = null;
    if(isFirstRequest) {
      isFirstRequest = false; // this is set before the exception is thrown
      simpleEjb.aMethod(); // throws RuntimeException
    }
    else {
      msg = simpleEjb.bMethod();
     }

     req.setAttribute("msg", msg);
     RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp");
     rd.forward(req, res);
  }
}

I have attached the ear that contains my ejb and web modules and source code.

ksak

Posts: 455
Re: GlassFish Cannot Find SFSBs After a RuntimeException
Posted: Feb 26, 2009 4:11 PM   in response to: bsevindi
  Click to reply to this thread Reply

This is the spec-defined behavior for stateful session beans. Each injection of a stateful
session bean reference results in a new stateful session bean identity. Here, the "client" is the
servlet instance itself, of which there is typically only one per web application. If the
bean instance throws a runtime exception the container must destroy it, which
means any other invocations on its reference will result in NoSuchEjbException.

This is one reason it's not recommended to inject a stateful session bean into a servlet instance.
It's better to store the stateful reference in some other clearly defined scope such as the
HttpSession or web application context. You can create a new stateful session bean dynamically by looking up the ejb dependency. To make it easier, just add name="simpleRef" to @EJB. Then, anywhere within the web application do the following :

SimpleEjb simple = (SimpleEjb) new InitialContext().lookup("java:comp/env/simpleRef");

Note that this won't change the fact that if the bean referred to by the reference returned from
the lookup throws a runtime exception it will be destroyed.




 XML java.net RSS