当前位置: 首页 > 知识库问答 >
问题:

eclipse“潜在资源泄漏”警告不正确

柳鸿信
2023-03-14

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);
}

为什么第二种方法会引起警告?

谢谢!

共有1个答案

公良飞尘
2023-03-14

Eclipse关闭警告代码只查看当前方法。它不分析CloseAll方法来查看它的功能(在这种情况下,它还必须查看CloseQuely)。

 类似资料: