Skip to content

Commit ade82be

Browse files
sedukullDaanHoogland
authored andcommitted
Fixed few resource leak issues
Signed-off-by: Daan Hoogland <daan@onecht.net> (cherry picked from commit 3c5f64c)
1 parent 1369a0d commit ade82be

1 file changed

Lines changed: 106 additions & 58 deletions

File tree

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

Lines changed: 106 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -356,91 +356,139 @@ public void saveDetails(UserVmVO vm) {
356356
@Override
357357
public List<Long> listPodIdsHavingVmsforAccount(long zoneId, long accountId) {
358358
TransactionLegacy txn = TransactionLegacy.currentTxn();
359-
PreparedStatement pstmt = null;
360359
List<Long> result = new ArrayList<Long>();
360+
String sql = LIST_PODS_HAVING_VMS_FOR_ACCOUNT;
361361

362-
try {
363-
String sql = LIST_PODS_HAVING_VMS_FOR_ACCOUNT;
364-
pstmt = txn.prepareAutoCloseStatement(sql);
362+
try(PreparedStatement pstmt = txn.prepareStatement(sql)) {
365363
pstmt.setLong(1, zoneId);
366364
pstmt.setLong(2, accountId);
367-
368-
ResultSet rs = pstmt.executeQuery();
369-
while (rs.next()) {
370-
result.add(rs.getLong(1));
365+
try(ResultSet rs = pstmt.executeQuery();)
366+
{
367+
while (rs.next()) {
368+
result.add(rs.getLong(1));
369+
}
371370
}
371+
catch (Exception e) {
372+
s_logger.error("listPodIdsHavingVmsforAccount:Exception: " + e.getMessage());
373+
throw new CloudRuntimeException("listPodIdsHavingVmsforAccount:Exception: " + e.getMessage(), e);
374+
}
375+
txn.commit();
372376
return result;
373-
} catch (SQLException e) {
374-
throw new CloudRuntimeException("DB Exception on: " + LIST_PODS_HAVING_VMS_FOR_ACCOUNT, e);
375-
} catch (Throwable e) {
376-
throw new CloudRuntimeException("Caught: " + LIST_PODS_HAVING_VMS_FOR_ACCOUNT, e);
377+
} catch (Exception e) {
378+
s_logger.error("listPodIdsHavingVmsforAccount:Exception : " + e.getMessage());
379+
throw new CloudRuntimeException("listPodIdsHavingVmsforAccount:Exception: " + e.getMessage(), e);
380+
}
381+
finally {
382+
try{
383+
if (txn != null)
384+
{
385+
txn.close();
386+
}
387+
}
388+
catch (Exception e)
389+
{
390+
s_logger.error("listVmDetails:Exception:" + e.getMessage());
391+
}
377392
}
393+
378394
}
379395

380396
@Override
381397
public Hashtable<Long, UserVmData> listVmDetails(Hashtable<Long, UserVmData> userVmDataHash) {
382398
TransactionLegacy txn = TransactionLegacy.currentTxn();
383-
PreparedStatement pstmt = null;
384-
385399
try {
386400
int curr_index = 0;
387-
388401
List<UserVmData> userVmDataList = new ArrayList(userVmDataHash.values());
389-
390-
if (userVmDataList.size() > VM_DETAILS_BATCH_SIZE) {
391-
pstmt = txn.prepareStatement(VM_DETAILS + getQueryBatchAppender(VM_DETAILS_BATCH_SIZE));
392-
while ((curr_index + VM_DETAILS_BATCH_SIZE) <= userVmDataList.size()) {
393-
// set the vars value
394-
for (int k = 1, j = curr_index; j < curr_index + VM_DETAILS_BATCH_SIZE; j++, k++) {
395-
pstmt.setLong(k, userVmDataList.get(j).getId());
396-
}
397-
ResultSet rs = pstmt.executeQuery();
398-
while (rs.next()) {
399-
long vm_id = rs.getLong("vm_instance.id");
400-
//check if the entry is already there
401-
UserVmData uvm = userVmDataHash.get(vm_id);
402-
if (uvm == null) {
403-
uvm = new UserVmData();
404-
uvm.setId(vm_id);
402+
if (userVmDataList.size() > VM_DETAILS_BATCH_SIZE)
403+
{
404+
try (PreparedStatement pstmt = txn.prepareStatement(VM_DETAILS + getQueryBatchAppender(VM_DETAILS_BATCH_SIZE));)
405+
{
406+
while ((curr_index + VM_DETAILS_BATCH_SIZE) <= userVmDataList.size()) {
407+
// set the vars value
408+
for (int k = 1, j = curr_index; j < curr_index + VM_DETAILS_BATCH_SIZE; j++, k++) {
409+
pstmt.setLong(k, userVmDataList.get(j).getId());
410+
}
411+
try(ResultSet rs = pstmt.executeQuery();)
412+
{
413+
while (rs.next()) {
414+
long vm_id = rs.getLong("vm_instance.id");
415+
//check if the entry is already there
416+
UserVmData uvm = userVmDataHash.get(vm_id);
417+
if (uvm == null) {
418+
uvm = new UserVmData();
419+
uvm.setId(vm_id);
420+
}
421+
// initialize the data with this row
422+
setUserVmData(uvm, rs);
423+
}
405424
}
406-
// initialize the data with this row
407-
setUserVmData(uvm, rs);
425+
catch (Exception e)
426+
{
427+
s_logger.error("listVmDetails:Exception:" + e.getMessage());
428+
throw new CloudRuntimeException("listVmDetails: Exception:" + e.getMessage(),e);
429+
}
430+
curr_index += VM_DETAILS_BATCH_SIZE;
408431
}
409-
rs.close();
410-
curr_index += VM_DETAILS_BATCH_SIZE;
432+
}
433+
catch (Exception e)
434+
{
435+
s_logger.error("listVmDetails:Exception:" + e.getMessage());
436+
throw new CloudRuntimeException("listVmDetails: Exception:" + e.getMessage(),e);
411437
}
412438
}
413439

414440
if (curr_index < userVmDataList.size()) {
415441
int batch_size = (userVmDataList.size() - curr_index);
416-
pstmt = txn.prepareStatement(VM_DETAILS + getQueryBatchAppender(batch_size));
417-
// set the vars value
418-
for (int k = 1, j = curr_index; j < curr_index + batch_size; j++, k++) {
419-
pstmt.setLong(k, userVmDataList.get(j).getId());
420-
}
421-
ResultSet rs = pstmt.executeQuery();
422-
while (rs.next()) {
423-
long vm_id = rs.getLong("vm_instance.id");
424-
//check if the entry is already there
425-
UserVmData uvm = userVmDataHash.get(vm_id);
426-
if (uvm == null) {
427-
uvm = new UserVmData();
428-
uvm.setId(vm_id);
442+
try (PreparedStatement vm_details_pstmt = txn.prepareStatement(VM_DETAILS + getQueryBatchAppender(batch_size)))
443+
{
444+
// set the vars value
445+
for (int k = 1, j = curr_index; j < curr_index + batch_size; j++, k++) {
446+
vm_details_pstmt.setLong(k, userVmDataList.get(j).getId());
447+
}
448+
try(ResultSet rs = vm_details_pstmt.executeQuery();) {
449+
while (rs.next()) {
450+
long vm_id = rs.getLong("vm_instance.id");
451+
//check if the entry is already there
452+
UserVmData uvm = userVmDataHash.get(vm_id);
453+
if (uvm == null) {
454+
uvm = new UserVmData();
455+
uvm.setId(vm_id);
456+
}
457+
// initialize the data with this row
458+
setUserVmData(uvm, rs);
459+
}
460+
}
461+
catch (Exception e)
462+
{
463+
s_logger.error("listVmDetails: Exception:" + e.getMessage());
464+
throw new CloudRuntimeException("listVmDetails: Exception:" + e.getMessage(),e);
429465
}
430-
// initialize the data with this row
431-
setUserVmData(uvm, rs);
432466
}
433-
rs.close();
467+
catch (Exception e)
468+
{
469+
s_logger.error("listVmDetails:Exception:" + e.getMessage());
470+
throw new CloudRuntimeException("listVmDetails: Exception:" + e.getMessage(),e);
471+
}
434472
}
435-
436-
if (pstmt != null)
437-
pstmt.close();
473+
txn.commit();
438474
return userVmDataHash;
439-
} catch (SQLException e) {
440-
throw new CloudRuntimeException("DB Exception on: " + VM_DETAILS, e);
441-
} catch (Throwable e) {
442-
throw new CloudRuntimeException("Caught: " + VM_DETAILS, e);
475+
} catch (Exception e) {
476+
s_logger.error("listVmDetails:Exception:" + e.getMessage());
477+
throw new CloudRuntimeException("listVmDetails:Exception : ", e);
443478
}
479+
finally {
480+
try{
481+
if (txn != null)
482+
{
483+
txn.close();
484+
}
485+
}
486+
catch (Exception e)
487+
{
488+
s_logger.error("listVmDetails:Exception:" + e.getMessage());
489+
}
490+
}
491+
444492
}
445493

446494
public static UserVmData setUserVmData(UserVmData userVmData, ResultSet rs) throws SQLException {

0 commit comments

Comments
 (0)