为什么 DirContext.close() 不返回与池的 LDAP 连接?



我注意到在使用 LDAP 连接池时,在上下文中调用close()似乎并没有将其返回到池中,尽管文档另有说明。因此,当我尝试从池中获取一个项目时,当它已经达到最大大小时,它会挂起。

我设法将其缩小到最小的情况。尽管我相信我正在确定性地调用所有相关对象的close(),但它似乎依赖于垃圾收集来实际将对象返回到池中,这是出乎意料的。为什么会这样?我应该关闭其他对象吗?

在下面的代码片段中:

  • 我人为地将最大池大小设置为 1 以突出显示问题。
  • 我从池中获取一个DirContext(第 (2 行)),尝试将其返回到池(第 (4 行)),然后从池中获取另一个(第 (6)行),它应该返回相同的返回对象。
  • 相反,第二个请求(第 (6) 行)挂在对 Object.wait() 的某个内部调用上。我猜它正在等待池化对象可用。
  • 如果通过注释掉 (1) 关闭池化,它不会挂起(但我想要池化!
  • 如果我注释掉 (3) - 调用SearchResults.next() - 它工作正常。
  • 如果我取消注释行 (5) 以在"返回到池"调用和向池请求新对象之间强制垃圾收集,它不会挂起。

由于注释掉第 (3) 行会使问题消失,也许我没有正确关闭它的返回值,并且它使池连接保持打开状态。但是,在这种情况下,该方法results.next()返回一个SearchResult,该方法没有close方法,其文档中也没有有关如何干净关闭它的指导。

测试用例:

@Test
public void testHangs() throws NamingException {
    System.setProperty("com.sun.jndi.ldap.connect.pool.debug", "fine");
    System.setProperty("com.sun.jndi.ldap.connect.pool.maxsize", "1");
    Hashtable<String,String> env = new Hashtable<String,String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.SECURITY_PRINCIPAL, user);
    env.put(Context.SECURITY_CREDENTIALS, passwd);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.PROVIDER_URL, ldapUrl);
    // use a connection pool
    env.put("com.sun.jndi.ldap.connect.pool", "true"); // -----------------  (1)
    // get a context from the pool.
    DirContext context = new InitialDirContext(env); // -------------------- (2)
    NamingEnumeration<SearchResult> results = context.search("", query, getSC());
    // obviously the next two lines would normally be in a 
    // while(results.hasMore()) { ... = results.next(); } loop.
    assertTrue(results.hasMore()); // this is only a problem when there are some results.
    results.next(); // ----------------------------------------------------- (3)
    // ensure the context is returned to the pool.
    results.close();
    context.close(); // ---------------------------------------------------- (4)
    //System.gc(); // ------------------------------------------------------ (5)
    new InitialDirContext(env);  // hangs here! ---------------------------- (6)
}

使用原样代码,我的控制台显示:

Create com.sun.jndi.ldap.LdapClient@1a7bf11[ldapad:389]
Use com.sun.jndi.ldap.LdapClient@1a7bf11

然而,如果我强制使用 GC,我还会看到:

Release com.sun.jndi.ldap.LdapClient@93dee9 <-- on GC
Use com.sun.jndi.ldap.LdapClient@93dee9     <-- on new InitialDirContext

经过一番调查,我发现LDAP连接没有返回到池中,因为SearchResult对象包含对LdapCtx对象的引用。

如果替换

results.next();

与以下

SeachResult ob = results.next();
((Context)ob.getObject()).close();

连接将正确返回到池中。这似乎是默认实现中的一个错误。

使用Spring LDAPTemplate时不存在这个问题,因为它在环境中提供了一个自定义的"java.naming.factory.object",该环境关闭了LdapCtx作为构建搜索结果过程的一部分。这可以通过将Spring LDAP库添加到类路径并将以下内容添加到InitialContext中来轻松演示

java.naming.factory.object=org.springframework.ldap.core.support.DefaultDirObjectFactory

完成此操作后,SearchResult 保存的对象将从 com.sun.jndi.ldap.LdapCtx:com.sun.jndi.ldap.Ldap.LdapCtx 更改为 org.springframework.ldap.core.DirContextAdapter负责创建 DirContextAdapter,并在将 DirContextAdapter 返回到 DirectoryManager 之前关闭 LdapCtx。这是来自 DefaultDirObjectFactory 的最后一个块

finally {
        // It seems that the object supplied to the obj parameter is a
        // DirContext instance with reference to the same Ldap connection as
        // the original context. Since it is not the same instance (that's
        // the nameCtx parameter) this one really needs to be closed in
        // order to correctly clean up and return the connection to the pool
        // when we're finished with the surrounding operation.
        if (obj instanceof Context) {
            Context ctx = (Context) obj;
            try {
                ctx.close();
            }
            catch (Exception e) {
                // Never mind this
            }
        }
    }

SearchControls对象更改为 returningObjFlag 属性 false。通常不需要对象本身,只需要它的名称 InNamespace 及其属性。如果要创建或修改子上下文,则只需要对象本身。

最新更新