Web应用程序中的ldap连接池

时间:2014-08-05 03:29:26

标签: java ldap jndi connection-pooling

我需要在Web应用程序中使用ldap连接池。要验证管理员,我使用下面的代码:

    Properties props = new Properties();
    System.setProperty("com.sun.jndi.ldap.connect.pool", "true");
    System.setProperty("com.sun.jndi.ldap.connect.pool.maxsize", "1");
    System.setProperty("com.sun.jndi.ldap.connect.pool.debug", "fine");

    props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    props.put(Context.PROVIDER_URL, "ldap://localhost:10389/o=myldap");
    props.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");//adminuser 
    props.put(Context.SECURITY_CREDENTIALS, "xxxxx");
    InitialDirContext context = new InitialDirContext(props);

要查找和验证其他用户,我需要更改SECURITY_PRINCIPAL和SECURITY_CREDENTIALS,然后再次创建新的上下文。当我这样做是一个POJO,它使用连接池,但当我在多用户Web应用程序中使用它(我尝试使用两个线程)时,它不使用连接池。

可以使用哪种解决方法?

2 个答案:

答案 0 :(得分:0)

您需要使用java调用上的-D参数将参数设置为JVM系统参数。

答案 1 :(得分:-1)

System.setProperty("com.sun.jndi.ldap.connect.pool", "true");
System.setProperty("com.sun.jndi.ldap.connect.pool.maxsize", "1");
System.setProperty("com.sun.jndi.ldap.connect.pool.debug", "fine");

错误的属性对象。您应该将它们设置为'props'对象,而不是系统属性对象。

编辑并且不要调试为true。设置它具有关闭池的奇怪效果。

相关问题