Skip to content

Commit 800604c

Browse files
committed
coverity 1116711: findLostHost trivial try-with-resource inserted
1 parent d0ac719 commit 800604c

1 file changed

Lines changed: 10 additions & 21 deletions

File tree

engine/schema/src/com/cloud/host/dao/HostDaoImpl.java

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -740,32 +740,21 @@ public void loadHostTags(HostVO host) {
740740
@DB
741741
@Override
742742
public List<HostVO> findLostHosts(long timeout) {
743-
TransactionLegacy txn = TransactionLegacy.currentTxn();
744-
PreparedStatement pstmt = null;
745743
List<HostVO> result = new ArrayList<HostVO>();
746-
ResultSet rs = null;
747-
try {
748-
String sql =
744+
String sql =
749745
"select h.id from host h left join cluster c on h.cluster_id=c.id where h.mgmt_server_id is not null and h.last_ping < ? and h.status in ('Up', 'Updating', 'Disconnected', 'Connecting') and h.type not in ('ExternalFirewall', 'ExternalLoadBalancer', 'TrafficMonitor', 'SecondaryStorage', 'LocalSecondaryStorage', 'L2Networking') and (h.cluster_id is null or c.managed_state = 'Managed') ;";
750-
pstmt = txn.prepareStatement(sql);
746+
try (
747+
TransactionLegacy txn = TransactionLegacy.currentTxn();
748+
PreparedStatement pstmt = txn.prepareStatement(sql);) {
751749
pstmt.setLong(1, timeout);
752-
rs = pstmt.executeQuery();
753-
while (rs.next()) {
754-
long id = rs.getLong(1); //ID column
755-
result.add(findById(id));
756-
}
757-
} catch (Exception e) {
758-
s_logger.warn("Exception: ", e);
759-
} finally {
760-
try {
761-
if (rs != null) {
762-
rs.close();
763-
}
764-
if (pstmt != null) {
765-
pstmt.close();
750+
try (ResultSet rs = pstmt.executeQuery();) {
751+
while (rs.next()) {
752+
long id = rs.getLong(1); //ID column
753+
result.add(findById(id));
766754
}
767-
} catch (SQLException e) {
768755
}
756+
} catch (SQLException e) {
757+
s_logger.warn("Exception: ", e);
769758
}
770759
return result;
771760
}

0 commit comments

Comments
 (0)