Skip to content

Commit 703febc

Browse files
committed
Fix CID-1147052 Use try-with-resources to fix the leak
1 parent d21a15b commit 703febc

1 file changed

Lines changed: 8 additions & 15 deletions

File tree

usage/src/com/cloud/usage/UsageSanityChecker.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,22 @@ protected boolean checkItemCountByPstmt() throws SQLException {
6464
}
6565

6666
protected boolean checkItemCountByPstmt(CheckCase checkCase) throws SQLException {
67-
List<PreparedStatement> pstmt2Close = new ArrayList<PreparedStatement>();
6867
boolean checkOk = true;
6968

7069
/*
7170
* Check for item usage records which are created after it is removed
7271
*/
73-
PreparedStatement pstmt;
74-
try {
75-
pstmt = conn.prepareStatement(checkCase.sqlTemplate);
72+
try (PreparedStatement pstmt = conn.prepareStatement(checkCase.sqlTemplate)) {
7673
if(checkCase.checkId) {
7774
pstmt.setInt(1, lastId);
7875
pstmt.setInt(2, maxId);
7976
}
8077

81-
pstmt2Close.add(pstmt);
8278
ResultSet rs = pstmt.executeQuery();
8379
if (rs.next() && (rs.getInt(1) > 0)) {
8480
errors.append(String.format("Error: Found %s %s\n", rs.getInt(1), checkCase.itemName));
8581
checkOk = false;
8682
}
87-
} catch (SQLException e) {
88-
throw e;
89-
} finally {
90-
TransactionLegacy.closePstmts(pstmt2Close);
9183
}
9284
return checkOk;
9385
}
@@ -190,12 +182,13 @@ protected void readLastCheckId(){
190182
}
191183

192184
protected void readMaxId() throws SQLException {
193-
PreparedStatement pstmt = conn.prepareStatement("select max(id) from cloud_usage.cloud_usage");
194-
ResultSet rs = pstmt.executeQuery();
195-
maxId = -1;
196-
if (rs.next() && (rs.getInt(1) > 0)) {
197-
maxId = rs.getInt(1);
198-
lastCheckId += " and cu.id <= ?";
185+
try (PreparedStatement pstmt = conn.prepareStatement("select max(id) from cloud_usage.cloud_usage")) {
186+
ResultSet rs = pstmt.executeQuery();
187+
maxId = -1;
188+
if (rs.next() && (rs.getInt(1) > 0)) {
189+
maxId = rs.getInt(1);
190+
lastCheckId += " and cu.id <= ?";
191+
}
199192
}
200193
}
201194

0 commit comments

Comments
 (0)