gpt4 book ai didi

java - JNDI LDAP 池连接

转载 作者:行者123 更新时间:2023-12-01 19:21:44 26 4
gpt4 key购买 nike

我的 JNDI 连接池似乎没有按预期工作。当我运行测试时,我看到每次都会创建新连接。连接成功(就像我可以很好地查询 Active Directory 一样),但它们没有像我期望的那样进行池化。以下是有关我的设置的一些重要说明:

  1. 连接通过 SSL(即 ldaps)进行

  2. 使用自定义套接字工厂

  3. AD 身份验证通过“简单”身份验证完成

  4. 使用默认池配置值(目前)

相关代码如下:

TestLdapsPooling.java

public class TestLdapsPooling {

public static void main(String[] args) throws Exception {
// needed system properties
System.setProperty("com.sun.jndi.ldap.connect.pool.protocol", "ssl");
System.setProperty("com.sun.jndi.ldap.connect.pool.debug", "fine");

Hashtable<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, host);

// use ssl
env.put(Context.SECURITY_PROTOCOL, "ssl");

// authentication info
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);

// custom socket factory
env.put("java.naming.ldap.factory.socket", "ldap.TrustedSocketFactory");

// Enable connection pooling
env.put("com.sun.jndi.ldap.connect.pool", "true");

// Create the initial context
InitialDirContext context = new InitialDirContext(env);
System.out.println("First context created");

// Create a second context
InitialDirContext context2 = new InitialDirContext(env);
System.out.println("Second context created");

// close first context
context.close();
System.out.println("First context closed");

// close second context
context2.close();
System.out.println("Second context closed");

// Create a third context - I would expect this to use a connection from the pool
InitialDirContext context3 = new InitialDirContext(env);
System.out.println("Third context created");

// close third context
context3.close();
System.out.println("Third context closed");
}
}

TrustedSocketFactory.java

public class TrustedSocketFactory extends SSLSocketFactory implements Comparator<SocketFactory> {

// all the methods required from SSLSocketFactory

@Override
public int compare(SocketFactory arg0, SocketFactory arg1) {
// not really sure what this value should be
// is this causing the pooling issue?
return 0;
}

}

输出:

Create com.sun.jndi.ldap.LdapClient@3f0ee7cb[testdomain.com:636]
Use com.sun.jndi.ldap.LdapClient@3f0ee7cb
First context created
Create com.sun.jndi.ldap.LdapClient@75bd9247[testdomain.com:636]
Use com.sun.jndi.ldap.LdapClient@75bd9247
Second context created
Release com.sun.jndi.ldap.LdapClient@3f0ee7cb
First context closed
Release com.sun.jndi.ldap.LdapClient@75bd9247
Second context closed
Create com.sun.jndi.ldap.LdapClient@7d417077[testdomain.com:636]
Use com.sun.jndi.ldap.LdapClient@7d417077
Third context created
Release com.sun.jndi.ldap.LdapClient@7d417077
Third context closed

我希望第三个上下文重用前两个连接之一,但输出看起来像是创建了自己的连接。为了重用连接而不是创建新连接,我需要更改什么?

最佳答案

基于 https://docs.oracle.com/javase/jndi/tutorial/ldap/connect/config.html 的“汇集内容”部分这可能是自定义套接字工厂禁用池:

There are a couple of environment properties that automatically disqualify a Context instance from using a pooled connection. A Context instance cannot use a pooled connection if it has its "java.naming.ldap.factory.socket" property set to a custom socket factory class (...)

关于java - JNDI LDAP 池连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59349715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com