« November 2009
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     
       
Today

FEEDS

SEARCH



LINKS




CONTACT
jaya_hangal
Template by
Helquin

Monday Dec 05, 2005

Problems with Pooling when using JNDI?

I am writing a blog on this topic because many advance users of JNDI often have problems in getting the JNDI/LDAP pooling work as expected Here I will try to provide some tips that will help in self analysis of the application design.

JNDI provides a mechanism where by the connections used by different contexts can be pooled for the purposes of sharing. Pooling is required in production systems that need to create thousands of connections.

The pooling mechanism requires that a JNDI application release resources i.e. Context references held by it by calling Context.close() and any NamingEnumeration instances created by the Context must also be closed. Closing the Context or the NamingEnumeration objects indicates to the pooling mechanism that the specific use of a connection by the Context object is over, and the connection can now be reused by a new Context that is waiting for it.

Keeping track of all the Context instances becomes cumbersome in a complex application. To aid tracking of Context objects in an application, I will try to enumerate below all the cases when a new reference to a Context is created
Developers can use this as a checklist when the connections are not being reused or released by the JNDI/LDAP pooling mechanism.

  1. A Context object is obtained when lookup() methods and their variants are invoked.
    • Context.lookup()
    • Context.lookupLink()

    The lookup() method or any other API methods mentioned below returns by default a Context object unless the Service Provider finds an approrpiate Object Factory configured by the Application.

  2. A Context object is returned by invoking createSubcontext() and its variant methods:
    • Context.createSubcontext()

  3. A NamingEnumeration obtained as a result of any of the below API method invocation and their variants must be closed:
    • Context.list()
    • Context.listBindings()
    • DirContext.search()

  4. It's not enough if the NamingEnumeration is closed, a next step is required in some cases. And some developers may not realise this step (as it's not obvious) When any of the following API methods are invoked on a Context:
    • Context.listBindings()
    • DirContext.search()

    and the search() method is configured to return Objects as below:

            SearchControls ctl = new SearchControls();
    ctl.setReturningObjFlag(true);

    The Object returned by the search() or the listBindings() methods could be of type Context and that needs to be closed as well:

        NamingEnumeration ne = ctx.search(...);
    while (ne.hasNext()) {
    SearchResult sr = ne.next();
    Context ctxObj = (Context) sr.getObject());

    // do whatever is required with ctxObj

    ctxObj.close();
    }

  5. A DirContext is returned when the below Schema retrieval methods are invoked on it:
    • DirContext.getSchema()
    • DirContext.getSchemaClassDefinition()

  6. A InitialLdapContext is used and a new instance of it is obtained by invoking the method:
    • InitialLdapContext.newInstance()



Comments:

Post a Comment:
  • HTML Syntax: NOT allowed