Program: 89809
Project: 89809
Buggy source:
@Override @DB public UserVm recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationException, CloudRuntimeException {
  Long vmId=cmd.getId();
  Account accountHandle=UserContext.current().getCaller();
  if (accountHandle != null && accountHandle.getRemoved() != null) {
    throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + accountHandle.getId() + "String_Node_Str");
  }
  UserVmVO vm=_vmDao.findById(vmId.longValue());
  if (vm == null) {
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + vmId);
  }
  if ((accountHandle != null) && !_domainDao.isChildDomain(accountHandle.getDomainId(),vm.getDomainId())) {
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + vmId + "String_Node_Str");
  }
  if (vm.getRemoved() != null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + vmId);
    }
    throw new InvalidParameterValueException("String_Node_Str" + vmId);
  }
  if (vm.getState() != State.Destroyed) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + vmId);
    }
    throw new InvalidParameterValueException("String_Node_Str" + vmId + "String_Node_Str");
  }
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + vmId);
  }
  Transaction txn=Transaction.currentTxn();
  AccountVO account=null;
  txn.start();
  account=_accountDao.lockRow(vm.getAccountId(),true);
  if (account.getRemoved() != null) {
    throw new CloudRuntimeException("String_Node_Str");
  }
  if (_accountMgr.resourceLimitExceeded(account,ResourceType.user_vm)) {
    ResourceAllocationException rae=new ResourceAllocationException("String_Node_Str" + account.getAccountName() + "String_Node_Str");
    rae.setResourceType("String_Node_Str");
    txn.commit();
    throw rae;
  }
  _haMgr.cancelDestroy(vm,vm.getHostId());
  _accountMgr.incrementResourceCount(account.getId(),ResourceType.user_vm);
  if (!_itMgr.stateTransitTo(vm,VirtualMachine.Event.RecoveryRequested,null)) {
    s_logger.debug("String_Node_Str" + vmId);
    throw new InvalidParameterValueException("String_Node_Str" + vmId);
  }
  List<VolumeVO> volumes=_volsDao.findByInstanceIdDestroyed(vmId);
  for (  VolumeVO volume : volumes) {
    Long templateId=volume.getTemplateId();
    Long diskOfferingId=volume.getDiskOfferingId();
    Long offeringId=null;
    if (diskOfferingId != null) {
      DiskOfferingVO offering=_diskOfferingDao.findById(diskOfferingId);
      if (offering != null && (offering.getType() == DiskOfferingVO.Type.Disk)) {
        offeringId=offering.getId();
      }
    }
    UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_VOLUME_CREATE,volume.getAccountId(),volume.getDataCenterId(),volume.getId(),volume.getName(),offeringId,templateId,volume.getSize());
    _usageEventDao.persist(usageEvent);
  }
  _accountMgr.incrementResourceCount(account.getId(),ResourceType.volume,new Long(volumes.size()));
  UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_VM_CREATE,vm.getAccountId(),vm.getDataCenterId(),vm.getId(),vm.getName(),vm.getServiceOfferingId(),vm.getTemplateId(),null);
  _usageEventDao.persist(usageEvent);
  txn.commit();
  return _vmDao.findById(vmId);
}

Fixed version (ground truth):
@Override @DB public UserVm recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationException, CloudRuntimeException {
  Long vmId=cmd.getId();
  Account accountHandle=UserContext.current().getCaller();
  if (accountHandle != null && accountHandle.getRemoved() != null) {
    throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + accountHandle.getId() + "String_Node_Str");
  }
  UserVmVO vm=_vmDao.findById(vmId.longValue());
  if (vm == null) {
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + vmId);
  }
  if ((accountHandle != null) && !_domainDao.isChildDomain(accountHandle.getDomainId(),vm.getDomainId())) {
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + vmId + "String_Node_Str");
  }
  if (vm.getRemoved() != null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + vmId);
    }
    throw new InvalidParameterValueException("String_Node_Str" + vmId);
  }
  if (vm.getState() != State.Destroyed) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + vmId);
    }
    throw new InvalidParameterValueException("String_Node_Str" + vmId + "String_Node_Str");
  }
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + vmId);
  }
  Transaction txn=Transaction.currentTxn();
  AccountVO account=null;
  txn.start();
  account=_accountDao.lockRow(vm.getAccountId(),true);
  if (account.getRemoved() != null) {
    throw new CloudRuntimeException("String_Node_Str");
  }
  if (_accountMgr.resourceLimitExceeded(account,ResourceType.user_vm)) {
    ResourceAllocationException rae=new ResourceAllocationException("String_Node_Str" + account.getAccountName() + "String_Node_Str");
    rae.setResourceType("String_Node_Str");
    txn.commit();
    throw rae;
  }
  _haMgr.cancelDestroy(vm,vm.getHostId());
  _accountMgr.incrementResourceCount(account.getId(),ResourceType.user_vm);
  if (!_itMgr.stateTransitTo(vm,VirtualMachine.Event.RecoveryRequested,null)) {
    s_logger.debug("String_Node_Str" + vmId);
    throw new InvalidParameterValueException("String_Node_Str" + vmId);
  }
  List<VolumeVO> volumes=_volsDao.findByInstance(vmId);
  for (  VolumeVO volume : volumes) {
    if (volume.getVolumeType().equals(VolumeType.ROOT)) {
      Long templateId=volume.getTemplateId();
      Long diskOfferingId=volume.getDiskOfferingId();
      Long offeringId=null;
      if (diskOfferingId != null) {
        DiskOfferingVO offering=_diskOfferingDao.findById(diskOfferingId);
        if (offering != null && (offering.getType() == DiskOfferingVO.Type.Disk)) {
          offeringId=offering.getId();
        }
      }
      UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_VOLUME_CREATE,volume.getAccountId(),volume.getDataCenterId(),volume.getId(),volume.getName(),offeringId,templateId,volume.getSize());
      _usageEventDao.persist(usageEvent);
    }
  }
  _accountMgr.incrementResourceCount(account.getId(),ResourceType.volume,new Long(volumes.size()));
  UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_VM_CREATE,vm.getAccountId(),vm.getDataCenterId(),vm.getId(),vm.getName(),vm.getServiceOfferingId(),vm.getTemplateId(),null);
  _usageEventDao.persist(usageEvent);
  txn.commit();
  return _vmDao.findById(vmId);
}