fixed bug 42162: grid timeout returns unclear message
authorAsier Lostalé <asier.lostale@openbravo.com>
Thu, 31 Oct 2019 09:05:28 +0100
changeset 36728 23a72ef4e19b
parent 36727 96529e99687a
child 36729 fbeebc51d254
fixed bug 42162: grid timeout returns unclear message

When a query timeout occurs fetching grid data, the message displayed in
UI is not user friendly.

This is caused by different exception thrown after Hibernate upgrade.
modules/org.openbravo.service.json/src/org/openbravo/service/json/JsonUtils.java
--- a/modules/org.openbravo.service.json/src/org/openbravo/service/json/JsonUtils.java	Mon Oct 07 20:32:06 2019 -0300
+++ b/modules/org.openbravo.service.json/src/org/openbravo/service/json/JsonUtils.java	Thu Oct 31 09:05:28 2019 +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) 2009-2016 Openbravo SLU 
+ * All portions are Copyright (C) 2009-2019 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -25,13 +25,14 @@
 import java.util.List;
 import java.util.Map;
 
+import javax.persistence.QueryTimeoutException;
+
 import org.apache.commons.lang.StringUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.codehaus.jettison.json.JSONArray;
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
-import org.hibernate.QueryTimeoutException;
 import org.openbravo.authentication.AuthenticationManager;
 import org.openbravo.base.exception.OBException;
 import org.openbravo.base.exception.OBSecurityException;
@@ -238,10 +239,7 @@
         error.put("message", "OBUIAPP_ActionNotAllowed");
         error.put("type", "user");
         jsonResponse.put(JsonConstants.RESPONSE_ERROR, error);
-      } else if (localThrowable instanceof SQLTimeoutException
-          || localThrowable instanceof QueryTimeoutException
-          || (localThrowable.getCause() instanceof PSQLException && PG_QUERY_CANCELED
-              .equals(((PSQLException) localThrowable.getCause()).getSQLState()))) {
+      } else if (isQueryTimeout(localThrowable)) {
         final JSONObject error = new JSONObject();
         if (vars != null) {
           error.put("message", Utility.messageBD(new DalConnectionProvider(false),
@@ -271,6 +269,22 @@
     }
   }
 
+  private static boolean isQueryTimeout(Throwable localThrowable) {
+    // In case of query timeout in Hibernate, Oracle throws javax.persistence.QueryTimeoutException
+    // but PostgreSQL javax.persistence.PersistenceException, in PG the only way to get the root
+    // cause is to get the cause's cause and check SQL state.
+    if (localThrowable instanceof SQLTimeoutException
+        || localThrowable instanceof QueryTimeoutException) {
+      return true;
+    }
+
+    Throwable cause = localThrowable.getCause();
+    cause = cause != null ? cause.getCause() : cause;
+
+    return cause instanceof PSQLException
+        && PG_QUERY_CANCELED.equals(((PSQLException) cause).getSQLState());
+  }
+
   /**
    * Returns an empty result for a fetch call
    *