Skip to content

Commit 2b9f5b2

Browse files
committed
extra try-w-r
1 parent 119f6b0 commit 2b9f5b2

1 file changed

Lines changed: 127 additions & 142 deletions

File tree

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

Lines changed: 127 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -583,170 +583,155 @@ protected long insertNetwork(Connection conn, String name, String displayText, S
583583
}
584584

585585
protected void upgradeManagementIpAddress(Connection conn, long dcId) throws SQLException {
586-
PreparedStatement pstmt = conn.prepareStatement("SELECT op_dc_ip_address_alloc.id FROM op_dc_ip_address_alloc WHERE data_center_id=?");
587-
pstmt.setLong(1, dcId);
588-
ResultSet rs = pstmt.executeQuery();
589586
ArrayList<Object[]> allocatedIps = new ArrayList<Object[]>();
590-
while (rs.next()) {
591-
Object[] ip = new Object[10];
592-
ip[0] = rs.getLong(1); // id
593-
allocatedIps.add(ip);
587+
try (PreparedStatement pstmt = conn.prepareStatement("SELECT op_dc_ip_address_alloc.id FROM op_dc_ip_address_alloc WHERE data_center_id=?");) {
588+
pstmt.setLong(1, dcId);
589+
try (ResultSet rs = pstmt.executeQuery();) {
590+
while (rs.next()) {
591+
Object[] ip = new Object[10];
592+
ip[0] = rs.getLong(1); // id
593+
allocatedIps.add(ip);
594+
}
595+
}
594596
}
595-
rs.close();
596-
pstmt.close();
597-
598597
for (Object[] allocatedIp : allocatedIps) {
599-
pstmt = conn.prepareStatement("SELECT mac_address FROM data_center WHERE id = ?");
600-
pstmt.setLong(1, dcId);
601-
rs = pstmt.executeQuery();
602-
if (!rs.next()) {
603-
throw new CloudRuntimeException("Unable to get mac address for data center " + dcId);
598+
try (PreparedStatement pstmt = conn.prepareStatement("SELECT mac_address FROM data_center WHERE id = ?");) {
599+
pstmt.setLong(1, dcId);
600+
try (ResultSet rs = pstmt.executeQuery();) {
601+
if (!rs.next()) {
602+
throw new CloudRuntimeException("Unable to get mac address for data center " + dcId);
603+
}
604+
long mac = rs.getLong(1);
605+
try (PreparedStatement updateDcMacAddress = conn.prepareStatement("UPDATE data_center SET mac_address=mac_address+1 WHERE id = ?");) {
606+
updateDcMacAddress.setLong(1, dcId);
607+
updateDcMacAddress.executeUpdate();
608+
}
609+
try(PreparedStatement updateDcIp = conn.prepareStatement("UPDATE op_dc_ip_address_alloc SET mac_address=? WHERE id=?");) {
610+
updateDcIp.setLong(1, mac);
611+
updateDcIp.setLong(2, (Long)allocatedIp[0]);
612+
updateDcIp.executeUpdate();
613+
}
614+
}
604615
}
605-
long mac = rs.getLong(1);
606-
rs.close();
607-
pstmt.close();
608-
609-
pstmt = conn.prepareStatement("UPDATE data_center SET mac_address=mac_address+1 WHERE id = ?");
610-
pstmt.setLong(1, dcId);
611-
pstmt.executeUpdate();
612-
pstmt.close();
613-
614-
pstmt = conn.prepareStatement("UPDATE op_dc_ip_address_alloc SET mac_address=? WHERE id=?");
615-
pstmt.setLong(1, mac);
616-
pstmt.setLong(2, (Long)allocatedIp[0]);
617-
pstmt.executeUpdate();
618-
pstmt.close();
619616
}
620-
621617
}
622618

623619
protected void upgradeDirectUserIpAddress(Connection conn, long dcId, long networkId, String vlanType) throws SQLException {
624620
s_logger.debug("Upgrading user ip address for data center " + dcId + " network " + networkId + " vlan type " + vlanType);
625-
PreparedStatement pstmt =
626-
conn.prepareStatement("UPDATE user_ip_address INNER JOIN vlan ON user_ip_address.vlan_db_id=vlan.id SET user_ip_address.source_network_id=vlan.network_id WHERE user_ip_address.data_center_id=? AND vlan.vlan_type=?");
627-
pstmt.setLong(1, dcId);
628-
pstmt.setString(2, vlanType);
629-
pstmt.executeUpdate();
630-
pstmt.close();
631-
632-
pstmt =
633-
conn.prepareStatement("SELECT user_ip_address.id, user_ip_address.public_ip_address, user_ip_address.account_id, user_ip_address.allocated FROM user_ip_address INNER JOIN vlan ON vlan.id=user_ip_address.vlan_db_id WHERE user_ip_address.data_center_id = ? AND vlan.vlan_type=?");
634-
pstmt.setLong(1, dcId);
635-
pstmt.setString(2, vlanType);
636-
ResultSet rs = pstmt.executeQuery();
637-
ArrayList<Object[]> allocatedIps = new ArrayList<Object[]>();
638-
while (rs.next()) {
639-
Object[] ip = new Object[10];
640-
ip[0] = rs.getLong(1); // id
641-
ip[1] = rs.getString(2); // ip address
642-
ip[2] = rs.getLong(3); // account id
643-
ip[3] = rs.getDate(4); // allocated
644-
allocatedIps.add(ip);
621+
try (PreparedStatement pstmt =
622+
conn.prepareStatement("UPDATE user_ip_address INNER JOIN vlan ON user_ip_address.vlan_db_id=vlan.id SET user_ip_address.source_network_id=vlan.network_id WHERE user_ip_address.data_center_id=? AND vlan.vlan_type=?");) {
623+
pstmt.setLong(1, dcId);
624+
pstmt.setString(2, vlanType);
625+
pstmt.executeUpdate();
645626
}
646-
rs.close();
647-
pstmt.close();
648-
649-
s_logger.debug("Marking " + allocatedIps.size() + " ip addresses to belong to network " + networkId);
650-
s_logger.debug("Updating mac addresses for data center id=" + dcId + ". Found " + allocatedIps.size() + " ip addresses to update");
651-
652-
for (Object[] allocatedIp : allocatedIps) {
653-
pstmt = conn.prepareStatement("SELECT mac_address FROM data_center WHERE id = ?");
627+
try (PreparedStatement pstmt =
628+
conn.prepareStatement("SELECT user_ip_address.id, user_ip_address.public_ip_address, user_ip_address.account_id, user_ip_address.allocated FROM user_ip_address INNER JOIN vlan ON vlan.id=user_ip_address.vlan_db_id WHERE user_ip_address.data_center_id = ? AND vlan.vlan_type=?");) {
654629
pstmt.setLong(1, dcId);
655-
rs = pstmt.executeQuery();
656-
if (!rs.next()) {
657-
throw new CloudRuntimeException("Unable to get mac address for data center " + dcId);
630+
pstmt.setString(2, vlanType);
631+
try (ResultSet rs = pstmt.executeQuery();) {
632+
ArrayList<Object[]> allocatedIps = new ArrayList<Object[]>();
633+
while (rs.next()) {
634+
Object[] ip = new Object[10];
635+
ip[0] = rs.getLong(1); // id
636+
ip[1] = rs.getString(2); // ip address
637+
ip[2] = rs.getLong(3); // account id
638+
ip[3] = rs.getDate(4); // allocated
639+
allocatedIps.add(ip);
640+
}
641+
s_logger.debug("Marking " + allocatedIps.size() + " ip addresses to belong to network " + networkId);
642+
s_logger.debug("Updating mac addresses for data center id=" + dcId + ". Found " + allocatedIps.size() + " ip addresses to update");
643+
for (Object[] allocatedIp : allocatedIps) {
644+
try (PreparedStatement selectMacAdresses = conn.prepareStatement("SELECT mac_address FROM data_center WHERE id = ?");) {
645+
selectMacAdresses.setLong(1, dcId);
646+
try (ResultSet selectedMacAdresses = selectMacAdresses.executeQuery();) {
647+
if (!selectedMacAdresses.next()) {
648+
throw new CloudRuntimeException("Unable to get mac address for data center " + dcId);
649+
}
650+
long mac = selectedMacAdresses.getLong(1);
651+
try (PreparedStatement updateDataCenter = conn.prepareStatement("UPDATE data_center SET mac_address=mac_address+1 WHERE id = ?");) {
652+
updateDataCenter.setLong(1, dcId);
653+
updateDataCenter.executeUpdate();
654+
}
655+
try (PreparedStatement updateUserIpAddress = conn.prepareStatement("UPDATE user_ip_address SET mac_address=? WHERE id=?");) {
656+
updateUserIpAddress.setLong(1, mac);
657+
updateUserIpAddress.setLong(2, (Long)allocatedIp[0]);
658+
updateUserIpAddress.executeUpdate();
659+
}
660+
}
661+
}
662+
}
658663
}
659-
long mac = rs.getLong(1);
660-
rs.close();
661-
pstmt.close();
662-
663-
pstmt = conn.prepareStatement("UPDATE data_center SET mac_address=mac_address+1 WHERE id = ?");
664-
pstmt.setLong(1, dcId);
665-
pstmt.executeUpdate();
666-
pstmt.close();
667-
668-
pstmt = conn.prepareStatement("UPDATE user_ip_address SET mac_address=? WHERE id=?");
669-
pstmt.setLong(1, mac);
670-
pstmt.setLong(2, (Long)allocatedIp[0]);
671-
pstmt.executeUpdate();
672-
pstmt.close();
673664
}
674665
}
675666

676667
protected void upgradePublicUserIpAddress(Connection conn, long dcId, long networkId, String vlanType) throws SQLException {
677668
s_logger.debug("Upgrading user ip address for data center " + dcId + " network " + networkId + " vlan type " + vlanType);
678-
PreparedStatement pstmt =
679-
conn.prepareStatement("UPDATE user_ip_address INNER JOIN vlan ON user_ip_address.vlan_db_id=vlan.id SET source_network_id=? WHERE user_ip_address.data_center_id=? AND vlan.vlan_type=?");
680-
pstmt.setLong(1, networkId);
681-
pstmt.setLong(2, dcId);
682-
pstmt.setString(3, vlanType);
683-
pstmt.executeUpdate();
684-
pstmt.close();
685-
686-
pstmt = conn.prepareStatement("UPDATE vlan SET network_id = ? WHERE data_center_id=? AND vlan_type=?");
687-
pstmt.setLong(1, networkId);
688-
pstmt.setLong(2, dcId);
689-
pstmt.setString(3, vlanType);
690-
pstmt.executeUpdate();
691-
pstmt.close();
692-
693-
pstmt =
694-
conn.prepareStatement("SELECT user_ip_address.id, user_ip_address.public_ip_address, user_ip_address.account_id, user_ip_address.allocated FROM user_ip_address INNER JOIN vlan ON vlan.id=user_ip_address.vlan_db_id WHERE user_ip_address.data_center_id = ? AND vlan.vlan_type='VirtualNetwork'");
695-
pstmt.setLong(1, dcId);
696-
ResultSet rs = pstmt.executeQuery();
697-
ArrayList<Object[]> allocatedIps = new ArrayList<Object[]>();
698-
while (rs.next()) {
699-
Object[] ip = new Object[10];
700-
ip[0] = rs.getLong(1); // id
701-
ip[1] = rs.getString(2); // ip address
702-
ip[2] = rs.getLong(3); // account id
703-
ip[3] = rs.getDate(4); // allocated
704-
allocatedIps.add(ip);
669+
try (PreparedStatement pstmt =
670+
conn.prepareStatement("UPDATE user_ip_address INNER JOIN vlan ON user_ip_address.vlan_db_id=vlan.id SET source_network_id=? WHERE user_ip_address.data_center_id=? AND vlan.vlan_type=?");) {
671+
pstmt.setLong(1, networkId);
672+
pstmt.setLong(2, dcId);
673+
pstmt.setString(3, vlanType);
674+
pstmt.executeUpdate();
705675
}
706-
rs.close();
707-
pstmt.close();
708-
709-
for (Object[] allocatedIp : allocatedIps) {
710-
pstmt = conn.prepareStatement("SELECT mac_address FROM data_center WHERE id = ?");
711-
pstmt.setLong(1, dcId);
712-
rs = pstmt.executeQuery();
713-
if (!rs.next()) {
714-
throw new CloudRuntimeException("Unable to get mac address for data center " + dcId);
715-
}
716-
long mac = rs.getLong(1);
717-
rs.close();
718-
pstmt.close();
719-
720-
pstmt = conn.prepareStatement("UPDATE data_center SET mac_address=mac_address+1 WHERE id = ?");
721-
pstmt.setLong(1, dcId);
676+
try (PreparedStatement pstmt = conn.prepareStatement("UPDATE vlan SET network_id = ? WHERE data_center_id=? AND vlan_type=?");) {
677+
pstmt.setLong(1, networkId);
678+
pstmt.setLong(2, dcId);
679+
pstmt.setString(3, vlanType);
722680
pstmt.executeUpdate();
723-
pstmt.close();
724-
725-
Long associatedNetworkId = null;
726-
if (allocatedIp[3] != null && allocatedIp[2] != null) {
727-
pstmt = conn.prepareStatement("SELECT id FROM networks WHERE data_center_id=? AND account_id=?");
728-
pstmt.setLong(1, dcId);
729-
pstmt.setLong(2, (Long)allocatedIp[2]);
730-
rs = pstmt.executeQuery();
731-
if (!rs.next()) {
732-
throw new CloudRuntimeException("Unable to find a network for account " + allocatedIp[2] + " in dc " + dcId);
681+
}
682+
try (PreparedStatement pstmt =
683+
conn.prepareStatement("SELECT user_ip_address.id, user_ip_address.public_ip_address, user_ip_address.account_id, user_ip_address.allocated FROM user_ip_address INNER JOIN vlan ON vlan.id=user_ip_address.vlan_db_id WHERE user_ip_address.data_center_id = ? AND vlan.vlan_type='VirtualNetwork'");) {
684+
pstmt.setLong(1, dcId);
685+
try (ResultSet rs = pstmt.executeQuery();) {
686+
ArrayList<Object[]> allocatedIps = new ArrayList<Object[]>();
687+
while (rs.next()) {
688+
Object[] ip = new Object[10];
689+
ip[0] = rs.getLong(1); // id
690+
ip[1] = rs.getString(2); // ip address
691+
ip[2] = rs.getLong(3); // account id
692+
ip[3] = rs.getDate(4); // allocated
693+
allocatedIps.add(ip);
694+
}
695+
for (Object[] allocatedIp : allocatedIps) {
696+
try (PreparedStatement selectDataCenterMac = conn.prepareStatement("SELECT mac_address FROM data_center WHERE id = ?");) {
697+
selectDataCenterMac.setLong(1, dcId);
698+
try (ResultSet selectedDataCenterMac = selectDataCenterMac.executeQuery();) {
699+
if (!selectedDataCenterMac.next()) {
700+
throw new CloudRuntimeException("Unable to get mac address for data center " + dcId);
701+
}
702+
long mac = selectedDataCenterMac.getLong(1);
703+
try (PreparedStatement updateDataCenter = conn.prepareStatement("UPDATE data_center SET mac_address=mac_address+1 WHERE id = ?");) {
704+
updateDataCenter.setLong(1, dcId);
705+
updateDataCenter.executeUpdate();
706+
}
707+
Long associatedNetworkId = null;
708+
if (allocatedIp[3] != null && allocatedIp[2] != null) {
709+
try (PreparedStatement selectNetworks = conn.prepareStatement("SELECT id FROM networks WHERE data_center_id=? AND account_id=?");) {
710+
selectNetworks.setLong(1, dcId);
711+
selectNetworks.setLong(2, (Long)allocatedIp[2]);
712+
try (ResultSet selectedNetworks = selectNetworks.executeQuery();) {
713+
if (!selectedNetworks.next()) {
714+
throw new CloudRuntimeException("Unable to find a network for account " + allocatedIp[2] + " in dc " + dcId);
715+
}
716+
associatedNetworkId = selectedNetworks.getLong(1);
717+
}
718+
}
719+
}
720+
try (PreparedStatement updateUserIpAddress = conn.prepareStatement("UPDATE user_ip_address SET mac_address=?, network_id=? WHERE id=?");) {
721+
updateUserIpAddress.setLong(1, mac);
722+
if (associatedNetworkId != null) {
723+
updateUserIpAddress.setLong(2, associatedNetworkId);
724+
} else {
725+
updateUserIpAddress.setObject(2, null);
726+
}
727+
updateUserIpAddress.setLong(3, (Long)allocatedIp[0]);
728+
updateUserIpAddress.executeUpdate();
729+
}
730+
}
731+
}
733732
}
734-
associatedNetworkId = rs.getLong(1);
735-
rs.close();
736-
pstmt.close();
737-
}
738-
pstmt = conn.prepareStatement("UPDATE user_ip_address SET mac_address=?, network_id=? WHERE id=?");
739-
pstmt.setLong(1, mac);
740-
if (associatedNetworkId != null) {
741-
pstmt.setLong(2, associatedNetworkId);
742-
} else {
743-
pstmt.setObject(2, null);
744733
}
745-
pstmt.setLong(3, (Long)allocatedIp[0]);
746-
pstmt.executeUpdate();
747-
pstmt.close();
748734
}
749-
750735
}
751736

752737
protected void upgradeDataCenter(Connection conn) {

0 commit comments

Comments
 (0)