Mark,

On 12/17/25 2:53 PM, Mark Thomas wrote:
On 17/12/2025 15:46, Rémy Maucherat wrote:
On Wed, Dec 17, 2025 at 4:38 PM Mark Thomas <[email protected]> wrote:

On 17/12/2025 15:14, Christopher Schultz wrote:
All,

I had occasion to write some code to traverse the JNDI tree for JDBC
resources and list them. My code looks something like this:

InitialContext ictx = null;
Context ctx = null;

try {
    ictx = new InitialContext();

    ctx = ictx.lookup("java:comp/env/jdbc");

    ...
} finally {
      if(null != ctx) try { ctx.close(); } catch () ...
      if(null != ictx) try { ictx.close(); } catch () ...
}

This code is throwing an exception:

javax.naming.OperationNotSupportedException: Context is read only
      at
org.apache.naming.NamingContext.checkWritable(NamingContext.java:637)
      at org.apache.naming.NamingContext.close(NamingContext.java:430)
          at [... my code ...]

Examples online from Sun/Oracle seem to indicate that calling
Context.close() when code is finished with a Context resource is
appropriate.

Should this method be throwing an exception?

It looks like the call to checkWritable() is unnecessary in close().

You said that about it before:
https://bz.apache.org/bugzilla/show_bug.cgi?id=51744#c1

Thanks for finding the history. There is a lot of background information there I had forgotten.

:D

We definitely don't want calls to close() to break subsequent lookups.

We don't really want to be creating a new NamingContext every time it is looked up from the InitialContext. Do we? But is that what the usage guides are effectively implying?

The current implementation has been in place for a while so maybe not calling close isn't an issue.

I can certainly just NOT call close, here. I can also add that weird attribute to my <Context> that I never knew existed but I'm more likely to remove the close() calls.

The reference from Oracle I found is here:

https://docs.oracle.com/javase/tutorial/jndi/ldap/close.html

It does state that it was written for Java 8 and maybe things aren't the same anymore, but I'm unable to find a more modern reference from Oracle.

All the other discussions I can find online say:

1. You should definitely close Context objects in your code
2. ... unless you shouldn't
3. There is a lot of confusion surrounding this; see items #1 and #2 above

So I'm not sure much is settled, other than the fact that we haven't had any complaints for the past 14 years.

So maybe leaving things alone is the right move :)

-chris


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to