IntelliJ Java IDE
The moose likes EJB and Other Java EE Technologies and the fly likes Any Risk in Sharing the JNDI Context? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » EJB and Other Java EE Technologies
Reply Bookmark "Any Risk in Sharing the JNDI Context?" Watch "Any Risk in Sharing the JNDI Context?" New topic
Author

Any Risk in Sharing the JNDI Context?

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Is it safe to save a JNDI InitialContext as a ServletContext attribute, and then reuse it across all servlets in a web application?
Every code sample I see in my O'Reilly book on EJBs re-obtains a JNDI InitialContext in each method that needs one, as opposed to sharing it as an instance variable or as a ServletContext attribute.
Thanks in advance.
------------------
Miftah Khan
- Sun Certified Programmer for the Java� 2 Platform
- Sun Certified Web Component Developer for the Java� 2 Platform, Enterprise Edition
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3878
Yes, there is a risk. The JNDI Context is not defined by the spec as being threadsafe, which leaves it up to the vendor's discretion. In fact, many implementions of JNDI do not provide threadsafe InitialContext objects. So, the O'Reilly books are following a best practice. Don't cache InitialContexts, and get a new one every time you need one.
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thank you.
I was hoping you'd write back.
------------------
Miftah Khan
- Sun Certified Programmer for the Java� 2 Platform
- Sun Certified Web Component Developer for the Java� 2 Platform, Enterprise Edition
 
 
subject: Any Risk in Sharing the JNDI Context?
 
Threads others viewed
Certification or Degree??
SCJP 6
cleared scwcd exam with 62%
SCWCD with 71%
about resource reference for ejb
developer file tools