cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject [2/8] git commit: updated refs/heads/master to 8151f7f
Date Mon, 03 Aug 2015 19:57:28 GMT
coverity 1225199: vmware dc upgrade

Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/367e2ab7
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/367e2ab7
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/367e2ab7

Branch: refs/heads/master
Commit: 367e2ab71e7f7e78898ec7135ee140c6c8107cc8
Parents: 2f0813a
Author: Daan Hoogland <daan.hoogland@gmail.com>
Authored: Mon Jul 20 14:44:25 2015 +0200
Committer: Daan Hoogland <daan.hoogland@gmail.com>
Committed: Mon Jul 20 14:44:25 2015 +0200

----------------------------------------------------------------------
 .../com/cloud/upgrade/dao/Upgrade410to420.java  | 133 ++++++++-----------
 1 file changed, 57 insertions(+), 76 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/367e2ab7/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java
index 7e7f7b7..18f84f9 100644
--- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java
+++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java
@@ -846,94 +846,75 @@ public class Upgrade410to420 implements DbUpgrade {
     }
 
     private void updateNonLegacyZones(Connection conn, List<Long> zones) {
-        PreparedStatement clustersQuery = null;
-        PreparedStatement clusterDetailsQuery = null;
-        PreparedStatement pstmt = null;
-        ResultSet clusters = null;
-        ResultSet clusterDetails = null;
-        ResultSet dcInfo = null;
         try {
             for (Long zoneId : zones) {
                 s_logger.debug("Discovered non-legacy zone " + zoneId + ". Processing the
zone to associate with VMware datacenter.");
 
                 // All clusters in a non legacy zone will belong to the same VMware DC, hence
pick the first cluster
-                clustersQuery = conn.prepareStatement("select id from `cloud`.`cluster` where
removed is NULL AND data_center_id=?");
-                clustersQuery.setLong(1, zoneId);
-                clusters = clustersQuery.executeQuery();
-                clusters.next();
-                Long clusterId = clusters.getLong("id");
-
-                // Get VMware datacenter details from cluster_details table
-                String user = null;
-                String password = null;
-                String url = null;
-                clusterDetailsQuery = conn.prepareStatement("select name, value from `cloud`.`cluster_details`
where cluster_id=?");
-                clusterDetailsQuery.setLong(1, clusterId);
-                clusterDetails = clusterDetailsQuery.executeQuery();
-                while (clusterDetails.next()) {
-                    String key = clusterDetails.getString(1);
-                    String value = clusterDetails.getString(2);
-                    if (key.equalsIgnoreCase("username")) {
-                        user = value;
-                    } else if (key.equalsIgnoreCase("password")) {
-                        password = value;
-                    } else if (key.equalsIgnoreCase("url")) {
-                        url = value;
-                    }
-                }
-                String[] tokens = url.split("/"); // url format - http://vcenter/dc/cluster
-                String vc = tokens[2];
-                String dcName = tokens[3];
-                String guid = dcName + "@" + vc;
-
-                pstmt = conn.prepareStatement("INSERT INTO `cloud`.`vmware_data_center` (uuid,
name, guid, vcenter_host, username, password) values(?, ?, ?, ?, ?, ?)");
-                pstmt.setString(1, UUID.randomUUID().toString());
-                pstmt.setString(2, dcName);
-                pstmt.setString(3, guid);
-                pstmt.setString(4, vc);
-                pstmt.setString(5, user);
-                pstmt.setString(6, password);
-                pstmt.executeUpdate();
+                try (PreparedStatement clustersQuery = conn.prepareStatement("select id from
`cloud`.`cluster` where removed is NULL AND data_center_id=?");) {
+                    clustersQuery.setLong(1, zoneId);
+                    try (ResultSet clusters = clustersQuery.executeQuery();) {
+                        clusters.next();
+                        Long clusterId = clusters.getLong("id");
+
+                        // Get VMware datacenter details from cluster_details table
+                        String user = null;
+                        String password = null;
+                        String url = null;
+                        try (PreparedStatement clusterDetailsQuery = conn.prepareStatement("select
name, value from `cloud`.`cluster_details` where cluster_id=?");) {
+                            clusterDetailsQuery.setLong(1, clusterId);
+                            try (ResultSet clusterDetails = clusterDetailsQuery.executeQuery();)
{
+                                while (clusterDetails.next()) {
+                                    String key = clusterDetails.getString(1);
+                                    String value = clusterDetails.getString(2);
+                                    if (key.equalsIgnoreCase("username")) {
+                                        user = value;
+                                    } else if (key.equalsIgnoreCase("password")) {
+                                        password = value;
+                                    } else if (key.equalsIgnoreCase("url")) {
+                                        url = value;
+                                    }
+                                }
+                                String[] tokens = url.split("/"); // url format - http://vcenter/dc/cluster
+                                String vc = tokens[2];
+                                String dcName = tokens[3];
+                                String guid = dcName + "@" + vc;
+
+                                try (PreparedStatement insertVmWareDC = conn
+                                        .prepareStatement("INSERT INTO `cloud`.`vmware_data_center`
(uuid, name, guid, vcenter_host, username, password) values(?, ?, ?, ?, ?, ?)");) {
+                                    insertVmWareDC.setString(1, UUID.randomUUID().toString());
+                                    insertVmWareDC.setString(2, dcName);
+                                    insertVmWareDC.setString(3, guid);
+                                    insertVmWareDC.setString(4, vc);
+                                    insertVmWareDC.setString(5, user);
+                                    insertVmWareDC.setString(6, password);
+                                    insertVmWareDC.executeUpdate();
+                                }
+                                try (PreparedStatement selectVmWareDC = conn.prepareStatement("SELECT
id FROM `cloud`.`vmware_data_center` where guid=?");) {
+                                    selectVmWareDC.setString(1, guid);
+                                    try (ResultSet vmWareDcInfo = selectVmWareDC.executeQuery();)
{
+                                        Long vmwareDcId = -1L;
+                                        if (vmWareDcInfo.next()) {
+                                            vmwareDcId = vmWareDcInfo.getLong("id");
+                                        }
 
-                pstmt = conn.prepareStatement("SELECT id FROM `cloud`.`vmware_data_center`
where guid=?");
-                pstmt.setString(1, guid);
-                dcInfo = pstmt.executeQuery();
-                Long vmwareDcId = -1L;
-                if (dcInfo.next()) {
-                    vmwareDcId = dcInfo.getLong("id");
+                                        try (PreparedStatement insertMapping = conn
+                                                .prepareStatement("INSERT INTO `cloud`.`vmware_data_center_zone_map`
(zone_id, vmware_data_center_id) values(?, ?)");) {
+                                            insertMapping.setLong(1, zoneId);
+                                            insertMapping.setLong(2, vmwareDcId);
+                                            insertMapping.executeUpdate();
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                    }
                 }
-
-                pstmt = conn.prepareStatement("INSERT INTO `cloud`.`vmware_data_center_zone_map`
(zone_id, vmware_data_center_id) values(?, ?)");
-                pstmt.setLong(1, zoneId);
-                pstmt.setLong(2, vmwareDcId);
-                pstmt.executeUpdate();
             }
         } catch (SQLException e) {
             String msg = "Unable to update non legacy zones." + e.getMessage();
             s_logger.error(msg);
             throw new CloudRuntimeException(msg, e);
-        } finally {
-            try {
-                if (clustersQuery != null) {
-                    clustersQuery.close();
-                }
-                if (clusterDetails != null) {
-                    clusterDetails.close();
-                }
-                if (clusterDetailsQuery != null) {
-                    clusterDetailsQuery.close();
-                }
-                if (clusters != null) {
-                    clusters.close();
-                }
-                if (dcInfo != null) {
-                    dcInfo.close();
-                }
-                if (pstmt != null) {
-                    pstmt.close();
-                }
-            } catch (SQLException e) {
-            }
         }
     }
 


Mime
View raw message