Sonarqube对“使用try-with-resources或关闭此" ResultSet"结果为假阳性.在“最后"条款" [英] Sonarqube false positive for "Use try-with-resources or close this "ResultSet" in a "finally" clause"

查看:1864
本文介绍了Sonarqube对“使用try-with-resources或关闭此" ResultSet"结果为假阳性.在“最后"条款"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sonarqube一直用这个问题标记代码,我认为这是一个错误的肯定. 代码如下:

Sonarqube keeps marking code with this issue which is, in my opinion, a false positive. Code looks like this:

try(PreparedStatement st=con.prepareStatement(myQuery)){
    st.setInt(1, myValue);
    ...
    ResultSet rs = st.executeQuery();
    ...
}

如果我没记错的话,PreparedStatement实现Closeable,并且在关闭自身时也会关闭基础ResultSet.

If I'm not mistaken, the PreparedStatement implements Closeable and, when closing itself, it also closes the underlying ResultSet.

此行为将阻止ResultSet保持打开状态,但Sonarqube分析将其标记为严重错误.

This behaviour would prevent the ResultSet from being kept open, yet Sonarqube analysis marks it as a critical error.

我误会了吗? 在这种情况下是否有任何使Sonarqube忽略此规则的方法?

Am I mistaken? Any way of making Sonarqube ignore this rule under this circumstances?

在Sonarqube 6.7.3和JDK 8下测试.

Tested under Sonarqube 6.7.3 and JDK 8.

ResultSet javadoc:

当ResultSet对象关闭,重新执行或用于从多个结果序列中检索下一个结果时,将自动关闭ResultSet对象.

A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results.

推荐答案

实际上是假阳性.已经报告了该问题,并且有公开票证对其进行修复 https://jira.sonarsource.com/浏览/SONARJAVA-2060

Indeed this is a false positive. It was already reported and there is open ticket to fix it https://jira.sonarsource.com/browse/SONARJAVA-2060

您可以在SonarQube UI中将该问题标记为误报,或在引发该问题的行上添加// NOSONAR注释以忽略它.

You can mark the issue as false positive in SonarQube UI, or add // NOSONAR comment on the line where the issue is raised to ignore it.

这篇关于Sonarqube对“使用try-with-resources或关闭此" ResultSet"结果为假阳性.在“最后"条款"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆