【问题标题】:Is context need to be closed explicity in LDAP connection pooling是否需要在 LDAP 连接池中显式关闭上下文
【发布时间】:2023-04-01 16:50:02
【问题描述】:

我们正在为 LDAP JNDI 连接池使用以下设置:

    DirContext ctx = null;
    try 
    {
        Hashtable<String, String> env = new Hashtable<String, String>();

        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldaps://" + server + ":" + serverPort);
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, pUserName);
        env.put(Context.SECURITY_CREDENTIALS, pPassword);
        env.put(LdapContext.CONTROL_FACTORIES, "com.sun.jndi.ldap.ControlFactory");
        env.put(Context.SECURITY_PROTOCOL, "ssl");
        env.put("com.sun.jndi.ldap.read.timeout", "300000");

        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

        // load the location of keystore that holds trusted root certificates from web.xml
        ServletContext context = ApplicationServlet.getApplication().getServlet().getServletContext();
        String certificatePath = context.getInitParameter("AD_CERTIFICATE_PATH");

        System.setProperty("javax.net.ssl.trustStore",  certificatePath);
        //          System.setProperty("javax.net.debug", "all");

        // For connection pooling
        env.put("com.sun.jndi.ldap.connect.pool", "true");
        System.setProperty("com.sun.jndi.ldap.connect.pool.protocol", "plain ssl");
        System.setProperty("com.sun.jndi.ldap.connect.pool.maxsize", poolMaxSize);
        System.setProperty("com.sun.jndi.ldap.connect.pool.prefsize", poolPrefSize);
        System.setProperty("com.sun.jndi.ldap.connect.pool.timeout", poolTimeOut);
        System.setProperty("com.sun.jndi.ldap.connect.pool.debug", "fine");

        ctx = new InitialDirContext(env);
        return (DirContext) ctx;

在 Active Directory 中执行搜索后,我们将使用 ctx.close() 显式关闭上下文以释放连接回池。

通过上述实现,我们面临的问题是连接后立即关闭:

12:06:14,837 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-22) Create and use com.sun.jndi.ldap.LdapClient@26a2a0eb[eun1p3-be.stp-prod.st.com:636]
12:06:16,855 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-22) Close com.sun.jndi.ldap.LdapClient@26a2a0eb
12:06:18,301 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-23) Create and use com.sun.jndi.ldap.LdapClient@76e26d4a[eun1p3-be.stp-prod.st.com:636]
12:06:20,353 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-23) Close com.sun.jndi.ldap.LdapClient@76e26d4a
12:06:21,713 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-19) Create and use com.sun.jndi.ldap.LdapClient@4bb50913[eun1p3-be.stp-prod.st.com:636]
12:06:23,746 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-19) Close com.sun.jndi.ldap.LdapClient@4bb50913
12:06:25,366 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-6) Create and use com.sun.jndi.ldap.LdapClient@2a2eecb7[eun1p3-be.stp-prod.st.com:636]
12:06:27,473 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-6) Close com.sun.jndi.ldap.LdapClient@2a2eecb7
12:06:28,757 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-25) Create and use com.sun.jndi.ldap.LdapClient@3c34b0d[eun1p3-be.stp-prod.st.com:636]
12:06:30,855 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-25) Close com.sun.jndi.ldap.LdapClient@3c34b0d
12:06:32,214 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-22) Create and use com.sun.jndi.ldap.LdapClient@6d9ca028[eun1p3-be.stp-prod.st.com:636]
12:06:34,294 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-22) Close com.sun.jndi.ldap.LdapClient@6d9ca028
12:06:35,730 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-23) Create and use com.sun.jndi.ldap.LdapClient@72ed6bb2[eun1p3-be.stp-prod.st.com:636]
12:06:37,753 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-23) Close com.sun.jndi.ldap.LdapClient@72ed6bb2
12:06:39,184 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-19) Create and use com.sun.jndi.ldap.LdapClient@e87ce30[eun1p3-be.stp-prod.st.com:636]
12:06:41,266 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-19) Close com.sun.jndi.ldap.LdapClient@e87ce30

【问题讨论】:

  • 是的,实际上,我对相同的代码提出了不同的问题。在这里我面临问题连接立即关闭。
  • 大家好,我已经检查过,但在互联网上没有得到太多信息。根据 oracle 文档,提到需要关闭上下文才能将其返回到池中。
  • BUt 在我们的例子中,在关闭连接后,我们看到没有连接达到最大限制“20”在我们的例子中..连接在创建和使用后立即关闭..在 1 秒内......我上面有共享日志...

标签: active-directory ldap jndi


【解决方案1】:

是的,您不仅要关闭所有Contexts,还要关闭所有NamingEnumerations。这就是连接返回池的方式。

您的日志中没有证据表明 连接 正在关闭。关闭的是com.sun.jndi.ldap.LdapClients.

【讨论】:

猜你喜欢
  • 2011-08-08
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
  • 2020-11-22
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
相关资源
最近更新 更多