@Override @DB public boolean delete(TemplateProfile profile) { VMTemplateVO template = profile.getTemplate(); Long templateId = template.getId(); boolean success = true; String zoneName; if (!template.isCrossZones() && profile.getZoneId() != null) { zoneName = profile.getZoneId().toString(); } else { zoneName = "all zones"; } s_logger.debug( "Attempting to mark template host refs for template: " + template.getName() + " as destroyed in zone: " + zoneName); Account account = _accountDao.findByIdIncludingRemoved(template.getAccountId()); String eventType = EventTypes.EVENT_TEMPLATE_DELETE; List<TemplateDataStoreVO> templateHostVOs = this._tmpltStoreDao.listByTemplate(templateId); for (TemplateDataStoreVO vo : templateHostVOs) { TemplateDataStoreVO lock = null; try { lock = _tmpltStoreDao.acquireInLockTable(vo.getId()); if (lock == null) { s_logger.debug( "Failed to acquire lock when deleting templateDataStoreVO with ID: " + vo.getId()); success = false; break; } vo.setDestroyed(true); _tmpltStoreDao.update(vo.getId(), vo); } finally { if (lock != null) { _tmpltStoreDao.releaseFromLockTable(lock.getId()); } } } if (profile.getZoneId() != null) { UsageEventVO usageEvent = new UsageEventVO(eventType, account.getId(), profile.getZoneId(), templateId, null); _usageEventDao.persist(usageEvent); } else { List<DataCenterVO> dcs = _dcDao.listAllIncludingRemoved(); for (DataCenterVO dc : dcs) { UsageEventVO usageEvent = new UsageEventVO(eventType, account.getId(), dc.getId(), templateId, null); _usageEventDao.persist(usageEvent); } } VMTemplateZoneVO templateZone = _tmpltZoneDao.findByZoneTemplate(profile.getZoneId(), templateId); if (templateZone != null) { _tmpltZoneDao.remove(templateZone.getId()); } s_logger.debug( "Successfully marked template host refs for template: " + template.getName() + " as destroyed in zone: " + zoneName); // If there are no more non-destroyed template host entries for this template, delete it if (success && (_tmpltStoreDao.listByTemplate(templateId).size() == 0)) { long accountId = template.getAccountId(); VMTemplateVO lock = _tmpltDao.acquireInLockTable(templateId); try { if (lock == null) { s_logger.debug("Failed to acquire lock when deleting template with ID: " + templateId); success = false; } else if (_tmpltDao.remove(templateId)) { // Decrement the number of templates and total secondary storage space used by the // account. _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template); _resourceLimitMgr.recalculateResourceCount( accountId, template.getDomainId(), ResourceType.secondary_storage.getOrdinal()); } } finally { if (lock != null) { _tmpltDao.releaseFromLockTable(lock.getId()); } } s_logger.debug( "Removed template: " + template.getName() + " because all of its template host refs were marked as destroyed."); } return success; }