Is javax.naming.InitialContext ThreadSafe
java, jndi, thread-safety
Solution
Answers with regard to threadsafety are usually already mentioned in javadoc, whenever relevant. And indeed, the `InitialContext` javadoc mentions the following:
An `InitialContext` instance is not synchronized against concurrent access by multiple threads. Multiple threads each manipulating a different `InitialContext` instance need not synchronize. Threads that need to access a single `InitialContext` instance concurrently should synchronize amongst themselves and provide the necessary locking.
The last sentence confirms it: it's not threadsafe and per-thread synchronization is necessary. In your particular code example, however, no synchronization is necessary as it's been created in method local scope anyway (i.e. it's definitely not shared among threads). If the `InitialContext` was in your particular code example been an instance variable, then you'd have to add the `synchronized` keyword to the `getEJB()` method.
Problem
Currently I am use following code to lookup EJB3 sateless session beans for normal POJO class. (We are in JEE5 so we can not inject Stateless Session Beans in normal POJO class I have to use look up) ``` import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.apache.log4j.Logger; public Object getEJB(String jndiName) { logger.debug("WEBSPHERE EJB Lookup : " + jndiName); String modifiedJndiName = ""; Hashtable<Object, Object> properties = new Hashtable<Object, Object>(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory"); properties.put(Context.PROVIDER_URL, "iiop://localhost:2809"); try { Context context = new InitialContext(properties); logger.debug("WEBSPHERE EJB Lookup Modified JNDI Name: " + modifiedJndiName); return context.lookup("ejblocal:"+modifiedJndiName); }catch (NamingException ne) { logger.debug("Naming Exception occurred :"+jndiName +">>>"+ne.getMessage()); logger.error(ne.getMessage(), ne); } return null; } ``` So is Context object is ThredSafe? should I create Context object for each call [as shown in this code snippet] or I can reuse the Context for all threads?