Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is javax.naming.InitialContext ThreadSafe

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?

like image 310
Debopam Avatar asked Nov 10 '12 00:11

Debopam


People also ask

What is javax naming InitialContext?

The javax. naming. InitialContext class implements the Context interface and serves as our entry point to a naming system. To use JNDI to access objects in a naming system, you must first create an InitialContextobject. The InitialContext constructor takes a set of properties, in the form of a java.

What is InitialContext in JMS?

public class InitialContext extends Object implements Context. This class is the starting context for performing naming operations. All naming operations are relative to a context. The initial context implements the Context interface and provides the starting point for resolution of names.

What is synchronization what is thread safe?

Synchronized: only one thread can operate at same time. Threadsafe: a method or class instance can be used by multiple threads at the same time without any problems occurring.


1 Answers

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.

like image 194
BalusC Avatar answered Sep 21 '22 00:09

BalusC