File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
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: 3892
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Any Risk in Sharing the JNDI Context?