diff --git a/framework/db/src/com/cloud/utils/db/Transaction.java b/framework/db/src/com/cloud/utils/db/Transaction.java index dd91a967a06..e777cbd75ce 100755 --- a/framework/db/src/com/cloud/utils/db/Transaction.java +++ b/framework/db/src/com/cloud/utils/db/Transaction.java @@ -37,10 +37,10 @@ public class Transaction { } TransactionLegacy txn = TransactionLegacy.open(name, databaseId, false); try { -// if (txn.dbTxnStarted()){ -// String warnMsg = "Potential Wrong Usage: TRANSACTION.EXECUTE IS WRAPPED INSIDE ANOTHER DB TRANSACTION!"; -// s_logger.warn(warnMsg, new CloudRuntimeException(warnMsg)); -// } + // if (txn.dbTxnStarted()){ + // String warnMsg = "Potential Wrong Usage: TRANSACTION.EXECUTE IS WRAPPED INSIDE ANOTHER DB TRANSACTION!"; + // s_logger.warn(warnMsg, new CloudRuntimeException(warnMsg)); + // } txn.start(); T result = callback.doInTransaction(STATUS); txn.commit(); @@ -59,4 +59,30 @@ public class Transaction { }); } + @SuppressWarnings("deprecation") + public static T execute(final short databaseId, TransactionCallbackWithException callback) throws E { + String name = "tx-" + counter.incrementAndGet(); + TransactionLegacy currentTxn = TransactionLegacy.currentTxn(false); + short outer_txn_databaseId = (currentTxn != null ? currentTxn.getDatabaseId() : databaseId); + TransactionLegacy txn = TransactionLegacy.open(name, databaseId, true); + try { + txn.start(); + T result = callback.doInTransaction(STATUS); + txn.commit(); + return result; + } finally { + txn.close(); + TransactionLegacy.open(outer_txn_databaseId).close(); + } + } + + public static T execute(final short databaseId, final TransactionCallback callback) { + return execute(databaseId, new TransactionCallbackWithException() { + @Override + public T doInTransaction(TransactionStatus status) throws RuntimeException { + return callback.doInTransaction(status); + } + }); + } + } diff --git a/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaTariffDao.java b/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaTariffDao.java index 7977f28942d..f11a99d8af7 100644 --- a/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaTariffDao.java +++ b/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaTariffDao.java @@ -29,7 +29,7 @@ public interface QuotaTariffDao extends GenericDao { List listAllTariffPlans(Date onOrBefore); - boolean updateQuotaTariff(QuotaTariffVO plan); + Boolean updateQuotaTariff(QuotaTariffVO plan); QuotaTariffVO addQuotaTariff(QuotaTariffVO plan); } diff --git a/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaTariffDaoImpl.java b/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaTariffDaoImpl.java index c80db817e59..65c0d714ab4 100644 --- a/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaTariffDaoImpl.java +++ b/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaTariffDaoImpl.java @@ -20,9 +20,10 @@ import com.cloud.utils.db.Filter; 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.constant.QuotaTypes; import org.apache.cloudstack.quota.vo.QuotaTariffVO; import org.apache.log4j.Logger; @@ -56,102 +57,83 @@ public class QuotaTariffDaoImpl extends GenericDaoBase impl @Override public QuotaTariffVO findTariffPlanByUsageType(final int quotaType, final Date effectiveDate) { - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - List result = new ArrayList<>(); - try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) { - final Filter filter = new Filter(QuotaTariffVO.class, "updatedOn", false, 0L, 1L); - final SearchCriteria sc = listAllIncludedUsageType.create(); - sc.setParameters("onorbefore", effectiveDate); - sc.setParameters("quotatype", quotaType); - result = search(sc, filter); - } catch (Exception e) { - throw new CloudRuntimeException("Unable to find tariff plan by usage type"); - } finally { - TransactionLegacy.open(opendb).close(); - } - if (result != null && !result.isEmpty()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("QuotaTariffDaoImpl::findTariffPlanByUsageType: " + effectiveDate + "quota type " + quotaType + " val=" + result.get(0).getCurrencyValue()); + return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback() { + @Override + public QuotaTariffVO doInTransaction(final TransactionStatus status) { + List result = new ArrayList<>(); + final Filter filter = new Filter(QuotaTariffVO.class, "updatedOn", false, 0L, 1L); + final SearchCriteria sc = listAllIncludedUsageType.create(); + sc.setParameters("onorbefore", effectiveDate); + sc.setParameters("quotatype", quotaType); + result = search(sc, filter); + if (result != null && !result.isEmpty()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("QuotaTariffDaoImpl::findTariffPlanByUsageType: " + effectiveDate + "quota type " + quotaType + " val=" + result.get(0).getCurrencyValue()); + } + return result.get(0); + } else { + if (s_logger.isDebugEnabled()) { + s_logger.debug("QuotaTariffDaoImpl::findTariffPlanByUsageType: Missing quota type " + quotaType); + } + return null; + } } - return result.get(0); - } else { - if (s_logger.isDebugEnabled()) { - s_logger.debug("QuotaTariffDaoImpl::findTariffPlanByUsageType: Missing quota type " + quotaType); - } - return null; - } + }); } @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 List listAllTariffPlans(final Date effectiveDate) { - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - List tariffs = new ArrayList(); - try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) { - final Filter filter = new Filter(QuotaTariffVO.class, "updatedOn", false, 0L, 1L); - final SearchCriteria sc = listAllIncludedUsageType.create(); - sc.setParameters("onorbefore", effectiveDate); - for (Integer quotaType : QuotaTypes.listQuotaTypes().keySet()) { - sc.setParameters("quotatype", quotaType); - List result = search(sc, filter); - if (result != null && !result.isEmpty()) { - tariffs.add(result.get(0)); - s_logger.debug("ListAllTariffPlans on or before " + effectiveDate + " quota type " + result.get(0).getDescription() + " , effective Date=" + result.get(0).getEffectiveOn() - + " val=" + result.get(0).getCurrencyValue()); + return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback>() { + @Override + public List doInTransaction(final TransactionStatus status) { + List tariffs = new ArrayList(); + final Filter filter = new Filter(QuotaTariffVO.class, "updatedOn", false, 0L, 1L); + final SearchCriteria sc = listAllIncludedUsageType.create(); + sc.setParameters("onorbefore", effectiveDate); + for (Integer quotaType : QuotaTypes.listQuotaTypes().keySet()) { + sc.setParameters("quotatype", quotaType); + List result = search(sc, filter); + if (result != null && !result.isEmpty()) { + tariffs.add(result.get(0)); + s_logger.debug("ListAllTariffPlans on or before " + effectiveDate + " quota type " + result.get(0).getDescription() + " , effective Date=" + result.get(0).getEffectiveOn() + + " val=" + result.get(0).getCurrencyValue()); + } } + return tariffs; } - } catch (Exception e) { - throw new CloudRuntimeException("Unable to list all tariff plans"); - } finally { - TransactionLegacy.open(opendb).close(); - } - return tariffs; + }); } @Override - public boolean updateQuotaTariff(QuotaTariffVO plan) { - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - boolean result = false; - try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) { - result = this.update(plan.getId(), plan); - } catch (Exception e) { - throw new CloudRuntimeException("Unable to update quota tariff"); - } finally { - TransactionLegacy.open(opendb).close(); - } - return result; + public Boolean updateQuotaTariff(final QuotaTariffVO plan) { + return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback() { + @Override + public Boolean doInTransaction(final TransactionStatus status) { + return update(plan.getId(), plan); + } + }); } @Override - public QuotaTariffVO addQuotaTariff(QuotaTariffVO plan) { + public QuotaTariffVO addQuotaTariff(final QuotaTariffVO plan) { if (plan.getIdObj() != null) { throw new IllegalStateException("The QuotaTariffVO being added should not have an Id set "); } - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - QuotaTariffVO result = null; - try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) { - plan.setId(null); - result = this.persist(plan); - } catch (Exception e) { - throw new CloudRuntimeException("Unable to save quota tariff"); - } finally { - TransactionLegacy.open(opendb).close(); - } - return result; + return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback() { + @Override + public QuotaTariffVO doInTransaction(final TransactionStatus status) { + return persist(plan); + } + }); } } diff --git a/usage/src/org/apache/cloudstack/quota/QuotaAlertManagerImpl.java b/usage/src/org/apache/cloudstack/quota/QuotaAlertManagerImpl.java index 31b8e123054..65abf1c35b7 100644 --- a/usage/src/org/apache/cloudstack/quota/QuotaAlertManagerImpl.java +++ b/usage/src/org/apache/cloudstack/quota/QuotaAlertManagerImpl.java @@ -175,7 +175,7 @@ public class QuotaAlertManagerImpl extends ManagerBase implements QuotaAlertMana } } - } else { + } else if (lastStatementDate != null) { s_logger.info("For " + quotaAccount.getId() + " it is already more than " + getDifferenceDays(lastStatementDate, new Date()) + " days, will send statement in next cycle"); } } @@ -208,6 +208,7 @@ public class QuotaAlertManagerImpl extends ManagerBase implements QuotaAlertMana if (accountBalance.compareTo(zeroBalance) <= 0) { if (_lockAccountEnforcement && (lockable == 1)) { if (account.getType() == Account.ACCOUNT_TYPE_NORMAL) { + s_logger.info("Locking account " + account.getAccountName() + " due to quota exhasuted."); lockAccount(account.getId()); } }