diff --git a/engine/schema/src/com/cloud/host/dao/HostDaoImpl.java b/engine/schema/src/com/cloud/host/dao/HostDaoImpl.java index f1567ee51ea..8342f1fcf77 100644 --- a/engine/schema/src/com/cloud/host/dao/HostDaoImpl.java +++ b/engine/schema/src/com/cloud/host/dao/HostDaoImpl.java @@ -740,32 +740,21 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao @DB @Override public List findLostHosts(long timeout) { - TransactionLegacy txn = TransactionLegacy.currentTxn(); - PreparedStatement pstmt = null; List result = new ArrayList(); - ResultSet rs = null; - try { - String sql = + String sql = "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') ;"; - pstmt = txn.prepareStatement(sql); + try ( + TransactionLegacy txn = TransactionLegacy.currentTxn(); + PreparedStatement pstmt = txn.prepareStatement(sql);) { pstmt.setLong(1, timeout); - rs = pstmt.executeQuery(); - while (rs.next()) { - long id = rs.getLong(1); //ID column - result.add(findById(id)); + try (ResultSet rs = pstmt.executeQuery();) { + while (rs.next()) { + long id = rs.getLong(1); //ID column + result.add(findById(id)); + } } - } catch (Exception e) { + } catch (SQLException e) { s_logger.warn("Exception: ", e); - } finally { - try { - if (rs != null) { - rs.close(); - } - if (pstmt != null) { - pstmt.close(); - } - } catch (SQLException e) { - } } return result; }