LDAP connection issue

2 篇文章 0 订阅
[b]Problem1:[/b]

Sometimes we can not get LDAP connection from LDAP server.

[b]Cause1:[/b]
We can not close LDAP connection when program is end.
[b]
Solution1-1(don't do that):[/b]

Currently our code to close LDAP connection are as below,

LdapContext cladptx = null;
NamingEnumeration<?> results = null;
try {
cladptx = createConnection();
results = cladptx.search(ldapConnector.getEntry(), filter, constraints); } catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
try {
if (cladptx != null) {
cladptx.close();
}
} catch (Exception e2) {
logger.error(e2.getMessage(), e2);
}
}

But actually LDAP connection was not closed completely.

[b]Solution1-2(Good solution):[/b]

We need to close various parameters of context and NamingEnumeration.Code as below,

LdapContext cladptx = null;
NamingEnumeration<?> results = null;
try {
cladptx = createConnection();
results = cladptx.search(ldapConnector.getEntry(), filter, constraints);

} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
try {
if(results!=null) {
results.close();
}
if (cladptx != null) {
cladptx.close();
}
} catch (Exception e2) {
logger.error(e2.getMessage(), e2);
}
}

[b]Problem2:[/b]

After I closed various parameters and ran a test which loops 4000 times on a LDAP client application, I observed the following errors in Windows Operating System when the load increased beyond a certain threshold:

javax.naming.CommunicationException: localhost:10389
at com.sun.jndi.ldap.Connection.<init>(Connection.java:210)
at com.sun.jndi.ldap.LdapClient.<init>(LdapClient.java:118)
</init></init>
[b]
Cause2:[/b]
The causes for the errors are that the dynamic ports in the OS are running out so that client application can not make any more connections to LDAP server.
Usually LDAP server is running on a specific port (say 10389) and when a connection is created from client, that is assigned a port in the range of dynamic ports which is defined as a property of Windows OS. This issue is not visible on Linux.

So the above errors can occur when no available ports are left.

[b]Solution2:[/b]

We'd better use connection pool to solve this problem.See below code,

InitialDirContext context = null;
try {
System.setProperty("com.sun.jndi.ldap.connect.pool.maxsize", "50");
Hashtable<String,String> env = new Hashtable<String,String>();
//env.put("com.sun.jndi.ldap.connect.pool", "true");
context = new InitialDirContext(env);
} catch (NamingException e) {
logger.error(e.getMessage(),e);
}

[b]Conclusion[/b]
1. Enable LDAP connection pooling when creating the connection to the LDAP..
2. Close all sub Contexts and NamingEnumerations derived from a particular LDAP connection Context, close them explicitly at the end of their usage.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值