diff options
author | Gurmeen Bindra <gurmeen.bindra@isode.com> | 2012-08-16 09:53:11 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2012-08-16 10:07:01 (GMT) |
commit | a22d6c1c23f06bb4dbc8b5c72177ebf27b239482 (patch) | |
tree | 30818f83b43b7b66f877c65b88939d3a60aa3fd9 | |
parent | 8568b7b74fe962bf6d3252e9967aa3123968615c (diff) | |
download | stroke-a22d6c1c23f06bb4dbc8b5c72177ebf27b239482.zip stroke-a22d6c1c23f06bb4dbc8b5c72177ebf27b239482.tar.bz2 |
Close DirectoryContext after receiving results
If I leave an Application using Stroke running for a few hours(making periodic
connection attempts), the JVM would throw an Exception saying "Too Many Open Files".
On doing an "lsof -p <pid_of_jvm>", I noticed that there were number of open
sockets in CLOSE_WAIT state and these went up after every attempt to do a
connect on CoreClient object.
Closing of DirContext object fixes this bug and the number of open sockets
does not increase.
Test-information:
Ran MLC and kept on monitoring the result of "lsof -p <pid>". It would not
increase after this patch.
-rw-r--r-- | src/com/isode/stroke/network/PlatformDomainNameServiceQuery.java | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/com/isode/stroke/network/PlatformDomainNameServiceQuery.java b/src/com/isode/stroke/network/PlatformDomainNameServiceQuery.java index 66a38cc..0fc7976 100644 --- a/src/com/isode/stroke/network/PlatformDomainNameServiceQuery.java +++ b/src/com/isode/stroke/network/PlatformDomainNameServiceQuery.java @@ -32,8 +32,9 @@ public class PlatformDomainNameServiceQuery extends DomainNameServiceQuery imple Hashtable env = new Hashtable(); env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); env.put("java.naming.provider.url", "dns:"); + DirContext ctx = null; try { - DirContext ctx = new InitialDirContext(env); + ctx = new InitialDirContext(env); Attributes attrs = ctx.getAttributes(service, new String[]{"SRV"}); Attribute attribute = attrs.get("SRV"); for (int i = 0; attribute != null && i < attribute.size(); i++) { @@ -58,7 +59,15 @@ public class PlatformDomainNameServiceQuery extends DomainNameServiceQuery imple onResult.emit(results); } }); - + //close the context as otherwise this will lead to open sockets in + //CLOSE_WAIT condition + if(ctx != null) { + try { + ctx.close(); + } catch (NamingException e) { + //at least we try to close the context + } + } } } |