@Override
@DB
public void checkResourceLimit(Account account, ResourceType type, long... count) throws ResourceAllocationException {
long numResources = ((count.length == 0) ? 1 : count[0]);
Project project = null;
// Don't place any limits on system or root admin accounts
if (_accountMgr.isRootAdmin(account.getType())) {
return;
}
if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
project = _projectDao.findByProjectAccountId(account.getId());
}
Transaction txn = Transaction.currentTxn();
txn.start();
try {
// Lock all rows first so nobody else can read it
Set<Long> rowIdsToLock = _resourceCountDao.listAllRowsToUpdate(account.getId(), ResourceOwnerType.Account, type);
SearchCriteria<ResourceCountVO> sc = ResourceCountSearch.create();
sc.setParameters("id", rowIdsToLock.toArray());
_resourceCountDao.lockRows(sc, null, true);
// Check account limits
long accountLimit = findCorrectResourceLimitForAccount(account, type);
long potentialCount = _resourceCountDao.getResourceCount(account.getId(), ResourceOwnerType.Account, type) + numResources;
if (accountLimit != Resource.RESOURCE_UNLIMITED && potentialCount > accountLimit) {
String message = "Maximum number of resources of type '" + type + "' for account name=" + account.getAccountName()
+ " in domain id=" + account.getDomainId() + " has been exceeded.";
if (project != null) {
message = "Maximum number of resources of type '" + type + "' for project name=" + project.getName()
+ " in domain id=" + account.getDomainId() + " has been exceeded.";
}
throw new ResourceAllocationException(message, type);
}
// check all domains in the account's domain hierarchy
Long domainId = null;
if (project != null) {
domainId = project.getDomainId();
} else {
domainId = account.getDomainId();
}
while (domainId != null) {