CLOUDSTACK-8592: fixed various Quota*DaoImpls to use the new Transaction

callback mechanism
This commit is contained in:
Abhinandan Prateek 2015-10-19 14:55:53 +05:30
parent cb0fde68f8
commit 6c246acc16
5 changed files with 124 additions and 150 deletions

View File

@ -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<QuotaAccountVO, Long> im
@Override
public List<QuotaAccountVO> listAll() {
List<QuotaAccountVO> 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<List<QuotaAccountVO>>() {
@Override
public List<QuotaAccountVO> 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<QuotaAccountVO>() {
@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<QuotaAccountVO>() {
@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<Boolean>() {
@Override
public Boolean doInTransaction(final TransactionStatus status) {
return update(id, entity);
}
});
}
}

View File

@ -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<QuotaEmailTemplat
}
@Override
public List<QuotaEmailTemplatesVO> listAllQuotaEmailTemplates(String templateName) {
final short opendb = TransactionLegacy.currentTxn().getDatabaseId();
List<QuotaEmailTemplatesVO> result = new ArrayList<>();
try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) {
SearchCriteria<QuotaEmailTemplatesVO> sc = QuotaEmailTemplateSearch.create();
if (templateName != null) {
sc.setParameters("template_name", templateName);
public List<QuotaEmailTemplatesVO> listAllQuotaEmailTemplates(final String templateName) {
return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback<List<QuotaEmailTemplatesVO>>() {
@Override
public List<QuotaEmailTemplatesVO> doInTransaction(final TransactionStatus status) {
SearchCriteria<QuotaEmailTemplatesVO> 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<Boolean>() {
@Override
public Boolean doInTransaction(final TransactionStatus status) {
return update(template.getId(), template);
}
});
}
}

View File

@ -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<QuotaUsageVO, Long> implem
@Override
public List<QuotaUsageVO> findQuotaUsage(final Long accountId, final Long domainId, final Integer usageType, final Date startDate, final Date endDate) {
final short opendb = TransactionLegacy.currentTxn().getDatabaseId();
List<QuotaUsageVO> quotaUsageRecords = new ArrayList<QuotaUsageVO>();
try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) {
// TODO instead of max value query with reasonable number and iterate
SearchCriteria<QuotaUsageVO> sc = createSearchCriteria();
if (accountId != null) {
sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId);
return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback<List<QuotaUsageVO>>() {
@SuppressWarnings("deprecation")
@Override
public List<QuotaUsageVO> doInTransaction(final TransactionStatus status) {
SearchCriteria<QuotaUsageVO> 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<QuotaUsageVO>();
}
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<QuotaUsageVO>();
}
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;
});
}
}

View File

@ -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<ServiceOfferingVO, Lo
@Override
public ServiceOfferingVO findServiceOffering(final Long vmId, final long serviceOfferingId) {
final short opendb = TransactionLegacy.currentTxn().getDatabaseId();
ServiceOfferingVO result = null;
try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB)) {
result = findById(vmId, serviceOfferingId);
} catch (Exception e) {
s_logger.error("Quota ServiceOfferingDaoImpl::findServiceOffering() failed due to: " + e.getMessage(), e);
throw new CloudRuntimeException("Unable to find service offering for quota calculations");
} finally {
TransactionLegacy.open(opendb).close();
}
return result;
}
private ServiceOfferingVO findById(Long vmId, long serviceOfferingId) {
ServiceOfferingVO offering = super.findById(serviceOfferingId);
if (offering.isDynamic()) {
if (vmId == null) {
throw new CloudRuntimeException("missing argument vmId");
return Transaction.execute(TransactionLegacy.CLOUD_DB, new TransactionCallback<ServiceOfferingVO>() {
@Override
public ServiceOfferingVO doInTransaction(final TransactionStatus status) {
return findById(vmId, serviceOfferingId);
}
offering.setDynamicFlag(true);
Map<String, String> dynamicOffering = userVmDetailsDao.listDetailsKeyPairs(vmId);
return getcomputeOffering(offering, dynamicOffering);
}
return offering;
});
}
private ServiceOfferingVO getcomputeOffering(ServiceOfferingVO serviceOffering, Map<String, String> 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<ServiceOfferingVO>() {
@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<String, String> dynamicOffering = userVmDetailsDao.listDetailsKeyPairs(vmId);
return getcomputeOffering(offering, dynamicOffering);
}
return offering;
}
});
}
private ServiceOfferingVO getcomputeOffering(final ServiceOfferingVO serviceOffering, final Map<String, String> customParameters) {
return Transaction.execute(TransactionLegacy.CLOUD_DB, new TransactionCallback<ServiceOfferingVO>() {
@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;
}
});
}
}

View File

@ -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<QuotaUsageVO> 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);