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.

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.

Mark


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

Reply via email to