When we create a new InitialContext for remote access to Enterprise Java Beans, after work is done, should we always close the context via context.close() ?
Here is a code sample:
// Client side method
private void doSomeActionMethod() {
RouteTransactionFacadeBeanRemote remote = null;
final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
Context context = null;
try {
context = new InitialContext(jndiProperties);
remote = (RouteTransactionFacadeBeanRemote) context
.lookup("ejb:EJBEAR/EJBServer//RouteTransactionFacadeBean!facade.RouteTransactionFacadeBeanRemote");
//DO SOMETHING WITH REMOTE EJB
} catch (NamingException e) {
e.printStackTrace();
} finally {
try {
// Should we always do this?
if (context != null) context.close();
} catch (NamingException ex) {
ex.printStackTrace();
}
}
}
From javadocs:
Although, garbage collector will collect them but instead of waiting for
gc, you shouldcloseit by the provided function. So, my answer is, yes you should close this as well as any other resource that should be closed.