Elasticsearch resource leak
I am using transport client on version 1.3.2 so
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elastictest").build();
Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("1.1.1.1", 9300));
Close the method with
client.close();
Everything looks fine, but I get a nasty yellow warning under the words "new TransportClient (settings)" in Eclipse saying:
Resource leak: '<unassigned Closeable value>' is never closed
It looks like this:
So my question is, although I close the client at the end of the method, is it still leaking with the "new transport client"? Or should I just ignore this warning in eclipse?
In response to @Lucas it looks like this:
+3
source to share
2 answers
Why don't you try using try-with-resources
:
try(Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elastictest").build();
Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("1.1.1.1", 9300));) {
//your code
} catch (//your exceptions if any) {
//handle exceptions
}
It will automatically close the resources when you're done with them and should shut down Eclipse down
+4
source to share
I found that separating the creation of TransportClient and addTransportAddress stopped the warning:
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elastictest").build();
TransportClient client = new TransportClient(settings);
client.addTransportAddress(new InetSocketTransportAddress("1.1.1.1", 9300));
Good luck!
+2
source to share