Skip to content

Commit 565bdfb

Browse files
committed
Fix CLOUDSTACK-2525: clean up db entries in vm_template and
template_store_ref in case of template creation failure.
1 parent ef541a2 commit 565bdfb

3 files changed

Lines changed: 13 additions & 0 deletions

File tree

engine/api/src/org/apache/cloudstack/storage/datastore/db/TemplateDataStoreDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public interface TemplateDataStoreDao extends GenericDao<TemplateDataStoreVO, Lo
3737

3838
public void deletePrimaryRecordsForStore(long id);
3939

40+
public void deletePrimaryRecordsForTemplate(long templateId);
41+
4042
List<TemplateDataStoreVO> listByTemplateStore(long templateId, long storeId);
4143

4244
List<TemplateDataStoreVO> listByTemplateStoreStatus(long templateId, long storeId, State... states);

engine/storage/src/org/apache/cloudstack/storage/image/db/TemplateDataStoreDaoImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ public void deletePrimaryRecordsForStore(long id) {
182182
txn.commit();
183183
}
184184

185+
@Override
186+
public void deletePrimaryRecordsForTemplate(long templateId) {
187+
SearchCriteria<TemplateDataStoreVO> sc = templateSearch.create();
188+
sc.setParameters("template_id", templateId);
189+
Transaction txn = Transaction.currentTxn();
190+
txn.start();
191+
expunge(sc);
192+
txn.commit();
193+
}
185194

186195
@Override
187196
public List<TemplateDataStoreVO> listByTemplateStore(long templateId, long storeId) {

server/src/com/cloud/template/TemplateManagerImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,8 @@ public VirtualMachineTemplate createPrivateTemplate(CreateTemplateCmd command)
14561456
if (privateTemplate == null) {
14571457
Transaction txn = Transaction.currentTxn();
14581458
txn.start();
1459+
// Remove the template_store_ref record first, otherwise, we cannot remove the template record due to FK constraints
1460+
this._tmplStoreDao.deletePrimaryRecordsForTemplate(templateId);
14591461
// Remove the template record
14601462
this._tmpltDao.expunge(templateId);
14611463

0 commit comments

Comments
 (0)