简体   繁体   English

为什么在InitialContext上调用close()会破坏所有未来查找的JNDI(Glassfish)?

[英]Why does calling close() on an InitialContext break JNDI for all future lookups (Glassfish)?

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). 当作为针对Glassfish的独立应用程序(已配置为通过JNDI公开QueueConnectionFactoryDataSource )运行时,下面代码中的第二个JNDI查找失败并出现异常。 However, the code works fine when the line jndiContext.close() is removed. 但是,删除行jndiContext.close()时代码工作正常。

In the real code, the call to close() is being made by Spring in a JndiObjectFactoryBean , so I can't easily remove it. 在实际的代码,调用close()是由春在做JndiObjectFactoryBean ,所以我不能轻易将其删除。

Is this a bug in Glassfish, or am I doing something wrong (eg misconfiguration or incorrect coding)? 这是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);          
    }
}

Your JNDI implementation may only support a single static implementation of the InitialContext object. 您的JNDI实现可能只支持InitialContext对象的单个静态实现。 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. 您可以使用sun中的文档来确定如何找出JNDI上下文工厂的实际具体类型,然后找到详细说明close的功能的实现文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM