Java CommunicationException [Root 异常是 ConnectException: Connection timed out]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16412582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CommunicationException [Root exception is ConnectException: Connection timed out]
提问by Karthik Bose
I'm getting this exception occasionally, while trying to connect Active Directory.
我在尝试连接 Active Directory 时偶尔会遇到此异常。
javax.naming.CommunicationException: <ServerIP>:<PORT>
[Root exception is java.net.ConnectException: Connection timed out: connect]
Here is my code:
这是我的代码:
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);
Getting the connection timeout exception in this line ctx = new InitialDirContext(env);
.
It doesn't happen every-time, but happens quite often.
在此行中获取连接超时异常ctx = new InitialDirContext(env);
。它不会每次都发生,但经常发生。
Please advise me, how to get rid of this issue?
请告诉我,如何摆脱这个问题?
回答by Tamara Aviv
This happens to me occasionally as well. And because it only happens ~1% of the time, I doubt it's any of the reasons listed in Juned's answer since nothing changes in my setting.
这种情况也偶尔发生在我身上。而且因为它只发生约 1% 的时间,我怀疑这是 Juned 的答案中列出的任何原因,因为我的设置没有任何变化。
For me it happens quite randomly and is fixed without any specific action on my part. This makes me believe that the answer provided hereis correct:
对我来说,它发生的非常随机,并且在我没有任何具体操作的情况下被修复。这让我相信这里提供的答案是正确的:
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.
这很可能是连接泄漏。连接超时可能由很多原因引起,但大多数情况每次都会引起。很可能 LDAP 服务器具有将同时处理的最大连接数,除此之外它不会调用 accept(),因此新的传入连接保留在积压队列中,该队列已满,这可能会导致进一步传入连接的时间出去。
@OP 发生这种情况时,您能否在服务器上运行 netstat -anp 以检查上述假设?您还可以在 LDAP 服务器上设置连接空闲超时吗?这将修复连接泄漏,但以一种可能会破坏其他事物的蛮力方式。
回答by KERR
Had the same intermittent issue, although the config pointed to a domain name (not an IP).
有同样的间歇性问题,尽管配置指向一个域名(不是 IP)。
By using NSLOOKUP, it was discovered that a non-existent DC was listed which was causing intermittent connection issues.
通过使用 NSLOOKUP,发现列出了不存在的 DC,这导致了间歇性连接问题。
回答by Dave McLure
I began to notice this as well when I swapped out the use of a Timer with that of ScheduledExecutorService for launching my Ldap Server. The problem turned out to be a race condition. I changed the launch time of my Ldap Server from 0 delay to a 5 second delay and this seems to have resolved the java.net.ConnectException to my Ldap Server.
当我将 Timer 的使用换成 ScheduledExecutorService 来启动我的 Ldap 服务器时,我也开始注意到这一点。结果证明问题是竞争条件。我将 Ldap 服务器的启动时间从 0 延迟更改为 5 秒延迟,这似乎解决了我的 Ldap 服务器的 java.net.ConnectException 问题。
Race condition existed here :
此处存在竞争条件:
final ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(); ses.scheduleWithFixedDelay(ldapServer, 0, 5, TimeUnit.SECONDS);
最终 ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(); ses.scheduleWithFixedDelay(ldapServer, 0, 5, TimeUnit.SECONDS);
Race condition resolved here :
竞争条件在这里解决:
final ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(); ses.scheduleWithFixedDelay(ldapServer, 5, 5, TimeUnit.SECONDS);
最终 ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(); ses.scheduleWithFixedDelay(ldapServer, 5, 5, TimeUnit.SECONDS);
回答by Luzuko Edwana
I have been getting the same error after moving over to LDAPS I am now using Port 636 and I discovered that one of the Domain Controllers on the Domain I connect to is blocked on port 636.
转移到 LDAPS 后,我遇到了同样的错误,我现在使用端口 636,我发现我连接到的域上的域控制器之一在端口 636 上被阻止。
[Root exception is java.net.ConnectException: Connection timed out: connect] I
[Root 异常是 java.net.ConnectException: Connection timed out: connect] I