Dependency injection and stateful session beans


EJB design: Dependency injection and stateful session beans

  1. Dear all, In the book "EJB 3 in Action" (by Debu Panda, Reza Rahman and Derek Lane) I've found the following passage: "Keep in mind that you must not inject a stateful session bean into a stateless object, such as stateless session bean or servlet that may be shared by multiple concurrent clients (you should use JNDI in such cases instead)." Could someone please elaborate on that ? I don't see how JNDI could help in such a case and why dependency injection is an issue. (I realize that using a stateful bean inside a stateless one is not very useful, since after each method call the stateless object "forgets the state".) Thank you, Greetings, Sorin
  2. Thats because, when dependency injection is used, container may inject a new Stateful session bean in stateless session bean, the same Stateful session bean may not be injected in all Stateless beans. When JNDI is used, the reference to a particular Statefull bean can be obtained and injected in all the stateless session beans. The same statefull bean will be used.
  3. But you don't want to inject a session bean (even the same instance) into several stateless beans, because the latter may be used by different clients who will gain thus access to inaccurate state in the stateful bean (state pertaining to a different client). So why would you inject (via JNDI or otherwise) stateful beans into stateless beans ? Sorin