Skip to content

Commit 0c28f36

Browse files
committed
Fix a number of resource leaks reported by Coverity
1 parent 515ef0d commit 0c28f36

5 files changed

Lines changed: 129 additions & 170 deletions

File tree

engine/schema/src/com/cloud/vm/dao/UserVmDaoImpl.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,28 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
7979
ResourceTagDao _tagsDao;
8080

8181
private static final String LIST_PODS_HAVING_VMS_FOR_ACCOUNT =
82-
"SELECT pod_id FROM cloud.vm_instance WHERE data_center_id = ? AND account_id = ? AND pod_id IS NOT NULL AND (state = 'Running' OR state = 'Stopped') "
83-
+ "GROUP BY pod_id HAVING count(id) > 0 ORDER BY count(id) DESC";
82+
"SELECT pod_id FROM cloud.vm_instance WHERE data_center_id = ? AND account_id = ? AND pod_id IS NOT NULL AND (state = 'Running' OR state = 'Stopped') "
83+
+ "GROUP BY pod_id HAVING count(id) > 0 ORDER BY count(id) DESC";
8484

8585
private static final String VM_DETAILS = "select vm_instance.id, "
86-
+ "account.id, account.account_name, account.type, domain.name, instance_group.id, instance_group.name,"
87-
+ "data_center.id, data_center.name, data_center.is_security_group_enabled, host.id, host.name, "
88-
+ "vm_template.id, vm_template.name, vm_template.display_text, iso.id, iso.name, "
89-
+ "vm_template.enable_password, service_offering.id, disk_offering.name, storage_pool.id, storage_pool.pool_type, "
90-
+ "service_offering.cpu, service_offering.speed, service_offering.ram_size, volumes.id, volumes.device_id, volumes.volume_type, security_group.id, security_group.name, "
91-
+ "security_group.description, nics.id, nics.ip4_address, nics.default_nic, nics.gateway, nics.network_id, nics.netmask, nics.mac_address, nics.broadcast_uri, " +
92-
"nics.isolation_uri, "
93-
+ "networks.traffic_type, networks.guest_type, user_ip_address.id, user_ip_address.public_ip_address from vm_instance "
94-
+ "left join account on vm_instance.account_id=account.id " + "left join domain on vm_instance.domain_id=domain.id "
95-
+ "left join instance_group_vm_map on vm_instance.id=instance_group_vm_map.instance_id "
96-
+ "left join instance_group on instance_group_vm_map.group_id=instance_group.id " + "left join data_center on vm_instance.data_center_id=data_center.id "
97-
+ "left join host on vm_instance.host_id=host.id " + "left join vm_template on vm_instance.vm_template_id=vm_template.id "
98-
+ "left join user_vm on vm_instance.id=user_vm.id " + "left join vm_template iso on iso.id=user_vm.iso_id "
99-
+ "left join service_offering on vm_instance.service_offering_id=service_offering.id "
100-
+ "left join disk_offering on vm_instance.service_offering_id=disk_offering.id " + "left join volumes on vm_instance.id=volumes.instance_id "
101-
+ "left join storage_pool on volumes.pool_id=storage_pool.id " + "left join security_group_vm_map on vm_instance.id=security_group_vm_map.instance_id "
102-
+ "left join security_group on security_group_vm_map.security_group_id=security_group.id " + "left join nics on vm_instance.id=nics.instance_id "
103-
+ "left join networks on nics.network_id=networks.id " + "left join user_ip_address on user_ip_address.vm_id=vm_instance.id " + "where vm_instance.id in (";
86+
+ "account.id, account.account_name, account.type, domain.name, instance_group.id, instance_group.name,"
87+
+ "data_center.id, data_center.name, data_center.is_security_group_enabled, host.id, host.name, "
88+
+ "vm_template.id, vm_template.name, vm_template.display_text, iso.id, iso.name, "
89+
+ "vm_template.enable_password, service_offering.id, disk_offering.name, storage_pool.id, storage_pool.pool_type, "
90+
+ "service_offering.cpu, service_offering.speed, service_offering.ram_size, volumes.id, volumes.device_id, volumes.volume_type, security_group.id, security_group.name, "
91+
+ "security_group.description, nics.id, nics.ip4_address, nics.default_nic, nics.gateway, nics.network_id, nics.netmask, nics.mac_address, nics.broadcast_uri, " +
92+
"nics.isolation_uri, "
93+
+ "networks.traffic_type, networks.guest_type, user_ip_address.id, user_ip_address.public_ip_address from vm_instance "
94+
+ "left join account on vm_instance.account_id=account.id " + "left join domain on vm_instance.domain_id=domain.id "
95+
+ "left join instance_group_vm_map on vm_instance.id=instance_group_vm_map.instance_id "
96+
+ "left join instance_group on instance_group_vm_map.group_id=instance_group.id " + "left join data_center on vm_instance.data_center_id=data_center.id "
97+
+ "left join host on vm_instance.host_id=host.id " + "left join vm_template on vm_instance.vm_template_id=vm_template.id "
98+
+ "left join user_vm on vm_instance.id=user_vm.id " + "left join vm_template iso on iso.id=user_vm.iso_id "
99+
+ "left join service_offering on vm_instance.service_offering_id=service_offering.id "
100+
+ "left join disk_offering on vm_instance.service_offering_id=disk_offering.id " + "left join volumes on vm_instance.id=volumes.instance_id "
101+
+ "left join storage_pool on volumes.pool_id=storage_pool.id " + "left join security_group_vm_map on vm_instance.id=security_group_vm_map.instance_id "
102+
+ "left join security_group on security_group_vm_map.security_group_id=security_group.id " + "left join nics on vm_instance.id=nics.instance_id "
103+
+ "left join networks on nics.network_id=networks.id " + "left join user_ip_address on user_ip_address.vm_id=vm_instance.id " + "where vm_instance.id in (";
104104

105105
private static final int VM_DETAILS_BATCH_SIZE = 100;
106106

@@ -186,7 +186,7 @@ void init() {
186186
AccountDataCenterVirtualSearch.and("account", AccountDataCenterVirtualSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
187187
AccountDataCenterVirtualSearch.and("dc", AccountDataCenterVirtualSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
188188
AccountDataCenterVirtualSearch.join("nicSearch", nicSearch, AccountDataCenterVirtualSearch.entity().getId(), nicSearch.entity().getInstanceId(),
189-
JoinBuilder.JoinType.INNER);
189+
JoinBuilder.JoinType.INNER);
190190
AccountDataCenterVirtualSearch.done();
191191

192192
UserVmByIsoSearch = createSearchBuilder();
@@ -217,7 +217,7 @@ public List<UserVmVO> listByAccountAndDataCenter(long accountId, long dcId) {
217217

218218
@Override
219219
public void updateVM(long id, String displayName, boolean enable, Long osTypeId, String userData, boolean displayVm,
220-
boolean isDynamicallyScalable, String customId, String hostName, String instanceName) {
220+
boolean isDynamicallyScalable, String customId, String hostName, String instanceName) {
221221
UserVmVO vo = createForUpdate();
222222
vo.setDisplayName(displayName);
223223
vo.setHaEnabled(enable);
@@ -413,6 +413,8 @@ public Hashtable<Long, UserVmData> listVmDetails(Hashtable<Long, UserVmData> use
413413
rs.close();
414414
curr_index += VM_DETAILS_BATCH_SIZE;
415415
}
416+
if (pstmt != null)
417+
pstmt.close();
416418
}
417419

418420
if (curr_index < userVmDataList.size()) {

framework/db/src/com/cloud/utils/db/Merovingian2.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
4040
private static final Logger s_logger = Logger.getLogger(Merovingian2.class);
4141

4242
private static final String ACQUIRE_SQL =
43-
"INSERT INTO op_lock (op_lock.key, op_lock.mac, op_lock.ip, op_lock.thread, op_lock.acquired_on, waiters) VALUES (?, ?, ?, ?, ?, 1)";
43+
"INSERT INTO op_lock (op_lock.key, op_lock.mac, op_lock.ip, op_lock.thread, op_lock.acquired_on, waiters) VALUES (?, ?, ?, ?, ?, 1)";
4444
private static final String INCREMENT_SQL = "UPDATE op_lock SET waiters=waiters+1 where op_lock.key=? AND op_lock.mac=? AND op_lock.ip=? AND op_lock.thread=?";
4545
private static final String SELECT_SQL = "SELECT op_lock.key, mac, ip, thread, acquired_on, waiters FROM op_lock";
4646
private static final String INQUIRE_SQL = SELECT_SQL + " WHERE op_lock.key=?";
@@ -428,7 +428,7 @@ public void cleanupThread() {
428428
pstmt.setInt(3, threadId);
429429
int rows = pstmt.executeUpdate();
430430
assert (false) : "Abandon hope, all ye who enter here....There were still " + rows + ":" + c +
431-
" locks not released when the transaction ended, check for lock not released or @DB is not added to the code that using the locks!";
431+
" locks not released when the transaction ended, check for lock not released or @DB is not added to the code that using the locks!";
432432
} catch (SQLException e) {
433433
throw new CloudRuntimeException("Can't clear locks " + pstmt, e);
434434
} finally {
@@ -444,9 +444,7 @@ public void cleanupThread() {
444444
@Override
445445
public boolean releaseLockAsLastResortAndIReallyKnowWhatIAmDoing(String key) {
446446
s_logger.info("Releasing a lock from JMX lck-" + key);
447-
PreparedStatement pstmt = null;
448-
try {
449-
pstmt = _concierge.conn().prepareStatement(RELEASE_LOCK_SQL);
447+
try (PreparedStatement pstmt = _concierge.conn().prepareStatement(RELEASE_LOCK_SQL)) {
450448
pstmt.setString(1, key);
451449
int rows = pstmt.executeUpdate();
452450
return rows > 0;

0 commit comments

Comments
 (0)