Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Any Risk in Sharing the JNDI Context?

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic