details: https://code.openbravo.com/erp/devel/pi/rev/5c01854254bb changeset: 33452:5c01854254bb user: Mark <markmm82 <at> gmail.com> date: Fri Feb 09 09:29:04 2018 -0500 summary: Fix for issue 37852: VariantChDescUpdateProcessor fails with OutOfMemoryError when there are many products to update
The process to update the characteristics description using the import entry was failing if there are many products to update. Here some reasons causing this issue: 1. The VariantChDescUpdateProcessor wasn't clearing the session. 2. ProductCharacteristicValueEventHandler creates an import entry with all the products instead of having some limit. 3. The VariantChDescUpdateProcess was refreshing each ProductCharacteristic and ProductCharacteristicValue. These refreshes aren't really needed. As solution to this issue, were fixed the three points explained above: 1. The VariantChDescUpdateProcessor is clearing the session. 2. ProductCharacteristicValueEventHandler creates an import entry with a defined limit (currently 100) of products instead of passing the entire product list. 3. Was removed refreshing of the objects. details: https://code.openbravo.com/erp/devel/pi/rev/877547db1c2d changeset: 33453:877547db1c2d user: David Miguelez <david.miguelez <at> openbravo.com> date: Thu Feb 15 16:37:07 2018 +0100 summary: Related to Issue 37852. Code Review changes. * Changed import entry size from 3 to 100 * Created productSubListIds as a JSONArray from the start instead of converting it again * Added logger and log error message * Moved try-cacth to beginning-end of the block diffstat: src/org/openbravo/event/ProductCharacteristicValueEventHandler.java | 47 ++++++--- src/org/openbravo/materialmgmt/VariantChDescUpdateProcess.java | 7 +- src/org/openbravo/materialmgmt/VariantChDescUpdateProcessor.java | 4 +- 3 files changed, 33 insertions(+), 25 deletions(-) diffs (139 lines): diff -r ca9d0d95c9b7 -r 877547db1c2d src/org/openbravo/event/ProductCharacteristicValueEventHandler.java --- a/src/org/openbravo/event/ProductCharacteristicValueEventHandler.java Thu Feb 15 14:37:32 2018 +0100 +++ b/src/org/openbravo/event/ProductCharacteristicValueEventHandler.java Thu Feb 15 16:37:07 2018 +0100 @@ -18,16 +18,17 @@ */ package org.openbravo.event; +import java.util.ArrayList; import java.util.HashSet; import java.util.Set; import javax.enterprise.event.Observes; import javax.inject.Inject; -import org.apache.log4j.Logger; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.jfree.util.Log; import org.openbravo.base.model.Entity; import org.openbravo.base.model.ModelProvider; import org.openbravo.client.kernel.event.EntityDeleteEvent; @@ -40,9 +41,12 @@ import org.openbravo.erpCommon.utility.SequenceIdData; import org.openbravo.model.common.plm.ProductCharacteristicValue; import org.openbravo.service.importprocess.ImportEntryManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ProductCharacteristicValueEventHandler extends EntityPersistenceEventObserver { - protected Logger logger = Logger.getLogger(this.getClass()); + private static final int IMPORT_ENTRY_SIZE = 100; + protected Logger logger = LoggerFactory.getLogger(this.getClass()); private static Entity[] entities = { ModelProvider.getInstance().getEntity( ProductCharacteristicValue.ENTITY_NAME) }; private static ThreadLocal<Set<String>> prodchvalueUpdated = new ThreadLocal<Set<String>>(); @@ -83,23 +87,30 @@ } public void onTransactionCompleted(@Observes TransactionCompletedEvent event) { - Set<String> productList = prodchvalueUpdated.get(); - prodchvalueUpdated.set(null); - prodchvalueUpdated.remove(); - if (productList == null || productList.isEmpty() || event.getTransaction().wasRolledBack()) { - return; + try { + Set<String> productList = prodchvalueUpdated.get(); + prodchvalueUpdated.set(null); + prodchvalueUpdated.remove(); + if (productList == null || productList.isEmpty() || event.getTransaction().wasRolledBack()) { + return; + } + ArrayList<String> products = new ArrayList<String>(productList); + int productCount = productList.size(); + for (int i = 0; i < productCount; i += IMPORT_ENTRY_SIZE) { + int currentLimit = (i + IMPORT_ENTRY_SIZE) < productCount ? (i + IMPORT_ENTRY_SIZE) + : productCount; + JSONArray productSubListIds = new JSONArray(products.subList(i, currentLimit)); + JSONObject entryJson = new JSONObject(); + entryJson.put("productIds", productSubListIds); + if (!SessionHandler.getInstance().isCurrentTransactionActive()) { + SessionHandler.getInstance().beginNewTransaction(); + } + importEntryManager.createImportEntry(SequenceIdData.getUUID(), "VariantChDescUpdate", + entryJson.toString(), true); + } + } catch (JSONException ignore) { + Log.error("Error in ProductCharacteristicValueEventHandler.onTransactionCompleted", e); } - JSONObject entryJson = new JSONObject(); - try { - JSONArray productIds = new JSONArray(productList); - entryJson.put("productIds", productIds); - } catch (JSONException ignore) { - } - if (!SessionHandler.getInstance().isCurrentTransactionActive()) { - SessionHandler.getInstance().beginNewTransaction(); - } - importEntryManager.createImportEntry(SequenceIdData.getUUID(), "VariantChDescUpdate", - entryJson.toString(), true); } private void addProductToList(ProductCharacteristicValue pchv) { diff -r ca9d0d95c9b7 -r 877547db1c2d src/org/openbravo/materialmgmt/VariantChDescUpdateProcess.java --- a/src/org/openbravo/materialmgmt/VariantChDescUpdateProcess.java Thu Feb 15 14:37:32 2018 +0100 +++ b/src/org/openbravo/materialmgmt/VariantChDescUpdateProcess.java Thu Feb 15 16:37:07 2018 +0100 @@ -11,7 +11,7 @@ * under the License. * The Original Code is Openbravo ERP. * The Initial Developer of the Original Code is Openbravo SLU - * All portions are Copyright (C) 2013 Openbravo SLU + * All portions are Copyright (C) 2013-2018 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************* @@ -156,8 +156,6 @@ pchQuery.setFilterOnReadableOrganization(false); pchQuery.setNamedParameter("product", product); for (ProductCharacteristic pch : pchQuery.list()) { - // Reload pch to avoid errors after session clear. - OBDal.getInstance().refresh(pch); if (StringUtils.isNotBlank(strChDesc)) { strChDesc += ", "; } @@ -174,12 +172,9 @@ pchvQuery.setNamedParameter("ch", pch.getCharacteristic().getId()); pchvQuery.setNamedParameter("product", product.getId()); for (ProductCharacteristicValue pchv : pchvQuery.list()) { - // Reload pchv to avoid errors after session clear. - OBDal.getInstance().refresh(pchv); strChDesc += " " + pchv.getCharacteristicValue().getName(); } } product.setCharacteristicDescription(strChDesc); - OBDal.getInstance().save(product); } } \ No newline at end of file diff -r ca9d0d95c9b7 -r 877547db1c2d src/org/openbravo/materialmgmt/VariantChDescUpdateProcessor.java --- a/src/org/openbravo/materialmgmt/VariantChDescUpdateProcessor.java Thu Feb 15 14:37:32 2018 +0100 +++ b/src/org/openbravo/materialmgmt/VariantChDescUpdateProcessor.java Thu Feb 15 16:37:07 2018 +0100 @@ -11,7 +11,7 @@ * under the License. * The Original Code is Openbravo ERP. * The Initial Developer of the Original Code is Openbravo SLU - * All portions are Copyright (C) 2016 Openbravo SLU + * All portions are Copyright (C) 2016-2018 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************* @@ -66,6 +66,8 @@ .getInstanceFromStaticBeanManager(VariantChDescUpdateProcess.class); try { process.update(productIds.getString(i), null); + OBDal.getInstance().flush(); + OBDal.getInstance().getSession().clear(); } catch (Exception e) { log.error("Error updating product ch description", e); } ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ Openbravo-commits mailing list Openbravo-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbravo-commits