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.
推荐答案
实际上是假阳性.已经报告了该问题,并且有公开票证对其进行修复 jira.sonarsource/浏览/SONARJAVA-2060
Indeed this is a false positive. It was already reported and there is open ticket to fix it jira.sonarsource/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
发布评论