From 703febc64bf714e866ed197ee3fab73c6e004062 Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Tue, 3 Jun 2014 17:20:16 +0200 Subject: [PATCH] Fix CID-1147052 Use try-with-resources to fix the leak --- .../com/cloud/usage/UsageSanityChecker.java | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/usage/src/com/cloud/usage/UsageSanityChecker.java b/usage/src/com/cloud/usage/UsageSanityChecker.java index 5e6123bc777..50f6fe58565 100644 --- a/usage/src/com/cloud/usage/UsageSanityChecker.java +++ b/usage/src/com/cloud/usage/UsageSanityChecker.java @@ -64,30 +64,22 @@ public class UsageSanityChecker { } protected boolean checkItemCountByPstmt(CheckCase checkCase) throws SQLException { - List pstmt2Close = new ArrayList(); boolean checkOk = true; /* * Check for item usage records which are created after it is removed */ - PreparedStatement pstmt; - try { - pstmt = conn.prepareStatement(checkCase.sqlTemplate); + try (PreparedStatement pstmt = conn.prepareStatement(checkCase.sqlTemplate)) { if(checkCase.checkId) { pstmt.setInt(1, lastId); pstmt.setInt(2, maxId); } - pstmt2Close.add(pstmt); ResultSet rs = pstmt.executeQuery(); if (rs.next() && (rs.getInt(1) > 0)) { errors.append(String.format("Error: Found %s %s\n", rs.getInt(1), checkCase.itemName)); checkOk = false; } - } catch (SQLException e) { - throw e; - } finally { - TransactionLegacy.closePstmts(pstmt2Close); } return checkOk; } @@ -190,12 +182,13 @@ public class UsageSanityChecker { } protected void readMaxId() throws SQLException { - PreparedStatement pstmt = conn.prepareStatement("select max(id) from cloud_usage.cloud_usage"); - ResultSet rs = pstmt.executeQuery(); - maxId = -1; - if (rs.next() && (rs.getInt(1) > 0)) { - maxId = rs.getInt(1); - lastCheckId += " and cu.id <= ?"; + try (PreparedStatement pstmt = conn.prepareStatement("select max(id) from cloud_usage.cloud_usage")) { + ResultSet rs = pstmt.executeQuery(); + maxId = -1; + if (rs.next() && (rs.getInt(1) > 0)) { + maxId = rs.getInt(1); + lastCheckId += " and cu.id <= ?"; + } } }