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