Providing JNDI Context Information

Be familiar with the JNDI specification to use jConnect with JNDI.

See the JNDI specification from Oracle Technology Network.

In particular, make sure that required initialization properties are set in javax.naming.directory.DirContext when JNDI and jConnect are used together. Set these properties either at the system level or at runtime.

The properties are:
  • Context.INITIAL_CONTEXT_FACTORY – takes the fully qualified class name of the initial context factory for JNDI to use. This determines the JNDI driver that is used with the URL specified in the Context.PROVIDER_URL property.

  • Context.PROVIDER_URL – takes the URL of the directory service that the driver (for example, the LDAP driver) is to access. The URL should be a string, such as “ldap://ldaphost:427”.

This example shows how to set context properties at runtime and how to get a connection using JNDI and LDAP. The INITIAL_CONTEXT_FACTORY context property is set to invoke the Oracle implementation of an LDAP service provider. The Context.PROVIDER_URL property is set to the URL of an LDAP directory service located on the host “ldap_server1” at port 389.
Properties props = new Properties();
 
 /* We want to use LDAP, so INITIAL_CONTEXT_FACTORY is set to the
 * class name of an LDAP context factory. In this case, the 
 * context factory is provided by Sun’s implementation of a 
 * driver for LDAP directory service.
 */
 props.put(Context.INITIAL_CONTEXT_FACTORY,
   "com.sun.jndi.ldap.LdapCtxFactory");
 
 /* Now, we set PROVIDER_URL to the URL of the LDAP server that 
 * is to provide directory information for the connection.
 */
 props.put(Context.PROVIDER_URL, "ldap://ldap_server1:389");
 
 /* Set up additional context properties, as needed. */
 props.put("user", "xyz");
 props.put("password", "123");
 
 /* get the connection */
 Connection con = DriverManager.getConnection
   ("jdbc:sybase:jndi:ldap://ldap_server1:389" +
   "/servername=Sybase11,o=MyCompany,c=US",props); 

The connection string passed to getConnection contains LDAP-specific information, which the developer must provide.

When JNDI properties are set at runtime, as in the preceding example, jConnect passes them to JNDI to be used in initializing a server, as in this jConnect code:
javax.naming.directory.DirContext ctx =
   new javax.naming.directory.InitialDirContext(props);
jConnect then obtains the connection information it needs from JNDI by invoking DirContext.getAtributes, as in this example, where ctx is a DirContext object:
javax.naming.directory.Attributes attrs =
   ctx.getAttributes("ldap://ldap_server1:389/servername=" +
     "Sybase11", SYBASE_SERVER_ATTRIBUTES);

SYBASE_SERVER_ATTRIBUTES is an array of strings defined within jConnect. The array values are the OIDs for the required directory information listed in Required Directory Service Information .