diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade420to421.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade420to421.java index 192855219d4..66c1a2ea3e3 100644 --- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade420to421.java +++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade420to421.java @@ -280,14 +280,13 @@ public class Upgrade420to421 implements DbUpgrade { private static void upgradeResourceCountforDomain(Connection conn, Long domainId, String type, Long resourceCount) throws SQLException { //update or insert into resource_count table. - PreparedStatement pstmt = null; - pstmt = - conn.prepareStatement("INSERT INTO `cloud`.`resource_count` (domain_id, type, count) VALUES (?,?,?) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), count=?"); - pstmt.setLong(1, domainId); - pstmt.setString(2, type); - pstmt.setLong(3, resourceCount); - pstmt.setLong(4, resourceCount); - pstmt.executeUpdate(); - pstmt.close(); + String sqlInsertResourceCount = "INSERT INTO `cloud`.`resource_count` (domain_id, type, count) VALUES (?,?,?) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), count=?"; + try (PreparedStatement pstmt = conn.prepareStatement(sqlInsertResourceCount);) { + pstmt.setLong(1, domainId); + pstmt.setString(2, type); + pstmt.setLong(3, resourceCount); + pstmt.setLong(4, resourceCount); + pstmt.executeUpdate(); + } } }