diff --git a/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java index 9bc373f8f3e4..ca79eedd5298 100644 --- a/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java +++ b/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java @@ -116,7 +116,6 @@ public void addIpRange(long dcId, long podId, String start, String end) { String insertSql = "INSERT INTO `cloud`.`op_dc_ip_address_alloc` (ip_address, data_center_id, pod_id, mac_address) " + "VALUES (?, ?, ?, (select mac_address from `cloud`.`data_center` where id=?))"; String updateSql = "UPDATE `cloud`.`data_center` set mac_address = mac_address+1 where id=?"; - PreparedStatement stmt = null; long startIP = NetUtils.ip2Long(start); long endIP = NetUtils.ip2Long(end); @@ -125,17 +124,17 @@ public void addIpRange(long dcId, long podId, String start, String end) { txn.start(); while (startIP <= endIP) { - stmt = txn.prepareStatement(insertSql); - stmt.setString(1, NetUtils.long2Ip(startIP++)); - stmt.setLong(2, dcId); - stmt.setLong(3, podId); - stmt.setLong(4, dcId); - stmt.executeUpdate(); - stmt.close(); - stmt = txn.prepareStatement(updateSql); - stmt.setLong(1, dcId); - stmt.executeUpdate(); - stmt.close(); + try(PreparedStatement insertPstmt = txn.prepareStatement(insertSql);) { + insertPstmt.setString(1, NetUtils.long2Ip(startIP++)); + insertPstmt.setLong(2, dcId); + insertPstmt.setLong(3, podId); + insertPstmt.setLong(4, dcId); + insertPstmt.executeUpdate(); + } + try(PreparedStatement updatePstmt = txn.prepareStatement(updateSql);) { + updatePstmt.setLong(1, dcId); + updatePstmt.executeUpdate(); + } } txn.commit(); } catch (SQLException ex) { diff --git a/engine/schema/src/com/cloud/host/dao/HostDaoImpl.java b/engine/schema/src/com/cloud/host/dao/HostDaoImpl.java index f1567ee51ea1..8342f1fcf773 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 void loadHostTags(HostVO host) { @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)); - } - } catch (Exception e) { - s_logger.warn("Exception: ", e); - } finally { - try { - if (rs != null) { - rs.close(); - } - if (pstmt != null) { - pstmt.close(); + try (ResultSet rs = pstmt.executeQuery();) { + while (rs.next()) { + long id = rs.getLong(1); //ID column + result.add(findById(id)); } - } catch (SQLException e) { } + } catch (SQLException e) { + s_logger.warn("Exception: ", e); } return result; } diff --git a/framework/db/src/com/cloud/utils/db/Merovingian2.java b/framework/db/src/com/cloud/utils/db/Merovingian2.java index 0c76fb5886d9..fe0b24d4dd90 100644 --- a/framework/db/src/com/cloud/utils/db/Merovingian2.java +++ b/framework/db/src/com/cloud/utils/db/Merovingian2.java @@ -71,7 +71,15 @@ private Merovingian2(long msId) { _concierge = new ConnectionConcierge("LockMaster", conn, true); } catch (SQLException e) { s_logger.error("Unable to get a new db connection", e); - throw new CloudRuntimeException("Unable to initialize a connection to the database for locking purposes: ", e); + throw new CloudRuntimeException("Unable to initialize a connection to the database for locking purposes", e); + } finally { + if (_concierge == null && conn != null) { + try { + conn.close(); + } catch (SQLException e) { + s_logger.debug("closing connection failed after everything else.", e); + } + } } }