views:

386

answers:

1

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

A: 

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.

Jherico
OK, but Spring is calling close() on the context for me. Do I have to forego using Spring if my InitialContext object is one of these static ones?
Simon Nickerson