gpt4 book ai didi

java - 结果集永远不会关闭 - Sonarqube 分析

转载 作者:行者123 更新时间:2023-12-01 17:56:45 24 4
gpt4 key购买 nike

在我们的代码中,我们已经关闭了finally block 中的结果集,但 Sonar 显示它从未关闭。请帮助。这里我们使用 Spring 数据源实用程序创建了连接,并使用相同的方法将连接释放到池中。

try {
con = DataSourceUtils.getConnection(dataSource); // connection to database using spring
stmt = con.createStatement();
rs = stmt.executeQuery("<>");
.
.
}
catch (Exception e) {
}
finally {
if (stmt != null && !stmt.isClosed()) {
stmt.close();
}
if (rs != null && !rs.isClosed()) {
rs.close();
}
if (con != null) {
DataSourceUtils.releaseConnection(con, dataSource);
}
}

最佳答案

有可能

stmt.close();

抛出 SQLException。如果发生这种情况,那么

rs.close();

永远不会被执行。正如其他人所建议的,考虑使用尝试资源

关于java - 结果集永远不会关闭 - Sonarqube 分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44160282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com