Return-Path: X-Original-To: apmail-cloudstack-commits-archive@www.apache.org Delivered-To: apmail-cloudstack-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 77B6F111D3 for ; Fri, 6 Jun 2014 17:24:19 +0000 (UTC) Received: (qmail 3743 invoked by uid 500); 6 Jun 2014 17:24:19 -0000 Delivered-To: apmail-cloudstack-commits-archive@cloudstack.apache.org Received: (qmail 3713 invoked by uid 500); 6 Jun 2014 17:24:19 -0000 Mailing-List: contact commits-help@cloudstack.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cloudstack.apache.org Delivered-To: mailing list commits@cloudstack.apache.org Received: (qmail 3706 invoked by uid 99); 6 Jun 2014 17:24:19 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 06 Jun 2014 17:24:19 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 1E4D18BA7DD; Fri, 6 Jun 2014 17:24:19 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: dahn@apache.org To: commits@cloudstack.apache.org Message-Id: <935ba5d647764d5cb9da327db18bda63@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: git commit: updated refs/heads/4.4-forward to fc52e64 Date: Fri, 6 Jun 2014 17:24:19 +0000 (UTC) Repository: cloudstack Updated Branches: refs/heads/4.4-forward 91d054cb0 -> fc52e641d try-with-resource to prevent resource leaks (cherry picked from commit 39f775c38126bb25a0418f82196caaba463f8c1c) Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/fc52e641 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/fc52e641 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/fc52e641 Branch: refs/heads/4.4-forward Commit: fc52e641d8f69d8c0b552119203b0a2bc58e488f Parents: 91d054c Author: Daan Hoogland Authored: Fri Jun 6 17:28:07 2014 +0200 Committer: Daan Hoogland Committed: Fri Jun 6 19:24:08 2014 +0200 ---------------------------------------------------------------------- .../com/cloud/upgrade/dao/Upgrade430to440.java | 267 ++++++------------- 1 file changed, 87 insertions(+), 180 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/fc52e641/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java index a3ac447..51494e7 100644 --- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java +++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java @@ -67,28 +67,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) { @@ -96,24 +93,12 @@ public class Upgrade430to440 implements DbUpgrade { } } - - private void secondaryIpsAccountAndDomainIdsUpdate(Connection conn) { - PreparedStatement pstmt = null; - PreparedStatement pstmtVm = null; - PreparedStatement pstmtNw = null; - PreparedStatement pstmtUpdate = null; - - ResultSet rs1 = null; - ResultSet vmRs = null; - ResultSet networkRs = null; - String secondIpsSql = "SELECT id, vmId, network_id, account_id, domain_id, ip4_address FROM `cloud`.`nic_secondary_ips`"; - try { - pstmt = conn.prepareStatement(secondIpsSql); - rs1 = pstmt.executeQuery(); - + try (PreparedStatement pstmt = conn.prepareStatement(secondIpsSql); + ResultSet rs1 = pstmt.executeQuery(); + ) { while(rs1.next()) { long ipId = rs1.getLong(1); long vmId = rs1.getLong(2); @@ -122,130 +107,77 @@ public class Upgrade430to440 implements DbUpgrade { long domainId = rs1.getLong(5); String ipAddr = rs1.getString(6); - pstmtVm = conn.prepareStatement("SELECT account_id, domain_id FROM `cloud`.`vm_instance` where id = ?"); - pstmtVm.setLong(1,vmId); - - vmRs = pstmtVm.executeQuery(); - - if (vmRs.next()) { - long vmAccountId = vmRs.getLong(1); - long vmDomainId = vmRs.getLong(2); - - if (vmAccountId != accountId && vmAccountId != domainId) { - // update the secondary ip accountid and domainid to vm accountid domainid - // check the network type. If network is shared accountid doaminid needs to be updated in - // in both nic_secondary_ips table and user_ip_address table - - pstmtUpdate = conn.prepareStatement("UPDATE `cloud`.`nic_secondary_ips` SET account_id = ?, domain_id= ? WHERE id = ?"); - pstmtUpdate.setLong(1, vmAccountId); - pstmtUpdate.setLong(2,vmDomainId); - pstmtUpdate.setLong(3,ipId); - pstmtUpdate.executeUpdate(); - pstmtUpdate.close(); - - pstmtNw = conn.prepareStatement("SELECT guest_type FROM `cloud`.`networks` where id = ?"); - pstmtNw.setLong(1,networkId); - - networkRs = pstmtNw.executeQuery(); - if (networkRs.next()) { - String guesttype = networkRs.getString(1); - - if (guesttype.equals(Network.GuestType.Shared.toString())) { - pstmtUpdate = conn.prepareStatement("UPDATE `cloud`.`user_ip_address` SET account_id = ?, domain_id= ? WHERE public_ip_address = ?"); - pstmtUpdate.setLong(1,vmAccountId); - pstmtUpdate.setLong(2,vmDomainId); - pstmtUpdate.setString(3,ipAddr); - pstmtUpdate.executeUpdate(); - pstmtUpdate.close(); - - } - } - networkRs.close(); - networkRs = null; - pstmtNw.close(); - pstmtNw = null; + try(PreparedStatement pstmtVm = conn.prepareStatement("SELECT account_id, domain_id FROM `cloud`.`vm_instance` where id = ?");) { + pstmtVm.setLong(1,vmId); + + try(ResultSet vmRs = pstmtVm.executeQuery();) { + + if (vmRs.next()) { + long vmAccountId = vmRs.getLong(1); + long vmDomainId = vmRs.getLong(2); + + if (vmAccountId != accountId && vmAccountId != domainId) { + // update the secondary ip accountid and domainid to vm accountid domainid + // check the network type. If network is shared accountid doaminid needs to be updated in + // in both nic_secondary_ips table and user_ip_address table + + try(PreparedStatement pstmtUpdate = conn.prepareStatement("UPDATE `cloud`.`nic_secondary_ips` SET account_id = ?, domain_id= ? WHERE id = ?");) { + pstmtUpdate.setLong(1, vmAccountId); + pstmtUpdate.setLong(2,vmDomainId); + pstmtUpdate.setLong(3,ipId); + pstmtUpdate.executeUpdate(); + } catch (SQLException e) { + throw new CloudRuntimeException("Exception while updating secondary ip for nic " + ipId, e); + } + + try(PreparedStatement pstmtNw = conn.prepareStatement("SELECT guest_type FROM `cloud`.`networks` where id = ?");) { + pstmtNw.setLong(1,networkId); + + try(ResultSet networkRs = pstmtNw.executeQuery();) { + if (networkRs.next()) { + String guesttype = networkRs.getString(1); + + if (guesttype.equals(Network.GuestType.Shared.toString())) { + try(PreparedStatement pstmtUpdate = conn.prepareStatement("UPDATE `cloud`.`user_ip_address` SET account_id = ?, domain_id= ? WHERE public_ip_address = ?");) { + pstmtUpdate.setLong(1,vmAccountId); + pstmtUpdate.setLong(2,vmDomainId); + pstmtUpdate.setString(3,ipAddr); + pstmtUpdate.executeUpdate(); + } catch (SQLException e) { + throw new CloudRuntimeException("Exception while updating public ip " + ipAddr, e); + } + } + } + } catch (SQLException e) { + throw new CloudRuntimeException("Exception while retrieving guest type for network " + networkId, e); + } + + } catch (SQLException e) { + throw new CloudRuntimeException("Exception while retrieving guest type for network " + networkId, e); + } + } // if + } // if } - } //if - - pstmtVm.close(); - pstmtVm = null; - vmRs.close(); - vmRs = null; + } } // while - - } catch (SQLException e) { throw new CloudRuntimeException("Exception while Moving private zone information to dedicated resources", e); - } finally { - - if (pstmt != null) { - try { - pstmt.close(); - - } catch (SQLException e) { - } - } - - - if (rs1 != null) { - try { - rs1.close(); - } catch (SQLException e) { - } - } - - - - if (pstmtVm != null) { - try { - pstmtVm.close(); - } catch (SQLException e) { - } - } - - if (vmRs != null) { - try { - vmRs.close(); - } catch (SQLException e) { - } - } - - - - if (pstmtNw != null) { - try { - pstmtNw.close(); - - } catch (SQLException e) { - } - } - - - if (networkRs != null) { - try { - networkRs.close(); - } catch (SQLException e) { - } - } } s_logger.debug("Done updating vm nic secondary ip account and domain ids"); } private void moveCidrsToTheirOwnTable(Connection conn) { - PreparedStatement pstmtItem = null; - PreparedStatement pstmtCidr = null; - ResultSet rsItems = null; + s_logger.debug("Moving network acl item cidrs to a row per cidr"); String networkAclItemSql = "SELECT id, cidr FROM `cloud`.`network_acl_item`"; + String networkAclItemCidrSql = "INSERT INTO `cloud`.`network_acl_item_cidrs` (network_acl_item_id, cidr) VALUES (?,?)"; - s_logger.debug("Moving network acl item cidrs to a row per cidr"); - try { - pstmtItem = conn.prepareStatement(networkAclItemSql); - rsItems = pstmtItem.executeQuery(); + try (PreparedStatement pstmtItem = conn.prepareStatement(networkAclItemSql); + ResultSet rsItems = pstmtItem.executeQuery(); + PreparedStatement pstmtCidr = conn.prepareStatement(networkAclItemCidrSql); + ) { - String networkAclItemCidrSql = "INSERT INTO `cloud`.`network_acl_item_cidrs` (network_acl_item_id, cidr) VALUES (?,?)"; - pstmtCidr = conn.prepareStatement(networkAclItemCidrSql); // for each network acl item while(rsItems.next()) { @@ -264,58 +196,33 @@ public class Upgrade430to440 implements DbUpgrade { } } catch (SQLException e) { throw new CloudRuntimeException("Exception while Moving network acl item cidrs to a row per cidr", e); - } finally { - - if (pstmtItem != null) { - try { - pstmtItem.close(); - - } catch (SQLException e) { - } - } - if (pstmtCidr != null) { - try { - pstmtCidr.close(); - - } catch (SQLException e) { - } - } } s_logger.debug("Done moving network acl item cidrs to a row per cidr"); } private void updateVlanUris(Connection conn) { s_logger.debug("updating vlan URIs"); - PreparedStatement pstmt = null; - ResultSet rs = null; - try { - pstmt = conn.prepareStatement("SELECT id, vlan_id FROM `cloud`.`vlan` where vlan_id not like '%:%'"); - rs = pstmt.executeQuery(); - while (rs.next()) { - long id = rs.getLong(1); - String vlan = rs.getString(2); + try(PreparedStatement selectstatement = conn.prepareStatement("SELECT id, vlan_id FROM `cloud`.`vlan` where vlan_id not like '%:%'"); + ResultSet results = selectstatement.executeQuery()) { + + while (results.next()) { + long id = results.getLong(1); + String vlan = results.getString(2); if (vlan == null || "".equals(vlan)) { continue; } String vlanUri = BroadcastDomainType.Vlan.toUri(vlan).toString(); - pstmt = conn.prepareStatement("update `cloud`.`vlan` set vlan_id=? where id=?"); - pstmt.setString(1, vlanUri); - pstmt.setLong(2, id); - pstmt.executeUpdate(); + try(PreparedStatement updatestatement = conn.prepareStatement("update `cloud`.`vlan` set vlan_id=? where id=?");) + { + updatestatement.setString(1, vlanUri); + updatestatement.setLong(2, id); + updatestatement.executeUpdate(); + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to update vlan URI " + vlanUri + " for vlan record " + id, e); + } } } catch (SQLException e) { throw new CloudRuntimeException("Unable to update vlan URIs ", e); - } finally { - try { - if (rs != null) { - rs.close(); - } - - if (pstmt != null) { - pstmt.close(); - } - } catch (SQLException e) { - } } s_logger.debug("Done updateing vlan URIs"); }