Sonarqube对“使用try

编程入门 行业动态 更新时间:2024-10-20 11:45:13
本文介绍了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.

推荐答案

实际上是假阳性.已经报告了该问题,并且有公开票证对其进行修复 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

本文发布于:2023-10-18 11:01:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504048.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Sonarqube

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!