eclipseの「潜在的なリソースリーク」警告が正しくありません。eclipse「潜在的なリソースリーク」警告が正しくない
警告なし:
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build();
String apiUrl = ...;
HttpPost httpPost = new HttpPost(apiUrl);
CloseableHttpResponse response = null;
HttpEntity entity;
try {
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
logger.warn("invoke failed, response status={},key={}",statusCode,key);
httpPost.close();
if (response != null) {
response.close();
}
return null;
}
}
警告:潜在的なリソースリーク:
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build();
String apiUrl = ...;
HttpPost httpPost = new HttpPost(apiUrl);
CloseableHttpResponse response = null;
HttpEntity entity;
try {
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
logger.warn("invoke failed, response status={},key={}",statusCode,key);
closeBoth(httpClient, response)
return null;
}
}
private void closeBoth(CloseableHttpClient client, CloseableHttpResponse resp) {
org.apache.poi.util.IOUtils.closeQuitely(client);
org.apache.poi.util.IOUtils.closeQuitely(resp);
}
なぜ第二のアプローチ原因警告 "応答は、この場所で閉鎖することはできませんか"?
ありがとうございます!
'closeBoth'が常に' httpClient'と 'response'を閉じることをeclipseがどのように知っていますか? –
try-with-resourcesを使用してみませんか? –