mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
try-with-resource to prevent resource leaks
This commit is contained in:
parent
ac1b4b0316
commit
39f775c381
@ -67,28 +67,25 @@ public class Upgrade430to440 implements DbUpgrade {
|
||||
}
|
||||
|
||||
private void addExtractTemplateAndVolumeColumns(Connection conn) {
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
|
||||
try {
|
||||
try (PreparedStatement selectTemplateInfostmt = conn.prepareStatement("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'cloud' AND TABLE_NAME = 'template_store_ref' AND COLUMN_NAME = 'download_url_created'");
|
||||
ResultSet templateInfoResults = selectTemplateInfostmt.executeQuery();
|
||||
PreparedStatement addDownloadUrlCreatedToTemplateStorerefstatement = conn.prepareStatement("ALTER TABLE `cloud`.`template_store_ref` ADD COLUMN `download_url_created` datetime");
|
||||
PreparedStatement addDownloadUrlToTemplateStorerefstatement = conn.prepareStatement("ALTER TABLE `cloud`.`template_store_ref` ADD COLUMN `download_url` varchar(255)");
|
||||
PreparedStatement selectVolumeInfostmt = conn.prepareStatement("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'cloud' AND TABLE_NAME = 'volume_store_ref' AND COLUMN_NAME = 'download_url_created'");
|
||||
ResultSet volumeInfoResults = selectVolumeInfostmt.executeQuery();
|
||||
PreparedStatement addDownloadUrlCreatedToVolumeStorerefstatement = conn.prepareStatement("ALTER TABLE `cloud`.`volume_store_ref` ADD COLUMN `download_url_created` datetime");
|
||||
) {
|
||||
|
||||
// Add download_url_created, download_url to template_store_ref
|
||||
pstmt = conn.prepareStatement("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'cloud' AND TABLE_NAME = 'template_store_ref' AND COLUMN_NAME = 'download_url_created'");
|
||||
rs = pstmt.executeQuery();
|
||||
if (!rs.next()) {
|
||||
pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`template_store_ref` ADD COLUMN `download_url_created` datetime");
|
||||
pstmt.executeUpdate();
|
||||
|
||||
pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`template_store_ref` ADD COLUMN `download_url` varchar(255)");
|
||||
pstmt.executeUpdate();
|
||||
if (!templateInfoResults.next()) {
|
||||
addDownloadUrlCreatedToTemplateStorerefstatement.executeUpdate();
|
||||
addDownloadUrlToTemplateStorerefstatement.executeUpdate();
|
||||
}
|
||||
|
||||
// Add download_url_created to volume_store_ref - note download_url already exists
|
||||
pstmt = conn.prepareStatement("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'cloud' AND TABLE_NAME = 'volume_store_ref' AND COLUMN_NAME = 'download_url_created'");
|
||||
rs = pstmt.executeQuery();
|
||||
if (!rs.next()) {
|
||||
pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`volume_store_ref` ADD COLUMN `download_url_created` datetime");
|
||||
pstmt.executeUpdate();
|
||||
if (!volumeInfoResults.next()) {
|
||||
addDownloadUrlCreatedToVolumeStorerefstatement.executeUpdate();
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
@ -96,24 +93,12 @@ public class Upgrade430to440 implements DbUpgrade {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void secondaryIpsAccountAndDomainIdsUpdate(Connection conn) {
|
||||
PreparedStatement pstmt = null;
|
||||
PreparedStatement pstmtVm = null;
|
||||
PreparedStatement pstmtNw = null;
|
||||
PreparedStatement pstmtUpdate = null;
|
||||
|
||||
ResultSet rs1 = null;
|
||||
ResultSet vmRs = null;
|
||||
ResultSet networkRs = null;
|
||||
|
||||
String secondIpsSql = "SELECT id, vmId, network_id, account_id, domain_id, ip4_address FROM `cloud`.`nic_secondary_ips`";
|
||||
|
||||
try {
|
||||
pstmt = conn.prepareStatement(secondIpsSql);
|
||||
rs1 = pstmt.executeQuery();
|
||||
|
||||
try (PreparedStatement pstmt = conn.prepareStatement(secondIpsSql);
|
||||
ResultSet rs1 = pstmt.executeQuery();
|
||||
) {
|
||||
while(rs1.next()) {
|
||||
long ipId = rs1.getLong(1);
|
||||
long vmId = rs1.getLong(2);
|
||||
@ -122,130 +107,77 @@ public class Upgrade430to440 implements DbUpgrade {
|
||||
long domainId = rs1.getLong(5);
|
||||
String ipAddr = rs1.getString(6);
|
||||
|
||||
pstmtVm = conn.prepareStatement("SELECT account_id, domain_id FROM `cloud`.`vm_instance` where id = ?");
|
||||
pstmtVm.setLong(1,vmId);
|
||||
try(PreparedStatement pstmtVm = conn.prepareStatement("SELECT account_id, domain_id FROM `cloud`.`vm_instance` where id = ?");) {
|
||||
pstmtVm.setLong(1,vmId);
|
||||
|
||||
vmRs = pstmtVm.executeQuery();
|
||||
try(ResultSet vmRs = pstmtVm.executeQuery();) {
|
||||
|
||||
if (vmRs.next()) {
|
||||
long vmAccountId = vmRs.getLong(1);
|
||||
long vmDomainId = vmRs.getLong(2);
|
||||
if (vmRs.next()) {
|
||||
long vmAccountId = vmRs.getLong(1);
|
||||
long vmDomainId = vmRs.getLong(2);
|
||||
|
||||
if (vmAccountId != accountId && vmAccountId != domainId) {
|
||||
// update the secondary ip accountid and domainid to vm accountid domainid
|
||||
// check the network type. If network is shared accountid doaminid needs to be updated in
|
||||
// in both nic_secondary_ips table and user_ip_address table
|
||||
if (vmAccountId != accountId && vmAccountId != domainId) {
|
||||
// update the secondary ip accountid and domainid to vm accountid domainid
|
||||
// check the network type. If network is shared accountid doaminid needs to be updated in
|
||||
// in both nic_secondary_ips table and user_ip_address table
|
||||
|
||||
pstmtUpdate = conn.prepareStatement("UPDATE `cloud`.`nic_secondary_ips` SET account_id = ?, domain_id= ? WHERE id = ?");
|
||||
pstmtUpdate.setLong(1, vmAccountId);
|
||||
pstmtUpdate.setLong(2,vmDomainId);
|
||||
pstmtUpdate.setLong(3,ipId);
|
||||
pstmtUpdate.executeUpdate();
|
||||
pstmtUpdate.close();
|
||||
try(PreparedStatement pstmtUpdate = conn.prepareStatement("UPDATE `cloud`.`nic_secondary_ips` SET account_id = ?, domain_id= ? WHERE id = ?");) {
|
||||
pstmtUpdate.setLong(1, vmAccountId);
|
||||
pstmtUpdate.setLong(2,vmDomainId);
|
||||
pstmtUpdate.setLong(3,ipId);
|
||||
pstmtUpdate.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
throw new CloudRuntimeException("Exception while updating secondary ip for nic " + ipId, e);
|
||||
}
|
||||
|
||||
pstmtNw = conn.prepareStatement("SELECT guest_type FROM `cloud`.`networks` where id = ?");
|
||||
pstmtNw.setLong(1,networkId);
|
||||
try(PreparedStatement pstmtNw = conn.prepareStatement("SELECT guest_type FROM `cloud`.`networks` where id = ?");) {
|
||||
pstmtNw.setLong(1,networkId);
|
||||
|
||||
networkRs = pstmtNw.executeQuery();
|
||||
if (networkRs.next()) {
|
||||
String guesttype = networkRs.getString(1);
|
||||
try(ResultSet networkRs = pstmtNw.executeQuery();) {
|
||||
if (networkRs.next()) {
|
||||
String guesttype = networkRs.getString(1);
|
||||
|
||||
if (guesttype.equals(Network.GuestType.Shared.toString())) {
|
||||
pstmtUpdate = conn.prepareStatement("UPDATE `cloud`.`user_ip_address` SET account_id = ?, domain_id= ? WHERE public_ip_address = ?");
|
||||
pstmtUpdate.setLong(1,vmAccountId);
|
||||
pstmtUpdate.setLong(2,vmDomainId);
|
||||
pstmtUpdate.setString(3,ipAddr);
|
||||
pstmtUpdate.executeUpdate();
|
||||
pstmtUpdate.close();
|
||||
if (guesttype.equals(Network.GuestType.Shared.toString())) {
|
||||
try(PreparedStatement pstmtUpdate = conn.prepareStatement("UPDATE `cloud`.`user_ip_address` SET account_id = ?, domain_id= ? WHERE public_ip_address = ?");) {
|
||||
pstmtUpdate.setLong(1,vmAccountId);
|
||||
pstmtUpdate.setLong(2,vmDomainId);
|
||||
pstmtUpdate.setString(3,ipAddr);
|
||||
pstmtUpdate.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
throw new CloudRuntimeException("Exception while updating public ip " + ipAddr, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new CloudRuntimeException("Exception while retrieving guest type for network " + networkId, e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
networkRs.close();
|
||||
networkRs = null;
|
||||
pstmtNw.close();
|
||||
pstmtNw = null;
|
||||
} catch (SQLException e) {
|
||||
throw new CloudRuntimeException("Exception while retrieving guest type for network " + networkId, e);
|
||||
}
|
||||
} // if
|
||||
} // if
|
||||
}
|
||||
} //if
|
||||
|
||||
pstmtVm.close();
|
||||
pstmtVm = null;
|
||||
vmRs.close();
|
||||
vmRs = null;
|
||||
}
|
||||
} // while
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new CloudRuntimeException("Exception while Moving private zone information to dedicated resources", e);
|
||||
} finally {
|
||||
|
||||
if (pstmt != null) {
|
||||
try {
|
||||
pstmt.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (rs1 != null) {
|
||||
try {
|
||||
rs1.close();
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (pstmtVm != null) {
|
||||
try {
|
||||
pstmtVm.close();
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
|
||||
if (vmRs != null) {
|
||||
try {
|
||||
vmRs.close();
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (pstmtNw != null) {
|
||||
try {
|
||||
pstmtNw.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (networkRs != null) {
|
||||
try {
|
||||
networkRs.close();
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
s_logger.debug("Done updating vm nic secondary ip account and domain ids");
|
||||
}
|
||||
|
||||
|
||||
private void moveCidrsToTheirOwnTable(Connection conn) {
|
||||
PreparedStatement pstmtItem = null;
|
||||
PreparedStatement pstmtCidr = null;
|
||||
ResultSet rsItems = null;
|
||||
s_logger.debug("Moving network acl item cidrs to a row per cidr");
|
||||
|
||||
String networkAclItemSql = "SELECT id, cidr FROM `cloud`.`network_acl_item`";
|
||||
String networkAclItemCidrSql = "INSERT INTO `cloud`.`network_acl_item_cidrs` (network_acl_item_id, cidr) VALUES (?,?)";
|
||||
|
||||
s_logger.debug("Moving network acl item cidrs to a row per cidr");
|
||||
try {
|
||||
pstmtItem = conn.prepareStatement(networkAclItemSql);
|
||||
rsItems = pstmtItem.executeQuery();
|
||||
try (PreparedStatement pstmtItem = conn.prepareStatement(networkAclItemSql);
|
||||
ResultSet rsItems = pstmtItem.executeQuery();
|
||||
PreparedStatement pstmtCidr = conn.prepareStatement(networkAclItemCidrSql);
|
||||
) {
|
||||
|
||||
String networkAclItemCidrSql = "INSERT INTO `cloud`.`network_acl_item_cidrs` (network_acl_item_id, cidr) VALUES (?,?)";
|
||||
pstmtCidr = conn.prepareStatement(networkAclItemCidrSql);
|
||||
|
||||
// for each network acl item
|
||||
while(rsItems.next()) {
|
||||
@ -264,58 +196,33 @@ public class Upgrade430to440 implements DbUpgrade {
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new CloudRuntimeException("Exception while Moving network acl item cidrs to a row per cidr", e);
|
||||
} finally {
|
||||
|
||||
if (pstmtItem != null) {
|
||||
try {
|
||||
pstmtItem.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
if (pstmtCidr != null) {
|
||||
try {
|
||||
pstmtCidr.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
s_logger.debug("Done moving network acl item cidrs to a row per cidr");
|
||||
}
|
||||
|
||||
private void updateVlanUris(Connection conn) {
|
||||
s_logger.debug("updating vlan URIs");
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
pstmt = conn.prepareStatement("SELECT id, vlan_id FROM `cloud`.`vlan` where vlan_id not like '%:%'");
|
||||
rs = pstmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
long id = rs.getLong(1);
|
||||
String vlan = rs.getString(2);
|
||||
try(PreparedStatement selectstatement = conn.prepareStatement("SELECT id, vlan_id FROM `cloud`.`vlan` where vlan_id not like '%:%'");
|
||||
ResultSet results = selectstatement.executeQuery()) {
|
||||
|
||||
while (results.next()) {
|
||||
long id = results.getLong(1);
|
||||
String vlan = results.getString(2);
|
||||
if (vlan == null || "".equals(vlan)) {
|
||||
continue;
|
||||
}
|
||||
String vlanUri = BroadcastDomainType.Vlan.toUri(vlan).toString();
|
||||
pstmt = conn.prepareStatement("update `cloud`.`vlan` set vlan_id=? where id=?");
|
||||
pstmt.setString(1, vlanUri);
|
||||
pstmt.setLong(2, id);
|
||||
pstmt.executeUpdate();
|
||||
try(PreparedStatement updatestatement = conn.prepareStatement("update `cloud`.`vlan` set vlan_id=? where id=?");)
|
||||
{
|
||||
updatestatement.setString(1, vlanUri);
|
||||
updatestatement.setLong(2, id);
|
||||
updatestatement.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
throw new CloudRuntimeException("Unable to update vlan URI " + vlanUri + " for vlan record " + id, e);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new CloudRuntimeException("Unable to update vlan URIs ", e);
|
||||
} finally {
|
||||
try {
|
||||
if (rs != null) {
|
||||
rs.close();
|
||||
}
|
||||
|
||||
if (pstmt != null) {
|
||||
pstmt.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
s_logger.debug("Done updateing vlan URIs");
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user