Is InitialContext.close neccessary in JBoss 7.1?
shadowcreeper Jun 13, 2012 6:24 PMInitialStateException is always thrown when calling a remote bean's method if InitialContext was closed first (when bean was looked up using PROVIDER_URL).
Here is the context setup:
{code}// stand-alone integration test (not run from within JBoss, and no jboss-ejb-client.properties available in the class path)
Properties jndiProperties = new Properties();
jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
jndiProperties.put(Context.PROVIDER_URL, "remote://localhost:4447");
jndiProperties.put("jboss.naming.client.ejb.context", true);
jndiProperties.put(Context.SECURITY_PRINCIPAL, "login");
jndiProperties.put(Context.SECURITY_CREDENTIALS, "password");
InitialContext context = new InitialContext( jndiProperties );{code}
This works:
{code}// stand-alone integration test (not run from within JBoss, and no jboss-ejb-client.properties available in the class path)
try
{
MyRemoteBean myRemoteBean = (MyRemoteBean)context.lookup( "my-ear/my-jar/MyBeanEJB!my.bean.MyRemoteBean" );
myRemoteBean.doSomething();
}
finally
{
context.close();
}{code}
This does not work (IllegalStateException gets thrown):
{code}// stand-alone integration test (not run from within JBoss, and no jboss-ejb-client.properties available in the class path)
MyRemoteBean myRemoteBean;
try
{
myRemoteBean = (MyRemoteBean)context.lookup( "my-ear/my-jar/MyBeanEJB!my.bean.MyRemoteBean" );
}
finally
{
context.close();
}
myRemoteBean.doSomething(); // IllegalStateException gets thrown here{code}
Previously (in JBoss5, and also in JBoss 7.1.1.Final when using the jboss-ejb-client.properties method) I would always close the Context after bean lookup (in the finally block as you can see above). Apparently, using the PROVIDER_URL method, this causes the bean to disconnect causing the following log entry:
INFO [org.jboss.as.naming] (Remoting "tribble" task-4) JBAS011806: Channel end notification received, closing channel Channel ID 682e6420 (inbound) of Remoting connection 43c9b57e to /127.0.0.1:50152
Is this a bug or is this expected?
I have also noticed that none of the examples I have seen ever show the context being closed. I generally see: return new InitialContext(props).lookup(bean);
Do we even need to close the context?
Is there any risk of leaks if we don't close the context?