Author: jleroux Date: Tue Jun 13 09:48:49 2017 New Revision: 1798571 URL: http://svn.apache.org/viewvc?rev=1798571&view=rev Log: No functional changes.
Replaces EntityListIterator.close() in finally by try-with-ressource I think I have done all these kinds of changes. This is tested. Modified: ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/category/CategoryServices.java ofbiz/ofbiz-framework/trunk/framework/minilang/src/main/java/org/apache/ofbiz/minilang/method/envops/Iterate.java ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/job/JobManager.java Modified: ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/category/CategoryServices.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/category/CategoryServices.java?rev=1798571&r1=1798570&r2=1798571&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/category/CategoryServices.java (original) +++ ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/category/CategoryServices.java Tue Jun 13 09:48:49 2017 @@ -285,7 +285,6 @@ public class CategoryServices { List<GenericValue> productCategoryMembers = null; if (productCategory != null) { - EntityListIterator pli = null; try { if (useCacheForMembers) { productCategoryMembers = EntityQuery.use(delegator).from(entityName).where("productCategoryId", productCategoryId).orderBy(orderByFields).cache(true).queryList(); @@ -354,45 +353,50 @@ public class CategoryServices { } EntityCondition mainCond = EntityCondition.makeCondition(mainCondList, EntityOperator.AND); - // set distinct on - // using list iterator - pli = EntityQuery.use(delegator).from(entityName).where(mainCond).orderBy(orderByFields).cursorScrollInsensitive().maxRows(highIndex).queryIterator(); - - // get the partial list for this page - if (limitView) { - if (viewProductCategoryId != null) { - // do manual checking to filter view allow - productCategoryMembers = new LinkedList<GenericValue>(); - GenericValue nextValue; - int chunkSize = 0; - listSize = 0; - - while ((nextValue = pli.next()) != null) { - String productId = nextValue.getString("productId"); - if (CategoryWorker.isProductInCategory(delegator, productId, viewProductCategoryId)) { - if (listSize + 1 >= lowIndex && chunkSize < viewSize) { - productCategoryMembers.add(nextValue); - chunkSize++; + // set distinct on using list iterator + EntityQuery eq = EntityQuery.use(delegator) + .from(entityName) + .where(mainCond) + .orderBy(orderByFields) + .cursorScrollInsensitive() + .maxRows(highIndex); + + try (EntityListIterator pli = eq.queryIterator()) { + // get the partial list for this page + if (limitView) { + if (viewProductCategoryId != null) { + // do manual checking to filter view allow + productCategoryMembers = new LinkedList<GenericValue>(); + GenericValue nextValue; + int chunkSize = 0; + listSize = 0; + + while ((nextValue = pli.next()) != null) { + String productId = nextValue.getString("productId"); + if (CategoryWorker.isProductInCategory(delegator, productId, viewProductCategoryId)) { + if (listSize + 1 >= lowIndex && chunkSize < viewSize) { + productCategoryMembers.add(nextValue); + chunkSize++; + } + listSize++; } - listSize++; } + } else { + productCategoryMembers = pli.getPartialList(lowIndex, viewSize); + listSize = pli.getResultsSizeAfterPartialList(); } } else { - productCategoryMembers = pli.getPartialList(lowIndex, viewSize); - listSize = pli.getResultsSizeAfterPartialList(); - } - } else { - productCategoryMembers = pli.getCompleteList(); - if (UtilValidate.isNotEmpty(viewProductCategoryId)) { - // filter out the view allow - productCategoryMembers = CategoryWorker.filterProductsInCategory(delegator, productCategoryMembers, viewProductCategoryId); + productCategoryMembers = pli.getCompleteList(); + if (UtilValidate.isNotEmpty(viewProductCategoryId)) { + // filter out the view allow + productCategoryMembers = CategoryWorker.filterProductsInCategory(delegator, productCategoryMembers, viewProductCategoryId); + } + + listSize = productCategoryMembers.size(); + lowIndex = 1; + highIndex = listSize; } - - listSize = productCategoryMembers.size(); - lowIndex = 1; - highIndex = listSize; } - // filter out of stock products if (filterOutOfStock) { try { @@ -415,16 +419,6 @@ public class CategoryServices { } catch (GenericEntityException e) { Debug.logError(e, module); } - finally { - // close the list iterator, if used - if (pli != null) { - try { - pli.close(); - } catch (GenericEntityException e) { - Debug.logError(e, module); - } - } - } } Map<String, Object> result = new HashMap<String, Object>(); Modified: ofbiz/ofbiz-framework/trunk/framework/minilang/src/main/java/org/apache/ofbiz/minilang/method/envops/Iterate.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/minilang/src/main/java/org/apache/ofbiz/minilang/method/envops/Iterate.java?rev=1798571&r1=1798570&r2=1798571&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/minilang/src/main/java/org/apache/ofbiz/minilang/method/envops/Iterate.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/minilang/src/main/java/org/apache/ofbiz/minilang/method/envops/Iterate.java Tue Jun 13 09:48:49 2017 @@ -75,9 +75,8 @@ public final class Iterate extends Metho Object oldEntryValue = entryFma.get(methodContext.getEnvMap()); Object objList = listFma.get(methodContext.getEnvMap()); if (objList instanceof EntityListIterator) { - EntityListIterator eli = (EntityListIterator) objList; - GenericValue theEntry; - try { + try (EntityListIterator eli = (EntityListIterator) objList) { + GenericValue theEntry; while ((theEntry = eli.next()) != null) { entryFma.put(methodContext.getEnvMap(), theEntry); try { @@ -96,12 +95,8 @@ public final class Iterate extends Metho throw e; } } - } finally { - try { - eli.close(); - } catch (GenericEntityException e) { - throw new MiniLangRuntimeException("Error closing entityListIterator: " + e.getMessage(), this); - } + } catch (GenericEntityException e) { + throw new MiniLangRuntimeException("Error with entityListIterator: " + e.getMessage(), this); } } else if (objList instanceof Collection<?>) { Collection<Object> theCollection = UtilGenerics.checkCollection(objList); Modified: ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/job/JobManager.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/job/JobManager.java?rev=1798571&r1=1798570&r2=1798571&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/job/JobManager.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/job/JobManager.java Tue Jun 13 09:48:49 2017 @@ -206,7 +206,6 @@ public final class JobManager { EntityCondition baseCondition = EntityCondition.makeCondition(expressions); EntityCondition poolCondition = EntityCondition.makeCondition(poolsExpr, EntityOperator.OR); EntityCondition mainCondition = EntityCondition.makeCondition(UtilMisc.toList(baseCondition, poolCondition)); - EntityListIterator jobsIterator = null; boolean beganTransaction = false; try { beganTransaction = TransactionUtil.begin(); @@ -214,19 +213,22 @@ public final class JobManager { Debug.logWarning("Unable to poll JobSandbox for jobs; unable to begin transaction.", module); return poll; } - jobsIterator = EntityQuery.use(delegator).from("JobSandbox").where(mainCondition).orderBy("runTime").queryIterator(); - GenericValue jobValue = jobsIterator.next(); - while (jobValue != null) { - // Claim ownership of this value. Using storeByCondition to avoid a race condition. - List<EntityExpr> updateExpression = UtilMisc.toList(EntityCondition.makeCondition("jobId", EntityOperator.EQUALS, jobValue.get("jobId")), EntityCondition.makeCondition("runByInstanceId", EntityOperator.EQUALS, null)); - int rowsUpdated = delegator.storeByCondition("JobSandbox", UtilMisc.toMap("runByInstanceId", instanceId), EntityCondition.makeCondition(updateExpression)); - if (rowsUpdated == 1) { - poll.add(new PersistedServiceJob(dctx, jobValue, null)); - if (poll.size() == limit) { - break; + try (EntityListIterator jobsIterator = EntityQuery.use(delegator).from("JobSandbox").where(mainCondition).orderBy("runTime").queryIterator()) { + GenericValue jobValue = jobsIterator.next(); + while (jobValue != null) { + // Claim ownership of this value. Using storeByCondition to avoid a race condition. + List<EntityExpr> updateExpression = UtilMisc.toList(EntityCondition.makeCondition("jobId", EntityOperator.EQUALS, jobValue.get("jobId")), EntityCondition.makeCondition("runByInstanceId", EntityOperator.EQUALS, null)); + int rowsUpdated = delegator.storeByCondition("JobSandbox", UtilMisc.toMap("runByInstanceId", instanceId), EntityCondition.makeCondition(updateExpression)); + if (rowsUpdated == 1) { + poll.add(new PersistedServiceJob(dctx, jobValue, null)); + if (poll.size() == limit) { + break; + } } + jobValue = jobsIterator.next(); } - jobValue = jobsIterator.next(); + } catch (GenericEntityException e) { + Debug.logWarning(e, module); } TransactionUtil.commit(beganTransaction); } catch (Throwable t) { @@ -238,14 +240,6 @@ public final class JobManager { } Debug.logWarning(t, errMsg, module); return Collections.emptyList(); - } finally { - if (jobsIterator != null) { - try { - jobsIterator.close(); - } catch (GenericEntityException e) { - Debug.logWarning(e, module); - } - } } if (poll.isEmpty()) { // No jobs to run, see if there are any jobs to purge @@ -263,21 +257,23 @@ public final class JobManager { EntityCondition doneCond = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition(canExp), EntityCondition.makeCondition(finExp)), EntityOperator.OR); mainCondition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("runByInstanceId", instanceId), doneCond)); beganTransaction = false; - jobsIterator = null; try { beganTransaction = TransactionUtil.begin(); if (!beganTransaction) { Debug.logWarning("Unable to poll JobSandbox for jobs; unable to begin transaction.", module); return Collections.emptyList(); } - jobsIterator = EntityQuery.use(delegator).from("JobSandbox").where(mainCondition).orderBy("jobId").queryIterator(); - GenericValue jobValue = jobsIterator.next(); - while (jobValue != null) { - poll.add(new PurgeJob(jobValue)); - if (poll.size() == limit) { - break; + try (EntityListIterator jobsIterator = EntityQuery.use(delegator).from("JobSandbox").where(mainCondition).orderBy("jobId").queryIterator()) { + GenericValue jobValue = jobsIterator.next(); + while (jobValue != null) { + poll.add(new PurgeJob(jobValue)); + if (poll.size() == limit) { + break; + } + jobValue = jobsIterator.next(); } - jobValue = jobsIterator.next(); + } catch (GenericEntityException e) { + Debug.logWarning(e, module); } TransactionUtil.commit(beganTransaction); } catch (Throwable t) { @@ -289,14 +285,6 @@ public final class JobManager { } Debug.logWarning(t, errMsg, module); return Collections.emptyList(); - } finally { - if (jobsIterator != null) { - try { - jobsIterator.close(); - } catch (GenericEntityException e) { - Debug.logWarning(e, module); - } - } } } return poll;