Skip to content

Commit 2018d7d

Browse files
committed
Fixed Coverity Issues Reported
1 parent 3bb3442 commit 2018d7d

1 file changed

Lines changed: 8 additions & 20 deletions

File tree

‎engine/storage/src/org/apache/cloudstack/storage/endpoint/DefaultEndPointSelector.java‎

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,33 +115,21 @@ protected EndPoint findEndPointInScope(Scope scope, String sqlBase, Long poolId)
115115
// TODO: order by rand() is slow if there are lot of hosts
116116
sbuilder.append(" ORDER by rand() limit 1");
117117
String sql = sbuilder.toString();
118-
PreparedStatement pstmt = null;
119-
ResultSet rs = null;
120118
HostVO host = null;
121119
TransactionLegacy txn = TransactionLegacy.currentTxn();
122-
123-
try {
124-
pstmt = txn.prepareStatement(sql);
120+
try(PreparedStatement pstmt = txn.prepareStatement(sql);) {
125121
pstmt.setLong(1, poolId);
126-
rs = pstmt.executeQuery();
127-
while (rs.next()) {
128-
long id = rs.getLong(1);
129-
host = hostDao.findById(id);
122+
try(ResultSet rs = pstmt.executeQuery();) {
123+
while (rs.next()) {
124+
long id = rs.getLong(1);
125+
host = hostDao.findById(id);
126+
}
127+
}catch (SQLException e) {
128+
s_logger.warn("can't find endpoint", e);
130129
}
131130
} catch (SQLException e) {
132131
s_logger.warn("can't find endpoint", e);
133-
} finally {
134-
try {
135-
if (rs != null) {
136-
rs.close();
137-
}
138-
if (pstmt != null) {
139-
pstmt.close();
140-
}
141-
} catch (SQLException e) {
142-
}
143132
}
144-
145133
if (host == null) {
146134
return null;
147135
}

0 commit comments

Comments
 (0)