From e49d09f7ddfabe8d7ef33e6b032ee9864a6fd133 Mon Sep 17 00:00:00 2001 From: Jacques Le Roux Date: Wed, 31 May 2017 18:43:05 +0000 Subject: [PATCH] No functional changes. Replaces entityListIterator.close() in finally by try-with-ressource git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1797097 13f79535-47bb-0310-9956-ffa450edef68 --- .../ofbiz/product/product/ProductEvents.java | 25 +++++---------- .../product/product/ProductSearchSession.java | 28 ++++++----------- .../ofbiz/entity/finder/ListFinder.java | 4 +-- .../org/apache/ofbiz/service/ServiceUtil.java | 31 +++++++------------ 4 files changed, 30 insertions(+), 58 deletions(-) diff --git a/applications/product/src/main/java/org/apache/ofbiz/product/product/ProductEvents.java b/applications/product/src/main/java/org/apache/ofbiz/product/product/ProductEvents.java index 4442789440..e80338db6e 100644 --- a/applications/product/src/main/java/org/apache/ofbiz/product/product/ProductEvents.java +++ b/applications/product/src/main/java/org/apache/ofbiz/product/product/ProductEvents.java @@ -109,12 +109,11 @@ public class ProductEvents { } - EntityListIterator entityListIterator = null; int numProds = 0; int errProds = 0; boolean beganTx = false; - try { + try (EntityListIterator entityListIterator = EntityQuery.use(delegator).from("Product").where(condition).queryIterator()) { // begin the transaction beganTx = TransactionUtil.begin(7200); try { @@ -122,7 +121,6 @@ public class ProductEvents { long count = EntityQuery.use(delegator).from("Product").where(condition).queryCount(); Debug.logInfo("========== Found " + count + " products to index ==========", module); } - entityListIterator = EntityQuery.use(delegator).from("Product").where(condition).queryIterator(); } catch (GenericEntityException gee) { Debug.logWarning(gee, gee.getMessage(), module); Map messageMap = UtilMisc.toMap("gee", gee.toString()); @@ -160,21 +158,12 @@ public class ProductEvents { Debug.logError(e2, module); } return "error"; - } finally { - if (entityListIterator != null) { - try { - entityListIterator.close(); - } catch (GenericEntityException gee) { - Debug.logError(gee, "Error closing EntityListIterator when indexing product keywords.", module); - } - } - - // commit the transaction - try { - TransactionUtil.commit(beganTx); - } catch (GenericTransactionException e) { - Debug.logError(e, module); - } + } + // commit the transaction + try { + TransactionUtil.commit(beganTx); + } catch (GenericTransactionException e) { + Debug.logError(e, module); } if (errProds == 0) { diff --git a/applications/product/src/main/java/org/apache/ofbiz/product/product/ProductSearchSession.java b/applications/product/src/main/java/org/apache/ofbiz/product/product/ProductSearchSession.java index 800d3f6bda..81f2ef43e0 100644 --- a/applications/product/src/main/java/org/apache/ofbiz/product/product/ProductSearchSession.java +++ b/applications/product/src/main/java/org/apache/ofbiz/product/product/ProductSearchSession.java @@ -924,16 +924,16 @@ public class ProductSearchSession { addOnTopProdCondList.add(EntityCondition.makeCondition(EntityCondition.makeCondition("thruDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("thruDate", EntityOperator.GREATER_THAN, now))); addOnTopProdCondList.add(EntityCondition.makeCondition("fromDate", EntityOperator.LESS_THAN, now)); addOnTopProdCondList.add(EntityCondition.makeCondition("productCategoryId", EntityOperator.EQUALS, addOnTopProdCategoryId)); - EntityListIterator pli = null; - try { - pli = EntityQuery.use(delegator).select(UtilMisc.toSet("productId", "sequenceNum")) - .from("ProductCategoryMember") - .where(addOnTopProdCondList) - .orderBy("sequenceNum") - .cursorScrollInsensitive() - .distinct() - .maxRows(highIndex) - .queryIterator(); + EntityQuery eq = EntityQuery.use(delegator) + .select(UtilMisc.toSet("productId", "sequenceNum")) + .from("ProductCategoryMember") + .where(addOnTopProdCondList) + .orderBy("sequenceNum") + .cursorScrollInsensitive() + .distinct() + .maxRows(highIndex); + + try (EntityListIterator pli = eq.queryIterator()) { addOnTopProductCategoryMembers = pli.getPartialList(lowIndex, viewSize); addOnTopListSize = addOnTopProductCategoryMembers.size(); for (GenericValue alwaysAddProductCategoryMember: addOnTopProductCategoryMembers) { @@ -943,14 +943,6 @@ public class ProductSearchSession { listSize = listSize + addOnTopTotalListSize; } catch (GenericEntityException e) { Debug.logError(e, module); - } finally { - if (pli != null) { - try { - pli.close(); - } catch (GenericEntityException e) { - Debug.logError(e, module); - } - } } } diff --git a/framework/entity/src/main/java/org/apache/ofbiz/entity/finder/ListFinder.java b/framework/entity/src/main/java/org/apache/ofbiz/entity/finder/ListFinder.java index 955f91d722..9e6625ddf8 100644 --- a/framework/entity/src/main/java/org/apache/ofbiz/entity/finder/ListFinder.java +++ b/framework/entity/src/main/java/org/apache/ofbiz/entity/finder/ListFinder.java @@ -207,12 +207,10 @@ public abstract class ListFinder extends Finder { options.setMaxRows(size * (index + 1)); } boolean beganTransaction = false; - try { + try (EntityListIterator eli = delegator.find(entityName, whereEntityCondition, havingEntityCondition, fieldsToSelect, orderByFields, options)) { if (useTransaction) { beganTransaction = TransactionUtil.begin(); } - - EntityListIterator eli = delegator.find(entityName, whereEntityCondition, havingEntityCondition, fieldsToSelect, orderByFields, options); this.outputHandler.handleOutput(eli, context, listAcsr); } catch (GenericEntityException e) { String errMsg = "Failure in by " + label + " find operation, rolling back transaction"; diff --git a/framework/service/src/main/java/org/apache/ofbiz/service/ServiceUtil.java b/framework/service/src/main/java/org/apache/ofbiz/service/ServiceUtil.java index 7754d08757..d4cfd379c6 100644 --- a/framework/service/src/main/java/org/apache/ofbiz/service/ServiceUtil.java +++ b/framework/service/src/main/java/org/apache/ofbiz/service/ServiceUtil.java @@ -49,6 +49,7 @@ import org.apache.ofbiz.entity.util.EntityListIterator; import org.apache.ofbiz.entity.util.EntityQuery; import org.apache.ofbiz.security.Security; import org.apache.ofbiz.service.config.ServiceConfigUtil; +import org.apache.ofbiz.service.config.model.ServiceEngine; import com.ibm.icu.util.Calendar; @@ -372,7 +373,7 @@ public final class ServiceUtil { } public static Map purgeOldJobs(DispatchContext dctx, Map context) { - Locale locale = (Locale)context.get("locale"); + Locale locale = (Locale)context.get("locale"); Debug.logWarning("purgeOldJobs service invoked. This service is obsolete - the Job Scheduler will purge old jobs automatically.", module); String sendPool = null; Calendar cal = Calendar.getInstance(); @@ -419,22 +420,17 @@ public final class ServiceUtil { try { // begin this transaction beganTx1 = TransactionUtil.begin(); + EntityQuery eq = EntityQuery.use(delegator) + .select("jobId") + .from("JobSandbox") + .where(EntityCondition.makeCondition(UtilMisc.toList(doneCond, pool))) + .cursorScrollInsensitive() + .maxRows(1000); - EntityListIterator foundJobs = null; - try { - foundJobs = EntityQuery.use(delegator) - .select("jobId") - .from("JobSandbox") - .where(EntityCondition.makeCondition(UtilMisc.toList(doneCond, pool))) - .cursorScrollInsensitive() - .maxRows(1000) - .queryIterator(); + try (EntityListIterator foundJobs = eq.queryIterator()) { curList = foundJobs.getPartialList(1, 1000); - } finally { - if (foundJobs != null) { - foundJobs.close(); - } } + } catch (GenericEntityException e) { Debug.logError(e, "Cannot obtain job data from datasource", module); try { @@ -481,15 +477,14 @@ public final class ServiceUtil { // Now JobSandbox data is cleaned up. Now process Runtime data and remove the whole data in single shot that is of no need. boolean beganTx3 = false; GenericValue runtimeData = null; - EntityListIterator runTimeDataIt = null; List runtimeDataToDelete = new LinkedList(); long jobsandBoxCount = 0; try { // begin this transaction beganTx3 = TransactionUtil.begin(); - runTimeDataIt = EntityQuery.use(delegator).select("runtimeDataId").from("RuntimeData").queryIterator(); - try { + EntityQuery eq =EntityQuery.use(delegator).select("runtimeDataId").from("RuntimeData"); + try (EntityListIterator runTimeDataIt = eq.queryIterator()) { while ((runtimeData = runTimeDataIt.next()) != null) { EntityCondition whereCondition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("runtimeDataId", EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition("runtimeDataId", EntityOperator.EQUALS, runtimeData.getString("runtimeDataId"))), EntityOperator.AND); @@ -498,8 +493,6 @@ public final class ServiceUtil { runtimeDataToDelete.add(runtimeData); } } - } finally { - runTimeDataIt.close(); } // Now we are ready to delete runtimeData, we can safely delete complete list that we have recently fetched i.e runtimeDataToDelete. delegator.removeAll(runtimeDataToDelete);