Clean up template_zone_ref entries in case of create template failure.

This commit is contained in:
Min Chen 2013-05-21 12:00:59 -07:00
parent 8f549db518
commit 2af36aa87b
4 changed files with 35 additions and 18 deletions

View File

@ -30,4 +30,6 @@ public interface VMTemplateZoneDao extends GenericDao<VMTemplateZoneVO, Long> {
public List<VMTemplateZoneVO> listByZoneTemplate(Long zoneId, long templateId);
public void deletePrimaryRecordsForTemplate(long templateId);
}

View File

@ -20,6 +20,7 @@ import java.util.List;
import javax.ejb.Local;
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
@ -27,6 +28,7 @@ import com.cloud.storage.VMTemplateZoneVO;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.Transaction;
@Component
@Local(value={VMTemplateZoneDao.class})
@ -86,4 +88,17 @@ public class VMTemplateZoneDaoImpl extends GenericDaoBase<VMTemplateZoneVO, Long
return listBy(sc);
}
@Override
public void deletePrimaryRecordsForTemplate(long templateId) {
SearchCriteria<VMTemplateZoneVO> sc = TemplateSearch.create();
sc.setParameters("template_id", templateId);
Transaction txn = Transaction.currentTxn();
txn.start();
remove(sc);
txn.commit();
}
}

View File

@ -155,7 +155,7 @@ public class StorageCacheManagerImpl implements StorageCacheManager, Manager {
result = future.get();
if (result.isFailed()) {
cacheStore.delete(data);
objOnCacheStore.processEvent(Event.OperationFailed);
} else {
objOnCacheStore.processEvent(Event.OperationSuccessed, result.getAnswer());
return objOnCacheStore;
@ -168,7 +168,7 @@ public class StorageCacheManagerImpl implements StorageCacheManager, Manager {
s_logger.debug("create cache storage failed: " + e.toString());
} finally {
if (result == null) {
cacheStore.delete(data);
objOnCacheStore.processEvent(Event.OperationFailed);
}
}
@ -191,8 +191,7 @@ public class StorageCacheManagerImpl implements StorageCacheManager, Manager {
}
@Override
public DataObject deleteCacheObject(DataObject data) {
// TODO Auto-generated method stub
return null;
public boolean deleteCacheObject(DataObject data) {
return objectInStoreMgr.delete(data);
}
}

View File

@ -1456,8 +1456,9 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
if (privateTemplate == null) {
Transaction txn = Transaction.currentTxn();
txn.start();
// Remove the template_store_ref record first, otherwise, we cannot remove the template record due to FK constraints
this._tmplStoreDao.deletePrimaryRecordsForTemplate(templateId);
// template_store_ref entries should have been removed using our DataObject.processEvent command in case of failure.
// Remove the template_zone_ref record
this._tmpltZoneDao.deletePrimaryRecordsForTemplate(templateId);
// Remove the template record
this._tmpltDao.expunge(templateId);