Skip to content

Commit 3bedeea

Browse files
committed
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>
1 parent 79709cd commit 3bedeea

1 file changed

Lines changed: 40 additions & 56 deletions

File tree

engine/schema/src/com/cloud/upgrade/dao/VersionDaoImpl.java

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -75,49 +75,39 @@ public VersionVO findByVersion(final String version, final Step step) {
7575
@Override
7676
@DB
7777
public String getCurrentVersion() {
78-
Connection conn = null;
79-
try {
78+
try (Connection conn = TransactionLegacy.getStandaloneConnection();) {
8079
s_logger.debug("Checking to see if the database is at a version before it was the version table is created");
8180

82-
conn = TransactionLegacy.getStandaloneConnection();
83-
84-
PreparedStatement pstmt = conn.prepareStatement("SHOW TABLES LIKE 'version'");
85-
ResultSet rs = pstmt.executeQuery();
86-
if (!rs.next()) {
87-
rs.close();
88-
pstmt.close();
89-
pstmt = conn.prepareStatement("SHOW TABLES LIKE 'nics'");
90-
rs = pstmt.executeQuery();
81+
try (
82+
PreparedStatement pstmt = conn.prepareStatement("SHOW TABLES LIKE 'version'");
83+
ResultSet rs = pstmt.executeQuery();
84+
) {
9185
if (!rs.next()) {
92-
rs.close();
93-
pstmt.close();
94-
95-
pstmt = conn.prepareStatement("SELECT domain_id FROM account_vlan_map LIMIT 1");
96-
try {
97-
pstmt.executeQuery();
98-
return "2.1.8";
99-
} catch (final SQLException e) {
100-
s_logger.debug("Assuming the exception means domain_id is not there.");
101-
s_logger.debug("No version table and no nics table, returning 2.1.7");
102-
return "2.1.7";
103-
} finally {
104-
pstmt.close();
105-
}
106-
} else {
107-
try {
108-
rs.close();
109-
pstmt.close();
110-
pstmt = conn.prepareStatement("SELECT is_static_nat from firewall_rules");
111-
rs = pstmt.executeQuery();
112-
return "2.2.1";
113-
} catch (final SQLException e) {
114-
s_logger.debug("Assuming the exception means static_nat field doesn't exist in firewall_rules table, returning version 2.2.2");
115-
return "2.2.2";
116-
} finally {
117-
rs.close();
118-
pstmt.close();
86+
try (PreparedStatement pstmt_nics = conn.prepareStatement("SHOW TABLES LIKE 'nics'");
87+
ResultSet rs_nics = pstmt_nics.executeQuery();
88+
) {
89+
if (!rs_nics.next()) {
90+
try (PreparedStatement pstmt_domain = conn.prepareStatement("SELECT domain_id FROM account_vlan_map LIMIT 1"); ){
91+
pstmt_domain.executeQuery();
92+
return "2.1.8";
93+
} catch (final SQLException e) {
94+
s_logger.debug("Assuming the exception means domain_id is not there.");
95+
s_logger.debug("No version table and no nics table, returning 2.1.7");
96+
return "2.1.7";
97+
}
98+
} else {
99+
try (PreparedStatement pstmt_static_nat = conn.prepareStatement("SELECT is_static_nat from firewall_rules");
100+
ResultSet rs_static_nat = pstmt_static_nat.executeQuery();){
101+
return "2.2.1";
102+
} catch (final SQLException e) {
103+
s_logger.debug("Assuming the exception means static_nat field doesn't exist in firewall_rules table, returning version 2.2.2");
104+
return "2.2.2";
105+
}
106+
}
119107
}
120108
}
109+
} catch (final SQLException e) {
110+
throw new CloudRuntimeException("Unable to get the current version", e);
121111
}
122112

123113
SearchCriteria<String> sc = CurrentVersionSearch.create();
@@ -137,36 +127,30 @@ public String getCurrentVersion() {
137127
}
138128

139129
// Use nics table information and is_static_nat field from firewall_rules table to determine version information
140-
try {
141-
s_logger.debug("Version table exists, but it's empty; have to confirm that version is 2.2.2");
142-
pstmt = conn.prepareStatement("SHOW TABLES LIKE 'nics'");
143-
rs = pstmt.executeQuery();
130+
s_logger.debug("Version table exists, but it's empty; have to confirm that version is 2.2.2");
131+
try (PreparedStatement pstmt = conn.prepareStatement("SHOW TABLES LIKE 'nics'");
132+
ResultSet rs = pstmt.executeQuery();){
144133
if (!rs.next()) {
145134
throw new CloudRuntimeException("Unable to determine the current version, version table exists and empty, nics table doesn't exist");
146135
} else {
147-
pstmt = conn.prepareStatement("SELECT is_static_nat from firewall_rules");
148-
pstmt.executeQuery();
149-
throw new CloudRuntimeException("Unable to determine the current version, version table exists and empty, " +
150-
"nics table doesn't exist, is_static_nat field exists in firewall_rules table");
136+
try (PreparedStatement pstmt_static_nat = conn.prepareStatement("SELECT is_static_nat from firewall_rules"); ) {
137+
pstmt_static_nat.executeQuery();
138+
throw new CloudRuntimeException("Unable to determine the current version, version table exists and empty, " +
139+
"nics table doesn't exist, is_static_nat field exists in firewall_rules table");
140+
} catch (final SQLException e) {
141+
s_logger.debug("Assuming the exception means static_nat field doesn't exist in firewall_rules table, returning version 2.2.2");
142+
return "2.2.2";
143+
}
151144
}
152145
} catch (final SQLException e) {
153-
s_logger.debug("Assuming the exception means static_nat field doesn't exist in firewall_rules table, returning version 2.2.2");
154-
return "2.2.2";
155-
} finally {
156-
rs.close();
157-
pstmt.close();
146+
throw new CloudRuntimeException("Unable to determine the current version, version table exists and empty, query for nics table yields SQL exception", e);
158147
}
159148
} else {
160149
return upgradedVersions.get(0);
161150
}
162151

163152
} catch (final SQLException e) {
164153
throw new CloudRuntimeException("Unable to get the current version", e);
165-
} finally {
166-
try {
167-
conn.close();
168-
} catch (final SQLException e) {
169-
}
170154
}
171155

172156
}

0 commit comments

Comments
 (0)