![]() ![]() ![]() ![]() |
Frequently Asked Questions |
What is the relationship between a context and the connection to the LDAP server?
When you create an InitialContext, InitialDirContext
, or InitialLdapContext
using the LDAP service provider, a connection is set up immediately with the target LDAP server. Any contexts and NamingEnumerations
that are derived from this initial context share the same connection as the initial context.
For example, if you invoke Context.lookup()
or Context.listBindings()
from the initial context and get back other contexts, all those contexts would share the same connection. If you create a new initial context, it would have its own connection.
When you change environment properties that are related to a connection, such as the principal name or credentials of the user, the context upon which you make these changes would get its own connection (if the connection was being shared). Contexts that are derived from this context in the future will share this new connection, but contexts that previously shared the context's connection are not affected (that is, they continue to use the old connection).
Similarly, if you use the LdapContext.reconnect()
method, the context instance upon which you invoke this method will get its own connection if the connection was being shared.
How do I close the connection to the LDAP server?
To close the connection to the server, you invoke Context.close()on all contexts originated from the initial context that created the connection. Make sure that all NamingEnumeration have been completed. For those context and enumeration objects that are no longer in scope, the Java runtime system will eventually garbage collect them, thus cleaning up the state that a close() would have done. If you want to force the garbage collection, you can use the following code:
Runtime.getRuntime().gc(); Runtime.getRuntime().runFinalization();Is the context safe for multithreaded access? Or do I need to lock/synchronize access to a context?
The answer depends on the implementation because the Contextand DirContext
interfaces do not specify synchronization requirements. Sun's LDAP implementation is optimized for single-threaded access. If you have multiple threads accessing the same context, each thread needs to lock the context when using it. This also applies to any NamingEnumeration that is derived from the same context. However, multiple threads can access different contexts (even ones derived from the same initial context) concurrently without locks.
Why does the LDAP provider ignore my security environment properties if I do not set the Context.SECURITY_CREDENTIALS
If you supply an empty string, an empty byte/char array, or null to the Context.SECURITY_CREDENTIALS environment property, anonymous bind will occur regardless of the setting of the other security-related environment properties. This is because the LDAP requires the password to be nonempty for doing any type of authentication; if a password is not supplied, the protocol automatically converts the authentication to "none".("java.naming.security.credentials") property or set it to the empty string?
I keep getting CommunicationException
Perhaps you are talking to a server that supports only the LDAP version 2. See the Miscellaneouswhen I try to create an initial context. Why?
lesson for an example of how to set the version number.
I'm seeing some strange behavior, how do I find out what's really going on?
Try using the com.sun.jndi.ldap.trace.ber environment property. If the value of this property is an instance of java.io.OutputStream, trace information about BER buffers sent and received by the LDAP provider is written to that stream. If the property's value is null, no trace output is written.For example, the following code will send the trace output to System.err.
env.put("com.sun.jndi.ldap.trace.ber", System.err);How do I use a different authentication mechanism such as Kerberos?
First you need to have Java classes that support Kerberos and/or GSSAPI. Then you need to follow the instructions in the Securitylesson for making a SASL mechanism implementation available to the LDAP provider.
![]() ![]() ![]() ![]() |
Frequently Asked Questions |