java-CommunicationException [根本异常是ConnectException:连接超时]

尝试连接Active Directory时,偶尔会遇到此异常.

javax.naming.CommunicationException: <ServerIP>:<PORT> 
  [Root exception is java.net.ConnectException: Connection timed out: connect]

这是我的代码:

    DirContext ctx = null;
    Properties env = new Properties();

    env.put(Context.SECURITY_PRINCIPAL, <Bind_USER>);
    env.put(Context.SECURITY_CREDENTIALS, <Bind_USER_PWD>);
    env.put(Context.PROVIDER_URL, "ldap://<ServerIP>:<PORT>");            
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

    ctx = new InitialDirContext(env);

在此行中获取连接超时异常ctx = new InitialDirContext(env);.
它并非每次都发生,但经常发生.

请告诉我,如何摆脱这个问题?

解决方法:

我偶尔也会发生这种情况.而且由于它只发生约1%的时间,因此我怀疑这是Juned答案中列出的任何原因,因为我的设置没有任何变化.

对我来说,它是随机发生的,并且是固定的,不需要我做任何特定的动作.这使我相信here提供的答案是正确的:

It is most likely a connection leak. Connection timeout can be caused by many things but most of them would cause it every time. Very likely the LDAP server has a maximum number of connections it will handle simultaneously, and beyond that it won’t call accept(), so new incoming connections remain in the backlog queue, which fills up, which can cause further incoming connections to time out.

@OP Can you run netstat -anp at the server when this happens, to check the hypothesis above? Can you also set a connection-idle timeout at the LDAP server? That would fix connection leaks but in a brute-force way that may break other things.

上一篇:小丑Linux学习之路-日志管理


下一篇:小丑Linux学习之路-ldap