From 6c246acc16e0741cbcb5efa3be9bf036846e4c63 Mon Sep 17 00:00:00 2001 From: Abhinandan Prateek Date: Mon, 19 Oct 2015 14:55:53 +0530 Subject: [PATCH] CLOUDSTACK-8592: fixed various Quota*DaoImpls to use the new Transaction callback mechanism --- .../quota/dao/QuotaAccountDaoImpl.java | 84 +++++++------------ .../quota/dao/QuotaEmailTemplatesDaoImpl.java | 51 +++++------ .../quota/dao/QuotaUsageDaoImpl.java | 54 ++++++------ .../quota/dao/ServiceOfferingDaoImpl.java | 80 ++++++++++-------- .../cloudstack/quota/QuotaServiceImpl.java | 5 -- 5 files changed, 124 insertions(+), 150 deletions(-) diff --git a/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaAccountDaoImpl.java b/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaAccountDaoImpl.java index a45c98a8df2..6dc875260e8 100644 --- a/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaAccountDaoImpl.java +++ b/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaAccountDaoImpl.java @@ -17,13 +17,17 @@ package org.apache.cloudstack.quota.dao; import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; import com.cloud.utils.db.TransactionLegacy; -import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.db.TransactionStatus; + import org.apache.cloudstack.quota.vo.QuotaAccountVO; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; import javax.ejb.Local; + import java.util.List; @Component @@ -33,66 +37,42 @@ public class QuotaAccountDaoImpl extends GenericDaoBase im @Override public List listAll() { - List result = null; - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - try { - TransactionLegacy.open(TransactionLegacy.USAGE_DB).close(); - result = super.listAll(); - } catch (Exception e) { - s_logger.error("QuotaAccountDaoImpl::listAll() failed due to: " + e.getMessage()); - throw new CloudRuntimeException("Unable to list Quota Accounts"); - } finally { - TransactionLegacy.open(opendb).close(); - } - return result; + return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback>() { + @Override + public List doInTransaction(final TransactionStatus status) { + return listAll(); + } + }); } @Override - public QuotaAccountVO findById(Long id) { - QuotaAccountVO result = null; - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - try { - TransactionLegacy.open(TransactionLegacy.USAGE_DB).close(); - result = super.findById(id); - } catch (Exception e) { - s_logger.error("QuotaAccountDaoImpl::findById() failed due to: " + e.getMessage()); - throw new CloudRuntimeException("Unable to find Quota Account by ID"); - } finally { - TransactionLegacy.open(opendb).close(); - } - return result; + public QuotaAccountVO findById(final Long id) { + return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback() { + @Override + public QuotaAccountVO doInTransaction(final TransactionStatus status) { + return findById(id); + } + }); } @Override - public QuotaAccountVO persist(QuotaAccountVO entity) { - QuotaAccountVO result = null; - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - try { - TransactionLegacy.open(TransactionLegacy.USAGE_DB).close(); - result = super.persist(entity); - } catch (Exception e) { - s_logger.error("QuotaAccountDaoImpl::persist() failed due to: " + e.getMessage()); - throw new CloudRuntimeException("Unable to save Quota Account"); - } finally { - TransactionLegacy.open(opendb).close(); - } - return result; + public QuotaAccountVO persist(final QuotaAccountVO entity) { + return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback() { + @Override + public QuotaAccountVO doInTransaction(final TransactionStatus status) { + return persist(entity); + } + }); } @Override - public boolean update(Long id, QuotaAccountVO entity) { - boolean result = false; - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - try { - TransactionLegacy.open(TransactionLegacy.USAGE_DB).close(); - result = super.update(id, entity); - } catch (Exception e) { - s_logger.error("QuotaAccountDaoImpl::update() failed due to: " + e.getMessage()); - throw new CloudRuntimeException("Unable to update Quota Account"); - } finally { - TransactionLegacy.open(opendb).close(); - } - return result; + public boolean update(final Long id, final QuotaAccountVO entity) { + return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback() { + @Override + public Boolean doInTransaction(final TransactionStatus status) { + return update(id, entity); + } + }); } } diff --git a/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaEmailTemplatesDaoImpl.java b/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaEmailTemplatesDaoImpl.java index 1fba007277b..ecb51b9c708 100644 --- a/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaEmailTemplatesDaoImpl.java +++ b/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaEmailTemplatesDaoImpl.java @@ -19,14 +19,17 @@ package org.apache.cloudstack.quota.dao; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; import com.cloud.utils.db.TransactionLegacy; -import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.db.TransactionStatus; + import org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; import javax.ejb.Local; -import java.util.ArrayList; + import java.util.List; @Component @@ -45,36 +48,26 @@ public class QuotaEmailTemplatesDaoImpl extends GenericDaoBase listAllQuotaEmailTemplates(String templateName) { - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - List result = new ArrayList<>(); - try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) { - SearchCriteria sc = QuotaEmailTemplateSearch.create(); - if (templateName != null) { - sc.setParameters("template_name", templateName); + public List listAllQuotaEmailTemplates(final String templateName) { + return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback>() { + @Override + public List doInTransaction(final TransactionStatus status) { + SearchCriteria sc = QuotaEmailTemplateSearch.create(); + if (templateName != null) { + sc.setParameters("template_name", templateName); + } + return listBy(sc); } - result = this.listBy(sc); - } catch (Exception e) { - s_logger.error("QuotaEmailTemplatesDaoImpl::listAllQuotaEmailTemplates() failed due to: " + e.getMessage()); - throw new CloudRuntimeException("Unable to list quota email templates"); - } finally { - TransactionLegacy.open(opendb).close(); - } - return result; + }); } @Override - public boolean updateQuotaEmailTemplate(QuotaEmailTemplatesVO template) { - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - boolean result = false; - try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) { - result = this.update(template.getId(), template); - } catch (Exception e) { - s_logger.error("QuotaEmailTemplatesDaoImpl::updateQuotaEmailTemplate() failed due to: " + e.getMessage()); - throw new CloudRuntimeException("Unable to update quota email template"); - } finally { - TransactionLegacy.open(opendb).close(); - } - return result; + public boolean updateQuotaEmailTemplate(final QuotaEmailTemplatesVO template) { + return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback() { + @Override + public Boolean doInTransaction(final TransactionStatus status) { + return update(template.getId(), template); + } + }); } } diff --git a/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaUsageDaoImpl.java b/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaUsageDaoImpl.java index 47a2d4fbf8b..435f7f66e07 100644 --- a/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaUsageDaoImpl.java +++ b/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaUsageDaoImpl.java @@ -18,13 +18,16 @@ package org.apache.cloudstack.quota.dao; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; import com.cloud.utils.db.TransactionLegacy; -import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.db.TransactionStatus; import org.apache.cloudstack.quota.vo.QuotaUsageVO; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; import javax.ejb.Local; + import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; @@ -47,34 +50,29 @@ public class QuotaUsageDaoImpl extends GenericDaoBase implem @Override public List findQuotaUsage(final Long accountId, final Long domainId, final Integer usageType, final Date startDate, final Date endDate) { - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - List quotaUsageRecords = new ArrayList(); - try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) { - // TODO instead of max value query with reasonable number and iterate - SearchCriteria sc = createSearchCriteria(); - if (accountId != null) { - sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId); + return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback>() { + @SuppressWarnings("deprecation") + @Override + public List doInTransaction(final TransactionStatus status) { + SearchCriteria sc = createSearchCriteria(); + if (accountId != null) { + sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId); + } + if (domainId != null) { + sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId); + } + if (usageType != null) { + sc.addAnd("usageType", SearchCriteria.Op.EQ, usageType); + } + if ((startDate != null) && (endDate != null) && startDate.before(endDate)) { + sc.addAnd("startDate", SearchCriteria.Op.BETWEEN, startDate, endDate); + sc.addAnd("endDate", SearchCriteria.Op.BETWEEN, startDate, endDate); + } else { + return new ArrayList(); + } + return listBy(sc); } - if (domainId != null) { - sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId); - } - if (usageType != null) { - sc.addAnd("usageType", SearchCriteria.Op.EQ, usageType); - } - if ((startDate != null) && (endDate != null) && startDate.before(endDate)) { - sc.addAnd("startDate", SearchCriteria.Op.BETWEEN, startDate, endDate); - sc.addAnd("endDate", SearchCriteria.Op.BETWEEN, startDate, endDate); - } else { - return new ArrayList(); - } - quotaUsageRecords = listBy(sc); - } catch (Exception e) { - s_logger.error("QuotaUsageDaoImpl::findQuotaUsage() failed due to: " + e.getMessage(), e); - throw new CloudRuntimeException("Unable to find quota usage"); - } finally { - TransactionLegacy.open(opendb).close(); - } - return quotaUsageRecords; + }); } } diff --git a/framework/quota/src/org/apache/cloudstack/quota/dao/ServiceOfferingDaoImpl.java b/framework/quota/src/org/apache/cloudstack/quota/dao/ServiceOfferingDaoImpl.java index a8fb9a9efc9..a73fe950798 100644 --- a/framework/quota/src/org/apache/cloudstack/quota/dao/ServiceOfferingDaoImpl.java +++ b/framework/quota/src/org/apache/cloudstack/quota/dao/ServiceOfferingDaoImpl.java @@ -28,7 +28,10 @@ import org.apache.cloudstack.quota.vo.ServiceOfferingVO; import com.cloud.event.UsageEventVO; import com.cloud.utils.db.DB; import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; import com.cloud.utils.db.TransactionLegacy; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; @Component @@ -42,45 +45,50 @@ public class ServiceOfferingDaoImpl extends GenericDaoBase() { + @Override + public ServiceOfferingVO doInTransaction(final TransactionStatus status) { + return findById(vmId, serviceOfferingId); } - offering.setDynamicFlag(true); - Map dynamicOffering = userVmDetailsDao.listDetailsKeyPairs(vmId); - return getcomputeOffering(offering, dynamicOffering); - } - return offering; + }); } - private ServiceOfferingVO getcomputeOffering(ServiceOfferingVO serviceOffering, Map customParameters) { - ServiceOfferingVO dummyoffering = new ServiceOfferingVO(serviceOffering); - dummyoffering.setDynamicFlag(true); - if (customParameters.containsKey(UsageEventVO.DynamicParameters.cpuNumber.name())) { - dummyoffering.setCpu(Integer.parseInt(customParameters.get(UsageEventVO.DynamicParameters.cpuNumber.name()))); - } - if (customParameters.containsKey(UsageEventVO.DynamicParameters.cpuSpeed.name())) { - dummyoffering.setSpeed(Integer.parseInt(customParameters.get(UsageEventVO.DynamicParameters.cpuSpeed.name()))); - } - if (customParameters.containsKey(UsageEventVO.DynamicParameters.memory.name())) { - dummyoffering.setRamSize(Integer.parseInt(customParameters.get(UsageEventVO.DynamicParameters.memory.name()))); - } - return dummyoffering; + private ServiceOfferingVO findById(final Long vmId, final long serviceOfferingId) { + return Transaction.execute(TransactionLegacy.CLOUD_DB, new TransactionCallback() { + @Override + public ServiceOfferingVO doInTransaction(final TransactionStatus status) { + ServiceOfferingVO offering = findById(serviceOfferingId); + if (offering.isDynamic()) { + if (vmId == null) { + throw new CloudRuntimeException("missing argument vmId"); + } + offering.setDynamicFlag(true); + Map dynamicOffering = userVmDetailsDao.listDetailsKeyPairs(vmId); + return getcomputeOffering(offering, dynamicOffering); + } + return offering; + } + }); + } + + private ServiceOfferingVO getcomputeOffering(final ServiceOfferingVO serviceOffering, final Map customParameters) { + return Transaction.execute(TransactionLegacy.CLOUD_DB, new TransactionCallback() { + @Override + public ServiceOfferingVO doInTransaction(final TransactionStatus status) { + ServiceOfferingVO dummyoffering = new ServiceOfferingVO(serviceOffering); + dummyoffering.setDynamicFlag(true); + if (customParameters.containsKey(UsageEventVO.DynamicParameters.cpuNumber.name())) { + dummyoffering.setCpu(Integer.parseInt(customParameters.get(UsageEventVO.DynamicParameters.cpuNumber.name()))); + } + if (customParameters.containsKey(UsageEventVO.DynamicParameters.cpuSpeed.name())) { + dummyoffering.setSpeed(Integer.parseInt(customParameters.get(UsageEventVO.DynamicParameters.cpuSpeed.name()))); + } + if (customParameters.containsKey(UsageEventVO.DynamicParameters.memory.name())) { + dummyoffering.setRamSize(Integer.parseInt(customParameters.get(UsageEventVO.DynamicParameters.memory.name()))); + } + return dummyoffering; + } + }); } } diff --git a/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaServiceImpl.java b/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaServiceImpl.java index ac46ee74679..429aca961a4 100644 --- a/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaServiceImpl.java +++ b/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaServiceImpl.java @@ -25,7 +25,6 @@ import com.cloud.user.AccountVO; import com.cloud.user.dao.AccountDao; import com.cloud.utils.component.ManagerBase; import com.cloud.utils.db.Filter; -import com.cloud.utils.db.TransactionLegacy; import org.apache.cloudstack.api.command.QuotaBalanceCmd; import org.apache.cloudstack.api.command.QuotaCreditsCmd; @@ -206,9 +205,6 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi @Override public List getQuotaUsage(Long accountId, String accountName, Long domainId, Integer usageType, Date startDate, Date endDate) { - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - TransactionLegacy.open(TransactionLegacy.CLOUD_DB).close(); - // if accountId is not specified, use accountName and domainId if ((accountId == null) && (accountName != null) && (domainId != null)) { Account userAccount = null; @@ -228,7 +224,6 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi throw new PermissionDeniedException("Invalid Domain Id or Account"); } } - TransactionLegacy.open(opendb).close(); if (startDate.after(endDate)) { throw new InvalidParameterValueException("Incorrect Date Range. Start date: " + startDate + " is after end date:" + endDate);