cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject [01/10] git commit: updated refs/heads/master to b0136c5
Date Thu, 09 Jul 2015 19:30:44 GMT
Repository: cloudstack
Updated Branches:
  refs/heads/master bcc8182f2 -> b0136c56e


coverity 1116509: heavy entanglement of prepared statements and result sets hope I got the
logic right. this is for very old versions, might be worth phasing out unless someone still
uses it.

Signed-off-by: Daan Hoogland <daan@onecht.net>


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

Branch: refs/heads/master
Commit: 3bedeeaacc9bbfde00df24bf63b45c82c6cbe52e
Parents: 79709cd
Author: Daan Hoogland <daan@onecht.net>
Authored: Tue Jul 7 13:20:39 2015 +0200
Committer: Daan Hoogland <daan@onecht.net>
Committed: Thu Jul 9 21:30:23 2015 +0200

----------------------------------------------------------------------
 .../com/cloud/upgrade/dao/VersionDaoImpl.java   | 96 ++++++++------------
 1 file changed, 40 insertions(+), 56 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/3bedeeaa/engine/schema/src/com/cloud/upgrade/dao/VersionDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/upgrade/dao/VersionDaoImpl.java b/engine/schema/src/com/cloud/upgrade/dao/VersionDaoImpl.java
index bc9c2f0..0fb2dfe 100644
--- a/engine/schema/src/com/cloud/upgrade/dao/VersionDaoImpl.java
+++ b/engine/schema/src/com/cloud/upgrade/dao/VersionDaoImpl.java
@@ -75,49 +75,39 @@ public class VersionDaoImpl extends GenericDaoBase<VersionVO, Long>
implements V
     @Override
     @DB
     public String getCurrentVersion() {
-        Connection conn = null;
-        try {
+        try (Connection conn = TransactionLegacy.getStandaloneConnection();) {
             s_logger.debug("Checking to see if the database is at a version before it was
the version table is created");
 
-            conn = TransactionLegacy.getStandaloneConnection();
-
-            PreparedStatement pstmt = conn.prepareStatement("SHOW TABLES LIKE 'version'");
-            ResultSet rs = pstmt.executeQuery();
-            if (!rs.next()) {
-                rs.close();
-                pstmt.close();
-                pstmt = conn.prepareStatement("SHOW TABLES LIKE 'nics'");
-                rs = pstmt.executeQuery();
+            try (
+                    PreparedStatement pstmt = conn.prepareStatement("SHOW TABLES LIKE 'version'");
+                    ResultSet rs = pstmt.executeQuery();
+                ) {
                 if (!rs.next()) {
-                    rs.close();
-                    pstmt.close();
-
-                    pstmt = conn.prepareStatement("SELECT domain_id FROM account_vlan_map
LIMIT 1");
-                    try {
-                        pstmt.executeQuery();
-                        return "2.1.8";
-                    } catch (final SQLException e) {
-                        s_logger.debug("Assuming the exception means domain_id is not there.");
-                        s_logger.debug("No version table and no nics table, returning 2.1.7");
-                        return "2.1.7";
-                    } finally {
-                        pstmt.close();
-                    }
-                } else {
-                    try {
-                        rs.close();
-                        pstmt.close();
-                        pstmt = conn.prepareStatement("SELECT is_static_nat from firewall_rules");
-                        rs = pstmt.executeQuery();
-                        return "2.2.1";
-                    } catch (final SQLException e) {
-                        s_logger.debug("Assuming the exception means static_nat field doesn't
exist in firewall_rules table, returning version 2.2.2");
-                        return "2.2.2";
-                    } finally {
-                        rs.close();
-                        pstmt.close();
+                    try (PreparedStatement pstmt_nics = conn.prepareStatement("SHOW TABLES
LIKE 'nics'");
+                         ResultSet rs_nics = pstmt_nics.executeQuery();
+                        ) {
+                        if (!rs_nics.next()) {
+                            try (PreparedStatement pstmt_domain = conn.prepareStatement("SELECT
domain_id FROM account_vlan_map LIMIT 1"); ){
+                                pstmt_domain.executeQuery();
+                                return "2.1.8";
+                            } catch (final SQLException e) {
+                                s_logger.debug("Assuming the exception means domain_id is
not there.");
+                                s_logger.debug("No version table and no nics table, returning
2.1.7");
+                                return "2.1.7";
+                            }
+                        } else {
+                            try (PreparedStatement pstmt_static_nat = conn.prepareStatement("SELECT
is_static_nat from firewall_rules");
+                                 ResultSet rs_static_nat = pstmt_static_nat.executeQuery();){
+                                return "2.2.1";
+                            } catch (final SQLException e) {
+                                s_logger.debug("Assuming the exception means static_nat field
doesn't exist in firewall_rules table, returning version 2.2.2");
+                                return "2.2.2";
+                            }
+                        }
                     }
                 }
+            } catch (final SQLException e) {
+                throw new CloudRuntimeException("Unable to get the current version", e);
             }
 
             SearchCriteria<String> sc = CurrentVersionSearch.create();
@@ -137,24 +127,23 @@ public class VersionDaoImpl extends GenericDaoBase<VersionVO, Long>
implements V
                 }
 
                 // Use nics table information and is_static_nat field from firewall_rules
table to determine version information
-                try {
-                    s_logger.debug("Version table exists, but it's empty; have to confirm
that version is 2.2.2");
-                    pstmt = conn.prepareStatement("SHOW TABLES LIKE 'nics'");
-                    rs = pstmt.executeQuery();
+                s_logger.debug("Version table exists, but it's empty; have to confirm that
version is 2.2.2");
+                try (PreparedStatement pstmt = conn.prepareStatement("SHOW TABLES LIKE 'nics'");
+                     ResultSet rs = pstmt.executeQuery();){
                     if (!rs.next()) {
                         throw new CloudRuntimeException("Unable to determine the current
version, version table exists and empty, nics table doesn't exist");
                     } else {
-                        pstmt = conn.prepareStatement("SELECT is_static_nat from firewall_rules");
-                        pstmt.executeQuery();
-                        throw new CloudRuntimeException("Unable to determine the current
version, version table exists and empty, " +
-                                "nics table doesn't exist, is_static_nat field exists in
firewall_rules table");
+                        try (PreparedStatement pstmt_static_nat = conn.prepareStatement("SELECT
is_static_nat from firewall_rules"); ) {
+                            pstmt_static_nat.executeQuery();
+                            throw new CloudRuntimeException("Unable to determine the current
version, version table exists and empty, " +
+                                    "nics table doesn't exist, is_static_nat field exists
in firewall_rules table");
+                        } catch (final SQLException e) {
+                            s_logger.debug("Assuming the exception means static_nat field
doesn't exist in firewall_rules table, returning version 2.2.2");
+                            return "2.2.2";
+                        }
                     }
                 } catch (final SQLException e) {
-                    s_logger.debug("Assuming the exception means static_nat field doesn't
exist in firewall_rules table, returning version 2.2.2");
-                    return "2.2.2";
-                } finally {
-                    rs.close();
-                    pstmt.close();
+                    throw new CloudRuntimeException("Unable to determine the current version,
version table exists and empty, query for nics table yields SQL exception", e);
                 }
             } else {
                 return upgradedVersions.get(0);
@@ -162,11 +151,6 @@ public class VersionDaoImpl extends GenericDaoBase<VersionVO, Long>
implements V
 
         } catch (final SQLException e) {
             throw new CloudRuntimeException("Unable to get the current version", e);
-        } finally {
-            try {
-                conn.close();
-            } catch (final SQLException e) {
-            }
         }
 
     }


Mime
View raw message