users@glassfish.java.net

Re: GlassFish Cannot Find SFSBs After a RuntimeException

From: <glassfish_at_javadesktop.org>
Date: Thu, 26 Feb 2009 16:11:25 PST

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.
[Message sent by forum member 'ksak' (ksak)]

http://forums.java.net/jive/thread.jspa?messageID=334109