users@glassfish.java.net

Do not use Injection in Servlets with Stateful Beans and Timeouts

From: <glassfish_at_javadesktop.org>
Date: Wed, 31 Oct 2007 04:37:33 PST

Hi,

1) If I understand it correctly, the class fields in a Servlet can be shared across multiple instances. When the EJB is stateful, than this bean will be shared across all instances. When I initialize the servlet with Principal user 1, then the SfullSB will be using this user. When Principal user 2 comes, the SfullSB already exists and uses the wrong user. Anyway, both users will use the same SfullSB ! [b]Please, correct me if I am wrong. [/b]

public class CacheServlet extends HttpServlet {
    
    @EJB
    private StructureSessionLocal structureSession; // SSB -> Wrong, will be shared across all calls which arrive in this servlet instance
  
....

}


2) Even if you use this approach (even though in 99% of all cases, this SfullSB it will not work correctly), what happens when the SfullSB has a timeout? Does the servlet refreshes the reference when a timeout occurs? In the case of Stateless Beans, there is no problem, I suppose?

---
The documentation about EJB3 is very cumbersome regarding this point, hence my post. I think many people use injection because it is easy, but do not realize that placing it in a class-field can be very dangerous, even though everything may seem to run as planned. Or I am going bananas or missed the point here?
Greetings
Jan De Cooman
[Message sent by forum member 'ossaert' (ossaert)]
http://forums.java.net/jive/thread.jspa?messageID=243100