From 3dc0a64b3e67ec16f25fc19b6ab0dd6f158daa6f Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Fri, 6 Jun 2014 17:28:07 +0200 Subject: [PATCH] try-with-resource to prevent resource leaks (cherry picked from commit 39f775c38126bb25a0418f82196caaba463f8c1c) (cherry picked from commit fc52e641d8f69d8c0b552119203b0a2bc58e488f) Conflicts: engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java --- .../cloud/upgrade/dao/Upgrade430to440.java | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java index 09ac6fe554f..7226ca79ed4 100644 --- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java +++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java @@ -68,28 +68,25 @@ public class Upgrade430to440 implements DbUpgrade { private void addExtractTemplateAndVolumeColumns(Connection conn) { - PreparedStatement pstmt = null; - ResultSet rs = null; - try { + try (PreparedStatement selectTemplateInfostmt = conn.prepareStatement("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'cloud' AND TABLE_NAME = 'template_store_ref' AND COLUMN_NAME = 'download_url_created'"); + ResultSet templateInfoResults = selectTemplateInfostmt.executeQuery(); + PreparedStatement addDownloadUrlCreatedToTemplateStorerefstatement = conn.prepareStatement("ALTER TABLE `cloud`.`template_store_ref` ADD COLUMN `download_url_created` datetime"); + PreparedStatement addDownloadUrlToTemplateStorerefstatement = conn.prepareStatement("ALTER TABLE `cloud`.`template_store_ref` ADD COLUMN `download_url` varchar(255)"); + PreparedStatement selectVolumeInfostmt = conn.prepareStatement("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'cloud' AND TABLE_NAME = 'volume_store_ref' AND COLUMN_NAME = 'download_url_created'"); + ResultSet volumeInfoResults = selectVolumeInfostmt.executeQuery(); + PreparedStatement addDownloadUrlCreatedToVolumeStorerefstatement = conn.prepareStatement("ALTER TABLE `cloud`.`volume_store_ref` ADD COLUMN `download_url_created` datetime"); + ) { // Add download_url_created, download_url to template_store_ref - pstmt = conn.prepareStatement("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'cloud' AND TABLE_NAME = 'template_store_ref' AND COLUMN_NAME = 'download_url_created'"); - rs = pstmt.executeQuery(); - if (!rs.next()) { - pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`template_store_ref` ADD COLUMN `download_url_created` datetime"); - pstmt.executeUpdate(); - - pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`template_store_ref` ADD COLUMN `download_url` varchar(255)"); - pstmt.executeUpdate(); + if (!templateInfoResults.next()) { + addDownloadUrlCreatedToTemplateStorerefstatement.executeUpdate(); + addDownloadUrlToTemplateStorerefstatement.executeUpdate(); } // Add download_url_created to volume_store_ref - note download_url already exists - pstmt = conn.prepareStatement("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'cloud' AND TABLE_NAME = 'volume_store_ref' AND COLUMN_NAME = 'download_url_created'"); - rs = pstmt.executeQuery(); - if (!rs.next()) { - pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`volume_store_ref` ADD COLUMN `download_url_created` datetime"); - pstmt.executeUpdate(); + if (!volumeInfoResults.next()) { + addDownloadUrlCreatedToVolumeStorerefstatement.executeUpdate(); } } catch (SQLException e) { @@ -97,7 +94,6 @@ public class Upgrade430to440 implements DbUpgrade { } } - private void secondaryIpsAccountAndDomainIdsUpdate(Connection conn) { String secondIpsSql = "SELECT id, vmId, network_id, account_id, domain_id, ip4_address FROM `cloud`.`nic_secondary_ips`";