简体   繁体   English

即使我们已经处理了语句,SonarLint 仍然显示阻止程序错误,连接在 finally 块中关闭

[英]SonarLint is still showing blocker errors even though we have handled the statement, Connection closes in finally block

SonarLint is showing below errors: SonarLint 显示以下错误:

  1. 'Use try-with-resources or close this "Statement" in a "finally" clause.' '使用 try-with-resources 或在“finally”子句中关闭此“语句”。'
  2. 'Use try-with-resources or close this "Connection" in a "finally" clause.' '使用 try-with-resources 或在“finally”子句中关闭此“连接”。'
    blocker errors even though we have closed the Statement stmt, Connection con in finally block.即使我们在 finally 块中关闭了 Statement stmt、Connection con,也会出现阻塞错误。

Please find the sample code.请找到示例代码。

public String getProductNumber() throws BusinessDelegateException {

        String productNo = null;
        Connection con = null;
        Statement stmt = null;
        ResultSet   rs  = null;
        String query  = //some query
        try {
            DataSource ds = getDataSource();
            con = ds.getConnection();
            stmt = con.createStatement();
            rs   = stmt.executeQuery(query);
            productNo =.......
            ....................
        }catch (Exception e) {
            String errorMsg = "Error occured in getProductNumber()";
            throw new BusinessDelegateException(errorMsg, e);
        }finally{
            try {
                if(rs != null)
                    rs.close();
                if (stmt != null)
                    stmt.close();
                if (con != null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return productNo;
    }

We were able to fix the issue by modifying the finally block in below manner.我们能够通过以下方式修改 finally 块来解决问题。 But still it seems like repetition of catch blocks.但它似乎仍然是 catch 块的重复。 Any other way we can fix this?我们还有其他方法可以解决这个问题吗?

finally{
        try {
            if(rs != null)
                rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (con != null)
                con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

Without try-with-resources you can only improve the code by using methods for re-usability, call main method: 没有try-with-resources,您只能通过使用方法来提高代码的可重用性,调用main方法:

closeResources(rs, stmt, con);

Which will call each for each resource a different method for example Statement: 它将为每种资源调用不同的方法,例如Statement:

 public void closeResource(Statement stmt) {
    if (stmt != null) {
        stmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }

BTW, you better use logger instead of e.printStackTrace() 顺便说一句,您最好使用logger而不是e.printStackTrace()

For complete solution, you can check extensive example which add resources in array and close them in a loop: 要获得完整的解决方案,您可以查看广泛的示例 ,这些示例将资源添加到数组中并在循环中将其关闭:

 for (Closeable resource : resources) { try { resource.close(); 

Use Try-with-resources.使用资源尝试。 This is the best option.这是最好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java-SonarLint检测阻止程序-在“ finally”子句中关闭此“ PreparedStatement” - Java - SonarLint Detect the Blocker - Close this “PreparedStatement” in a “finally” clause Java中finally块后语句的编译错误 - Compilation errors for statement after finally block in Java 为什么我仍然收到 InputMissmatchException 即使我有一个 catch 语句 - Why am I still getting a InputMissmatchException even though i have a catch statement 如何使用finally块在事务上下文中处理异常? - How is exception handled in transactional context with finally block? 最终关闭连接并声明 - Close connection and statement finally 即使处理了 ArrayList IndexOutOfBoundsException - ArrayList IndexOutOfBoundsException even though handled 即使请求正文中存在错误,BindingResult也不会显示错误 - BindingResult is not showing errors even though there exist errors in the request body 即使在调用方法中添加了try catch finally块,也仍然在main中获得编译错误 - Getting compilation error in main even though try catch finally block is added in the calling method finally 块后无法访问的语句 - unreachable statement after finally block 即使应用仍处于活动状态,当用户离开网站或关闭浏览器时,脚本事件也不会触发 - Scripting events no longer fire when a user leaves site or closes browser even though app is still active
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM