如何避免声纳错误“;使用“与资源一起尝试”或关闭此。。。在“a”中;最后";第条;是否使用自定义关闭方法(java 6)?

如何避免声纳错误“;使用“与资源一起尝试”或关闭此。。。在“a”中;最后";第条;是否使用自定义关闭方法(java 6)?,java,sonarqube,Java,Sonarqube,代码如下: Connection connection = null; try { connection = createConnection(); String sql = String.format(STRING_FOR_PROCEDURE, name); connection.createStatement().execute(sql); connection.commit(); } catch (Except

代码如下:

Connection connection = null;
    try {
        connection = createConnection();
        String sql = String.format(STRING_FOR_PROCEDURE, name);
        connection.createStatement().execute(sql);
        connection.commit();
    } catch (Exception e) {
        throw new DatabaseServiceException(e.getMessage(), e);
    } finally {
        closeConnection(connection);
    }
我想声纳想让我用这样的东西来关闭“最后”区的连接:

connection.close();
但我使用的自定义方法是:

    protected void closeConnection(Connection connection) {
    try {
        if (connection != null) {
            connection.close();
        }
    } catch (SQLException ex) {
        LOGGER.log(Level.SEVERE, null, ex);
    }
}
此方法需要在当前类中使用。但我收到了声纳拦截器“使用try with resources或在“finally”子句中关闭此语句”。
有没有办法解决这个问题?

声纳有助于设计正确的代码,但有时如果你很清楚,我们可以忽略警告。。但是是的,你能做到吗

Connection connection = null;
    try {
        connection = createConnection();
        String sql = String.format(STRING_FOR_PROCEDURE, name);
        connection.createStatement().execute(sql);
        connection.commit();
    } catch (Exception e) {
        throw new DatabaseServiceException(e.getMessage(), e);
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException ex) {
            LOGGER.log(Level.SEVERE, null, ex);
        }
    }

声纳有助于设计正确的代码,但有时如果你很清楚,我们可以忽略警告。。但是是的,你能做到吗

Connection connection = null;
    try {
        connection = createConnection();
        String sql = String.format(STRING_FOR_PROCEDURE, name);
        connection.createStatement().execute(sql);
        connection.commit();
    } catch (Exception e) {
        throw new DatabaseServiceException(e.getMessage(), e);
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException ex) {
            LOGGER.log(Level.SEVERE, null, ex);
        }
    }

请注意,您的错误是“在“finally”子句中使用try with resources或关闭此语句”

问题不在于如何关闭连接。问题是你没有结束你的陈述

Connection connection = null;
Statement statement = null;
try {
    connection = createConnection();
    String sql = String.format(STRING_FOR_PROCEDURE, name);
    statement = connection.createStatement();
    statement.execute(sql);
    connection.commit();
} catch (Exception e) {
    throw new DatabaseServiceException(e.getMessage(), e);
} finally {
    if (statement != null) {
        try {
            statement.close();
        } catch (SQLException e) {
            Logger.log(Level.SEVERE, "Could not close Statement.", e);
        }
    }
    closeConnection(connection);
}

请记住,在关闭连接之前需要关闭语句。

请注意,您的错误是“使用try with resources或在“finally”子句中关闭此语句。”

问题不在于如何关闭连接。问题是你没有结束你的陈述

Connection connection = null;
Statement statement = null;
try {
    connection = createConnection();
    String sql = String.format(STRING_FOR_PROCEDURE, name);
    statement = connection.createStatement();
    statement.execute(sql);
    connection.commit();
} catch (Exception e) {
    throw new DatabaseServiceException(e.getMessage(), e);
} finally {
    if (statement != null) {
        try {
            statement.close();
        } catch (SQLException e) {
            Logger.log(Level.SEVERE, "Could not close Statement.", e);
        }
    }
    closeConnection(connection);
}

请记住,在连接关闭之前需要关闭语句。

这是否回答了您的问题?你能发布你的createConnection()方法吗?最简单的解决方案是升级到更新的Java版本,并使用try-with-resources。所以我不得不问——为什么你仍然使用Java 6?这个庞大的项目有着大量的遗产,目前在1.6上,该项目是2012年第一次构建的。这是否回答了你的问题?你能发布你的createConnection()方法吗?最简单的解决方案是升级到更新的Java版本,并使用try-with-resources。所以我不得不问——为什么你仍然使用Java 6?这个庞大的项目有着大量的遗产,目前在1.6上,该项目是在2012年首次构建的。