是javax.naming.InitialContext ThreadSafe [英] Is javax.naming.InitialContext ThreadSafe

查看:233
本文介绍了是javax.naming.InitialContext ThreadSafe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我使用以下代码来查找正常POJO类的EJB3 sateless会话bean。 (我们在JEE5,所以我们不能在正常的POJO类中注入无状态会话Bean我必须使用查找)

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;  
            }  

因此Context对象是ThredSafe吗?我应该为每个调用创建Context对象[如此代码片段所示],还是我可以为所有线程重用Context?

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?

推荐答案

关于线程安全的答案通常在javadoc中已经提到,只要相关。事实上, InitialContext javadoc 提到以下内容:

Answers with regard to threadsafety are usually already mentioned in javadoc, whenever relevant. And indeed, the InitialContext javadoc mentions the following:


InitialContext 实例未与多个线程的并发访问同步。每个操作不同的 InitialContext 实例的多个线程无需同步。需要同时访问单个 InitialContext 实例的线程应该在它们之间进行同步并提供必要的锁定。

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.

最后一句确认了它:它不是线程安全的,并且每线程同步是必要的。但是,在您的特定代码示例中,不需要同步,因为它始终在方法本地范围中创建(即它绝对不在线程之间共享)。如果您的特定代码示例中的 InitialContext 是一个实例变量,那么您必须添加 synchronized 关键字到 getEJB()方法。

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.

这篇关于是javax.naming.InitialContext ThreadSafe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆