为什么在 InitialContext 上调用 close() 会破坏所有未来查找的 JNDI (Glassfish)?
当作为独立应用程序针对 Glassfish(已配置为通过 JNDI 公开 QueueConnectionFactory
和 DataSource
)运行时,下面代码中的第二个 JNDI 查找失败并出现异常。但是,当删除 jndiContext.close()
行时,代码可以正常工作。
在实际代码中,调用 close()
是由 Spring 在 JndiObjectFactoryBean
,所以我无法轻易删除它。
这是 Glassfish 中的错误,还是我做错了什么(例如配置错误或编码不正确)?
import javax.naming.Context;
import javax.naming.InitialContext;
public class TestInitCtx {
private final static String QUEUE_CONNECTION_FACTORY_JNDI_NAME = "QCF";
private final static String DATA_SOURCE_JNDI_NAME = "DS";
public static void main(String[] args) throws Exception {
Context jndiContext = new InitialContext();
jndiContext.lookup(QUEUE_CONNECTION_FACTORY_JNDI_NAME);
// In Glassfish, this line causes the second lookup
// to throw a com.sun.enterprise.connectors.ConnectorRuntimeException
// (wrapping a NullPointerException)
jndiContext.close();
jndiContext = new InitialContext();
jndiContext.lookup(DATA_SOURCE_JNDI_NAME);
}
}
The second JNDI lookup in the code below fails with an exception when running as a standalone application against Glassfish (which has been configured to expose a QueueConnectionFactory
and a DataSource
via JNDI). However, the code works fine when the line jndiContext.close()
is removed.
In the real code, the call to close()
is being made by Spring in a JndiObjectFactoryBean
, so I can't easily remove it.
Is this a bug in Glassfish, or am I doing something wrong (e.g. misconfiguration or incorrect coding)?
import javax.naming.Context;
import javax.naming.InitialContext;
public class TestInitCtx {
private final static String QUEUE_CONNECTION_FACTORY_JNDI_NAME = "QCF";
private final static String DATA_SOURCE_JNDI_NAME = "DS";
public static void main(String[] args) throws Exception {
Context jndiContext = new InitialContext();
jndiContext.lookup(QUEUE_CONNECTION_FACTORY_JNDI_NAME);
// In Glassfish, this line causes the second lookup
// to throw a com.sun.enterprise.connectors.ConnectorRuntimeException
// (wrapping a NullPointerException)
jndiContext.close();
jndiContext = new InitialContext();
jndiContext.lookup(DATA_SOURCE_JNDI_NAME);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 JNDI 实现可能只支持 InitialContext 对象的单个静态实现。您可以使用 sun 处的文档来确定如何找出 JNDI 上下文工厂的实际具体类型,然后找到详细说明 close 功能的实现文档。
Your JNDI implementation may only support a single static implementation of the InitialContext object. You can use the documentation at sun to determine how to find out what the actual concrete type of the JNDI context factory is and then find the implementation docs that detail what close does.