Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes EJB and other Java EE Technologies and the fly likes EJB staless/stateful beans and JNDI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB staless/stateful beans and JNDI" Watch "EJB staless/stateful beans and JNDI" New topic
Author

EJB staless/stateful beans and JNDI

Sorin Alexandru
Greenhorn

Joined: Nov 30, 2008
Posts: 28
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
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30138
    
150

I wouldn't call a stateful bean from inside a stateless bean at all because as you noted it doesn't make sense. A servlet calling a stateful bean does make sense so I'm going to assume it works the same way.

A servlet is called by multiple clients. It is common to have only one instance of the servlet that processes all requests. However dependency injection would inject the stateful bean only once and multiple callers would use it. By looking up the stateful bean from JNDI each time, you are giving the container a clue that it should look for the appropriate one and not necessarily the last one used.

Interestingly, Spring solves this problem by introducing a scope for request specific objects.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB staless/stateful beans and JNDI
 
Similar Threads
DI doubt in Session Bean
Bean reference in case of Stateless Session bean.
Caching EJB3 remote references prevents pooling or not
Dependency injection and stateful session beans
How to persist a Stateless Session Bean over several jsp pages?