java - 每次从 Ldap 连接池返回新的 Connection

标签 java active-directory ldap connection-pooling

我在我的项目中实现了 LDAP 连接池,并注意到一个奇怪的行为,即每次新的连接请求到来时,我配置的 LDAP 连接池都会返回新连接,而不是重新使用返回到池中的现有连接。

LDAP JNDI 日志:

00:07:10,824 ERROR [stderr] (IPAdminGlobalDataReloader) Create and use com.sun.jndi.ldap.LdapClient@46728c0f[eun2p3-be.stp-qa.st.com:636]
00:07:12,222 ERROR [stderr] (IPAdminGlobalDataReloader) Release com.sun.jndi.ldap.LdapClient@46728c0f
00:07:46,704 ERROR [stderr] (Thread-65) Expired com.sun.jndi.ldap.LdapClient@674b68bd expired
00:08:46,707 ERROR [stderr] (Thread-65) Expired com.sun.jndi.ldap.LdapClient@46728c0f expired
00:22:26,329 ERROR [stderr] (IPAdminGlobalDataReloader) Create com.sun.jndi.ldap.LdapClient@386bfda[eun2p3-be.stp-qa.st.com:636]
00:22:26,333 ERROR [stderr] (IPAdminGlobalDataReloader) Create and use com.sun.jndi.ldap.LdapClient@6d9f3716[eun2p3-be.stp-qa.st.com:636]
00:22:27,748 ERROR [stderr] (IPAdminGlobalDataReloader) Release com.sun.jndi.ldap.LdapClient@6d9f3716
00:22:46,730 ERROR [stderr] (Thread-65) Expired com.sun.jndi.ldap.LdapClient@386bfda expired
00:23:46,734 ERROR [stderr] (Thread-65) Expired com.sun.jndi.ldap.LdapClient@6d9f3716 expired
00:37:45,242 ERROR [stderr] (IPAdminGlobalDataReloader) Create com.sun.jndi.ldap.LdapClient@4a21c217[eun2p3-be.stp-qa.st.com:636]
00:37:45,244 ERROR [stderr] (IPAdminGlobalDataReloader) Create and use com.sun.jndi.ldap.LdapClient@1b79ab6f[eun2p3-be.stp-qa.st.com:636]
00:37:46,759 ERROR [stderr] (Thread-65) Expired com.sun.jndi.ldap.LdapClient@4a21c217 expired
00:37:46,823 ERROR [stderr] (IPAdminGlobalDataReloader) Release com.sun.jndi.ldap.LdapClient@1b79ab6f
00:39:46,764 ERROR [stderr] (Thread-65) Expired com.sun.jndi.ldap.LdapClient@1b79ab6f expired
00:53:00,864 ERROR [stderr] (IPAdminGlobalDataReloader) Create com.sun.jndi.ldap.LdapClient@668fc34[eun2p3-be.stp-qa.st.com:636]
00:53:00,865 ERROR [stderr] (IPAdminGlobalDataReloader) Create and use com.sun.jndi.ldap.LdapClient@4674a7fb[eun2p3-be.stp-qa.st.com:636]
00:53:02,392 ERROR [stderr] (IPAdminGlobalDataReloader) Release com.sun.jndi.ldap.LdapClient@4674a7fb
00:53:46,787 ERROR [stderr] (Thread-65) Expired com.sun.jndi.ldap.LdapClient@668fc34 expired
00:54:46,791 ERROR [stderr] (Thread-65) Expired com.sun.jndi.ldap.LdapClient@4674a7fb expired

我的连接设置:

    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;

感谢您确定根本原因在哪里,为什么每次都创建新连接而不是重用。

最佳答案

您还没有实现任何连接池,您正在使用来自 Sun 的损坏的 DirContext 连接池。这是气馁。查看 Spring LDAP 的 ContextSource 池。效果很好。

关于java - 每次从 Ldap 连接池返回新的 Connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27057672/

相关文章:

c# - 如何克服检查用户是否是组成员时出现“"The member' s SID 无法解析”错误?

Java ActiveDirectory LDAP - 使用密码哈希对用户进行身份验证

java - 如何在 RCP 编辑器选项卡中使用装饰

java - 将 OneToMany 方映射为所有者

spring - 使用多个后缀值/域验证 ldap 用户

active-directory - Active Directory 多个经理/部门

regex - 以自定义格式保存 ldapsearch 的 Bash 脚本

tomcat - JNDI Tomcat Servelet(grails)尝试从身份验证的 LDAP 绑定(bind)中获取更多属性

java - 将字符串值从一个函数传递到另一个函数而不将该字符串作为参数传递

java - 如果我的目录名包含空格,Hibernate 无法加载 JAR 文件!