Fixes issue 30119: ScrollableResults object should be closed
authorCarlos Aristu <carlos.aristu@openbravo.com>
Wed, 10 Jun 2015 18:23:19 +0200
changeset 26895 4775fc0c799b
parent 26894 2a75107a6f00
child 26897 aab70f0c1a50
Fixes issue 30119: ScrollableResults object should be closed
modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/ADTreeDatasourceService.java
modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/LinkToParentTreeDatasourceService.java
--- a/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/ADTreeDatasourceService.java	Wed Jun 10 18:04:18 2015 +0200
+++ b/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/ADTreeDatasourceService.java	Wed Jun 10 18:23:19 2015 +0200
@@ -288,31 +288,36 @@
     int ENTITY = 4;
     int cont = 0;
     ScrollableResults scrollNodes = obq.createQuery().scroll(ScrollMode.FORWARD_ONLY);
-    while (scrollNodes.next()) {
-      Object[] node = scrollNodes.get();
-      JSONObject value = null;
-      BaseOBObject bob = (BaseOBObject) node[ENTITY];
-      try {
-        value = toJsonConverter.toJsonObject(bob, DataResolvingMode.FULL);
-        value.put("nodeId", bob.getId().toString());
-        if (fetchRoot) {
-          value.put("parentId", ROOT_NODE_CLIENT);
-        } else {
-          value.put("parentId", node[PARENT_ID]);
+    try {
+      while (scrollNodes.next()) {
+        Object[] node = scrollNodes.get();
+        JSONObject value = null;
+        BaseOBObject bob = (BaseOBObject) node[ENTITY];
+        try {
+          value = toJsonConverter.toJsonObject(bob, DataResolvingMode.FULL);
+          value.put("nodeId", bob.getId().toString());
+          if (fetchRoot) {
+            value.put("parentId", ROOT_NODE_CLIENT);
+          } else {
+            value.put("parentId", node[PARENT_ID]);
+          }
+          addNodeCommonAttributes(entity, bob, value);
+          value.put("seqno", node[SEQNO]);
+          value
+              .put("_hasChildren", (this.nodeHasChildren(entity, (String) node[NODE_ID],
+                  hqlWhereClause)) ? true : false);
+        } catch (JSONException e) {
+          logger.error("Error while constructing JSON reponse", e);
         }
-        addNodeCommonAttributes(entity, bob, value);
-        value.put("seqno", node[SEQNO]);
-        value.put("_hasChildren",
-            (this.nodeHasChildren(entity, (String) node[NODE_ID], hqlWhereClause)) ? true : false);
-      } catch (JSONException e) {
-        logger.error("Error while constructing JSON reponse", e);
+        responseData.put(value);
+        if ((cont % 100) == 0) {
+          OBDal.getInstance().flush();
+          OBDal.getInstance().getSession().clear();
+        }
+        cont++;
       }
-      responseData.put(value);
-      if ((cont % 100) == 0) {
-        OBDal.getInstance().flush();
-        OBDal.getInstance().getSession().clear();
-      }
-      cont++;
+    } finally {
+      scrollNodes.close();
     }
     return responseData;
   }
--- a/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/LinkToParentTreeDatasourceService.java	Wed Jun 10 18:04:18 2015 +0200
+++ b/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/LinkToParentTreeDatasourceService.java	Wed Jun 10 18:23:19 2015 +0200
@@ -284,44 +284,48 @@
     }
     int count = 0;
     final ScrollableResults scrollableResults = query.scroll(ScrollMode.FORWARD_ONLY);
-    while (scrollableResults.next()) {
-      BaseOBObject bob = (BaseOBObject) scrollableResults.get()[0];
-      final JSONObject json = toJsonConverter.toJsonObject(bob, DataResolvingMode.FULL);
-      if (fetchRoot) {
-        json.put("parentId", ROOT_NODE_CLIENT);
-      } else {
-        json.put("parentId", parentId);
+    try {
+      while (scrollableResults.next()) {
+        BaseOBObject bob = (BaseOBObject) scrollableResults.get()[0];
+        final JSONObject json = toJsonConverter.toJsonObject(bob, DataResolvingMode.FULL);
+        if (fetchRoot) {
+          json.put("parentId", ROOT_NODE_CLIENT);
+        } else {
+          json.put("parentId", parentId);
+        }
+        Object nodeId = bob.get(nodeIdProperty.getName());
+        String nodeIdStr = null;
+        if (nodeId instanceof String) {
+          nodeIdStr = (String) nodeId;
+        } else if (nodeId instanceof BaseOBObject) {
+          nodeIdStr = ((BaseOBObject) nodeId).getId().toString();
+        }
+
+        Object parentNodeId = bob.get(linkToParentProperty.getName());
+        String parentNodeIdStr = null;
+        if (parentNodeId instanceof String) {
+          parentNodeIdStr = (String) parentNodeId;
+        } else if (parentNodeId instanceof BaseOBObject) {
+          parentNodeIdStr = ((BaseOBObject) parentNodeId).getId().toString();
+        }
+
+        if (isMultiParentTree) {
+          json.put("nodeId", parentNodeIdStr + ID_SEPARATOR + nodeIdStr);
+        } else {
+          json.put("nodeId", nodeIdStr);
+        }
+        addNodeCommonAttributes(entity, bob, json);
+        json.put("_hasChildren", (this.nodeHasChildren(entity, linkToParentProperty,
+            nodeIdProperty, bob, hqlWhereClause)) ? true : false);
+        responseData.put(json);
+        count++;
+        if (count % 100 == 0) {
+          OBDal.getInstance().getSession().clear();
+        }
+
       }
-      Object nodeId = bob.get(nodeIdProperty.getName());
-      String nodeIdStr = null;
-      if (nodeId instanceof String) {
-        nodeIdStr = (String) nodeId;
-      } else if (nodeId instanceof BaseOBObject) {
-        nodeIdStr = ((BaseOBObject) nodeId).getId().toString();
-      }
-
-      Object parentNodeId = bob.get(linkToParentProperty.getName());
-      String parentNodeIdStr = null;
-      if (parentNodeId instanceof String) {
-        parentNodeIdStr = (String) parentNodeId;
-      } else if (parentNodeId instanceof BaseOBObject) {
-        parentNodeIdStr = ((BaseOBObject) parentNodeId).getId().toString();
-      }
-
-      if (isMultiParentTree) {
-        json.put("nodeId", parentNodeIdStr + ID_SEPARATOR + nodeIdStr);
-      } else {
-        json.put("nodeId", nodeIdStr);
-      }
-      addNodeCommonAttributes(entity, bob, json);
-      json.put("_hasChildren", (this.nodeHasChildren(entity, linkToParentProperty, nodeIdProperty,
-          bob, hqlWhereClause)) ? true : false);
-      responseData.put(json);
-      count++;
-      if (count % 100 == 0) {
-        OBDal.getInstance().getSession().clear();
-      }
-
+    } finally {
+      scrollableResults.close();
     }
     return responseData;
   }
@@ -840,42 +844,46 @@
     }
     int count = 0;
     final ScrollableResults scrollableResults = query.scroll(ScrollMode.FORWARD_ONLY);
-    while (scrollableResults.next()) {
-      BaseOBObject bob = (BaseOBObject) scrollableResults.get()[0];
-      final JSONObject json = toJsonConverter.toJsonObject(bob, DataResolvingMode.FULL);
+    try {
+      while (scrollableResults.next()) {
+        BaseOBObject bob = (BaseOBObject) scrollableResults.get()[0];
+        final JSONObject json = toJsonConverter.toJsonObject(bob, DataResolvingMode.FULL);
 
-      Object nodeId = bob.get(nodeIdProperty.getName());
-      String nodeIdStr = null;
-      if (nodeId instanceof String) {
-        nodeIdStr = (String) nodeId;
-      } else if (nodeId instanceof BaseOBObject) {
-        nodeIdStr = ((BaseOBObject) nodeId).getId().toString();
+        Object nodeId = bob.get(nodeIdProperty.getName());
+        String nodeIdStr = null;
+        if (nodeId instanceof String) {
+          nodeIdStr = (String) nodeId;
+        } else if (nodeId instanceof BaseOBObject) {
+          nodeIdStr = ((BaseOBObject) nodeId).getId().toString();
+        }
+
+        Object parentNodeId = bob.get(linkToParentProperty.getName());
+        String parentNodeIdStr = null;
+        if (parentNodeId instanceof String) {
+          parentNodeIdStr = (String) parentNodeId;
+        } else if (parentNodeId instanceof BaseOBObject) {
+          parentNodeIdStr = ((BaseOBObject) parentNodeId).getId().toString();
+        }
+        try {
+          json.put("nodeId", nodeIdStr);
+          if (parentNodeIdStr == null) {
+            json.put("parentId", ROOT_NODE_CLIENT);
+          } else {
+            json.put("parentId", parentNodeIdStr);
+          }
+        } catch (JSONException e) {
+          logger.error("Error on tree datasource", e);
+        }
+
+        parentList.add(json);
+        count++;
+        if (count % 100 == 0) {
+          OBDal.getInstance().getSession().clear();
+        }
+
       }
-
-      Object parentNodeId = bob.get(linkToParentProperty.getName());
-      String parentNodeIdStr = null;
-      if (parentNodeId instanceof String) {
-        parentNodeIdStr = (String) parentNodeId;
-      } else if (parentNodeId instanceof BaseOBObject) {
-        parentNodeIdStr = ((BaseOBObject) parentNodeId).getId().toString();
-      }
-      try {
-        json.put("nodeId", nodeIdStr);
-        if (parentNodeIdStr == null) {
-          json.put("parentId", ROOT_NODE_CLIENT);
-        } else {
-          json.put("parentId", parentNodeIdStr);
-        }
-      } catch (JSONException e) {
-        logger.error("Error on tree datasource", e);
-      }
-
-      parentList.add(json);
-      count++;
-      if (count % 100 == 0) {
-        OBDal.getInstance().getSession().clear();
-      }
-
+    } finally {
+      scrollableResults.close();
     }
     return parentList;
   }