Related to issue 29766: Retail Operations Buffer: store all transactions in operations table before processing
authorMartin Taal <martin.taal@openbravo.com>
Sat, 13 Jun 2015 13:37:59 +0200
changeset 26913 875a134c2abb
parent 26906 91f6d8a87c92
child 26914 40fb5fa5584d
Related to issue 29766: Retail Operations Buffer: store all transactions in operations table before processing
Added extra checks in the import entry processing so that sessions/transactions are really committed
src/org/openbravo/service/importprocess/ImportEntryManager.java
src/org/openbravo/service/importprocess/ImportEntryProcessor.java
--- a/src/org/openbravo/service/importprocess/ImportEntryManager.java	Fri Jun 12 11:48:26 2015 +0200
+++ b/src/org/openbravo/service/importprocess/ImportEntryManager.java	Sat Jun 13 13:37:59 2015 +0200
@@ -43,6 +43,7 @@
 import org.hibernate.Query;
 import org.hibernate.ScrollMode;
 import org.hibernate.ScrollableResults;
+import org.openbravo.base.exception.OBException;
 import org.openbravo.base.provider.OBProvider;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.core.SessionHandler;
@@ -397,7 +398,7 @@
     try {
       // do not do org/client check as the error can be related to org/client access
       // so prevent this check to be done to even be able to save org/client access
-      // checks
+      // exceptions
       OBContext.setAdminMode(false);
       ImportEntry importEntry = OBDal.getInstance().get(ImportEntry.class, importEntryId);
       if (importEntry != null && !"Processed".equals(importEntry.getImportStatus())) {
@@ -406,6 +407,12 @@
         OBDal.getInstance().save(importEntry);
         OBDal.getInstance().commitAndClose();
       }
+    } catch (Throwable throwable) {
+      try {
+        OBDal.getInstance().rollbackAndClose();
+      } catch (Throwable ignored) {
+      }
+      throw new OBException(throwable);
     } finally {
       OBContext.restorePreviousMode();
       OBContext.setOBContext(prevOBContext);
--- a/src/org/openbravo/service/importprocess/ImportEntryProcessor.java	Fri Jun 12 11:48:26 2015 +0200
+++ b/src/org/openbravo/service/importprocess/ImportEntryProcessor.java	Sat Jun 13 13:37:59 2015 +0200
@@ -37,6 +37,7 @@
 import org.openbravo.dal.core.DalUtil;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.core.SessionHandler;
+import org.openbravo.dal.core.TriggerHandler;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.model.common.enterprise.Organization;
 
@@ -284,66 +285,78 @@
             // entry
             setOBContext(queuedImportEntry);
 
+            OBContext.setAdminMode(true);
+            ImportEntry localImportEntry;
             try {
-              OBContext.setAdminMode(true);
-              ImportEntry localImportEntry;
-              try {
-                // reload the importEntry
-                localImportEntry = OBDal.getInstance().get(ImportEntry.class,
-                    queuedImportEntry.importEntryId);
+              // reload the importEntry
+              localImportEntry = OBDal.getInstance().get(ImportEntry.class,
+                  queuedImportEntry.importEntryId);
 
-                // check if already processed, if so skip it
-                if (localImportEntry == null
-                    || !"Initial".equals(localImportEntry.getImportStatus())) {
-                  continue;
-                }
-              } finally {
-                OBContext.restorePreviousMode();
+              // check if already processed, if so skip it
+              if (localImportEntry == null || !"Initial".equals(localImportEntry.getImportStatus())) {
+                continue;
               }
+            } finally {
+              OBContext.restorePreviousMode();
+            }
 
-              // not changed, process
-              final String typeOfData = localImportEntry.getTypeofdata();
-              processEntry(localImportEntry);
+            // not changed, process
+            final String typeOfData = localImportEntry.getTypeofdata();
+            processEntry(localImportEntry);
 
-              // don't use the import entry anymore, touching methods on it
-              // may re-open a session
-              localImportEntry = null;
+            // don't use the import entry anymore, touching methods on it
+            // may re-open a session
+            localImportEntry = null;
 
-              // processed so can be removed
-              importEntryIds.remove(queuedImportEntry.importEntryId);
+            // processed so can be removed
+            importEntryIds.remove(queuedImportEntry.importEntryId);
 
-              // keep some stats
-              cnt++;
-              final long timeForEntry = (System.currentTimeMillis() - t0);
-              totalT += timeForEntry;
-              importEntryManager.reportStats(typeOfData, timeForEntry);
-              if ((cnt % 100) == 0 && logger.isDebugEnabled()) {
-                logger.debug("Runnable: " + key + ", processed " + cnt + " import entries in "
-                    + totalT + " millis, " + (totalT / cnt)
-                    + " per import entry, current queue size: " + importEntries.size());
-              }
+            // keep some stats
+            cnt++;
+            final long timeForEntry = (System.currentTimeMillis() - t0);
+            totalT += timeForEntry;
+            importEntryManager.reportStats(typeOfData, timeForEntry);
+            if ((cnt % 100) == 0 && logger.isDebugEnabled()) {
+              logger.debug("Runnable: " + key + ", processed " + cnt + " import entries in "
+                  + totalT + " millis, " + (totalT / cnt)
+                  + " per import entry, current queue size: " + importEntries.size());
+            }
 
-              // the import entry processEntry calls should not leave an open active session
-              if (SessionHandler.isSessionHandlerPresent()) {
-                // change to warning if the code in the subclasses really works correctly
-                logger
-                    .debug("Session handler present after processing import entry, this indicates that the processing code "
-                        + "does not correctly clean/close the session after its last actions. This should be fixed.");
-                OBDal.getInstance().commitAndClose();
-              }
+            if (TriggerHandler.getInstance().isDisabled()) {
+              logger
+                  .error("Triggers disabled at end of processing an entry, this is a coding error, "
+                      + "call TriggerHandler.enable in your code. Triggers are enabled again for now!");
+              TriggerHandler.getInstance().enable();
+              OBDal.getInstance().commitAndClose();
+            }
 
-            } finally {
-              cleanUpThreadForNextCycle();
+            // the import entry processEntry calls should not leave an open active session
+            if (SessionHandler.isSessionHandlerPresent()) {
+              // change to warning if the code in the subclasses really works correctly
+              logger
+                  .warn("Session handler present after processing import entry, this indicates that the processing code "
+                      + "does not correctly clean/close the session after its last actions. This should be fixed.");
+              OBDal.getInstance().commitAndClose();
             }
+
           } catch (Throwable t) {
             // bit rough but ensures that the connection is released/closed
             try {
               OBDal.getInstance().rollbackAndClose();
             } catch (Exception ignored) {
             }
+            try {
+              if (TriggerHandler.getInstance().isDisabled()) {
+                TriggerHandler.getInstance().enable();
+              }
+              OBDal.getInstance().commitAndClose();
+            } catch (Exception ignored) {
+            }
 
             // store the error
             importEntryManager.setImportEntryErrorIndependent(queuedImportEntry.importEntryId, t);
+          } finally {
+            cleanUpThreadForNextCycle();
           }
         }
         if (logger.isDebugEnabled()) {
@@ -360,6 +373,14 @@
         } catch (Exception ignored) {
         }
 
+        try {
+          if (TriggerHandler.getInstance().isDisabled()) {
+            TriggerHandler.getInstance().enable();
+          }
+          OBDal.getInstance().commitAndClose();
+        } catch (Exception ignored) {
+        }
+
         if (!importEntryProcessor.deregisterProcessThread(this)) {
           // a new entry was added when we tried to deregister, restart ourselves
           run();