3

SonarLint is showing below errors:

  1. 'Use try-with-resources or close this "Statement" in a "finally" clause.'
  2. 'Use try-with-resources or close this "Connection" in a "finally" clause.'
    blocker errors even though we have closed the Statement stmt, Connection con in finally block.

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. But still it seems like repetition of catch blocks. 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();
        }
    }
Raj
  • 1,945
  • 20
  • 40
vishnu
  • 165
  • 1
  • 16
  • 3
    There is a chance for e.g. your `rs.close` to throw an exception leading to `stmt` and `con` to never be closed. Use try-with-resources as suggested as they all implement `AutoClosable` anyways. – Ben May 29 '18 at 11:22
  • Also your close calls in the try are redundant. The finally executes anyways. And why is it in a try/catch in the finally but not in your first try? If you replicate code then at least... replicate it fully. That code really is just a mess. – Ben May 29 '18 at 11:24
  • @Ben..close calls in try block was a mistake while copying code here.edited the code. thanks for noticing it. Issue is replicable with this code – vishnu May 29 '18 at 11:28
  • @Ben.. There are so many methods like this in my code and the try blocks are more complex in my codes. I just taken a small method call for example. making these methods to 'try with resource' is difficult in my code. yeh..We can do it. But can we handle the issue through finally block itself? Any ways for doing this? – vishnu May 29 '18 at 11:36

2 Answers2

0

Without try-with-resources you can only improve the code by using methods for re-usability, call main method:

closeResources(rs, stmt, con);

Which will call each for each resource a different method for example 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()

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();
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
0

Use Try-with-resources. This is the best option.

thusila
  • 1
  • 3