• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

EJB staless/stateful beans and JNDI

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author & internet detective
Posts: 42151
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic