Wednesday, 25 May 2016

JNDI: Connection pooling


In my previous posts, I explained hot to connect to directory server, how to perform create, delete, query and update operations on directory server. Every time when I want to connect to Directory server, I am initializing DirContext object like below.
import java.util.Objects;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class DirectoryUtil {
 private final static String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
 private final static String PROVIDER_URL = "ldap://localhost:10389";
 private static DirContext context = null;

 public static DirContext getContext() throws NamingException {
  if (!Objects.isNull(context)) {
   return context;
  }

  Properties properties = new Properties();
  properties.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
  properties.put(Context.PROVIDER_URL, PROVIDER_URL);
  DirContext context = new InitialDirContext(properties);
  return context;
 }
}

One problem with above program is, each time an initial context is created, a new LDAP connection is created. By using connection pooling, we can share the connection. In this technique, LDAP service provider maintains a pool of connections; use them whenever they are needed. Once the connection objects are done with their work, these object come back to pool and wait for another turn.

How to create connection pooling?

Set the property “com.sun.jndi.ldap.connect.pool” to true, you are done.
import java.util.Properties;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class DirectoryUtil {
 private final static String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
 private final static String PROVIDER_URL = "ldap://localhost:10389";
 private static final Properties properties = new Properties();

 static {
  initProperties();
 }

 private static void initProperties() {
  properties.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
  properties.put(Context.PROVIDER_URL, PROVIDER_URL);
  properties.put("com.sun.jndi.ldap.connect.pool", "true");
 }

 public static DirContext getContext() throws NamingException {

  DirContext context = new InitialDirContext(properties);
  return context;
 }
}

import javax.naming.NamingException;
import javax.naming.directory.DirContext;

public class Test {

 public static void main(String[] args) throws NamingException {
  DirContext context1 = DirectoryUtil.getContext();

  context1.getEnvironment();
  context1.close();

  DirContext context2 = DirectoryUtil.getContext();
  context2.close();

  DirContext context3 = DirectoryUtil.getContext();
  context3.close();
 }
}

Run the program using following command.
java -Dcom.sun.jndi.ldap.connect.pool.debug=fine Test


You can able to see following kind of output.
Create com.sun.jndi.ldap.LdapClient@6d6f6e28[localhost:10389]
Use com.sun.jndi.ldap.LdapClient@6d6f6e28
Release com.sun.jndi.ldap.LdapClient@6d6f6e28
Use com.sun.jndi.ldap.LdapClient@6d6f6e28
Release com.sun.jndi.ldap.LdapClient@6d6f6e28
Use com.sun.jndi.ldap.LdapClient@6d6f6e28
Release com.sun.jndi.ldap.LdapClient@6d6f6e28


How to set this command line option in Eclipse?
Right click on Test.java, Run As -> Run Configurations. Click on Arguments tab and Under the VM arguments section add "-Dcom.sun.jndi.ldap.connect.pool.debug=fine".





Previous                                                 Next                                                 Home

1 comment: