Monday, 10 January 2011

Closing InitialContexts in Weblogic

In Weblogic (at least), when you create an initial context, it gets attached to a thread presumably using a ThreadLocal stack. This means that you can only have one current InitialContext at any one time and that if you do something like:

  • Create a InitialContext and use it
  • Create another InitialContext and use that...
  • Reuse your first InitialContext

then your last call to reuse your first initial context will use your second initial context.

At least that’s the theory, but it gets even more confusing when one Context uses security and the other doesn’t.

SO, the big idea here is to ALWAYS close your initial context after use:

Context ctx = new InitialContext(props)
:
:
:
ctx.close();

For more information take a look at the Weblogic documentation on this