When to close the JNDI context

When we view an object through JNDI, we do something like this:

public SomeResult doSomething() {
  Context ctx;
  try {
    ctx = new InitialContext(properties);
    SomeClass someObject = (SomeClass) ctx.lookup("jndiName");
    SomeResult someResult = someObject.getResult();
    return someResult
  } finally {
      ctx.close();
  }
}

A colleague did it a little differently:

public SomeClass getSomeClass() {
  Context ctx;
  SomeClass someObject = null;
  try {
    ctx = new InitialContext(properties);
    SomeClass someObject = (SomeClass) ctx.lookup("jndiName");
    return someObject
  } finally {
    ctx.close();
  }
}

It made me wonder which one to use? Is the returned object valid even after the context is closed? Does this work through purely coincidence or search-only context, and the object (like a proxy for ejb) is self-sustaining?

I tried to find something in the JNDI-Spec ... but got nothing ... but I can just be blind ,-)

What are the best methods for finding EJB through JNDI and using it and for what reasons.

+4
source share
1 answer

, , . .

+1

All Articles