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
posted
0
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.
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