From f02a322d93c07d049ffe75447105c86d9dc0ee39 Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Fri, 8 Mar 2019 12:08:04 +0000 Subject: [PATCH] server: Fix exception while update domain resource count While update resource count of a domain, I got the following exception which should not happen 2019-03-05 08:23:43,656 DEBUG [c.c.u.d.T.Transaction] (qtp858242339-15:ctx-13652208 ctx-56fe3704) (logid:130b0537) Rolling back the transaction: Time = 10 Name = qtp858242339-15; called by -TransactionLegacy.rollback:890-TransactionLegacy.removeUpTo:833-TransactionLegacy.close:657-TransactionContextInterceptor.invoke:36-ReflectiveMethodInvocation.proceed:174-ExposeInvocationInterceptor.invoke:92-ReflectiveMethodInvocation.proceed:185-JdkDynamicAopProxy.invoke:212-$Proxy42.countCpuNumberAllocatedToAccount:-1-ResourceLimitManagerImpl.countCpusForAccount:950-ResourceLimitManagerImpl.recalculateAccountResourceCount:910-ResourceLimitManagerImpl$3.doInTransaction:872 --- .../com/cloud/configuration/dao/ResourceCountDaoImpl.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/engine/schema/src/com/cloud/configuration/dao/ResourceCountDaoImpl.java b/engine/schema/src/com/cloud/configuration/dao/ResourceCountDaoImpl.java index 1de2b65aed12..82903104f90a 100644 --- a/engine/schema/src/com/cloud/configuration/dao/ResourceCountDaoImpl.java +++ b/engine/schema/src/com/cloud/configuration/dao/ResourceCountDaoImpl.java @@ -276,17 +276,18 @@ public long countMemoryAllocatedToAccount(long accountId) { } private long executeSqlCountComputingResourcesForAccount(long accountId, String sqlCountComputingResourcesAllocatedToAccount) { - try (TransactionLegacy tx = TransactionLegacy.currentTxn()) { + TransactionLegacy tx = TransactionLegacy.currentTxn(); + try { PreparedStatement pstmt = tx.prepareAutoCloseStatement(sqlCountComputingResourcesAllocatedToAccount); pstmt.setLong(1, accountId); ResultSet rs = pstmt.executeQuery(); if (!rs.next()) { - throw new CloudRuntimeException(String.format("An unexpected case happened while counting allocated computing resources for account: " + accountId)); + return 0L; } return rs.getLong("total"); } catch (SQLException e) { throw new CloudRuntimeException(e); } } -} \ No newline at end of file +}