Fixes issue 30308: Use get instead of refresh when saving or updating a record
authorCarlos Aristu <carlos.aristu@openbravo.com>
Fri, 03 Jul 2015 16:04:19 +0200
changeset 27064 b26a24d2053e
parent 27063 c5dd3e4e82e5
child 27065 e45f0f903a95
Fixes issue 30308: Use get instead of refresh when saving or updating a record

We use now OBDal.getInstance().get() instead of OBDal.getInstance().getSession().refresh(). To ensure that the get() methods retrieves the last changes in the object from database, we call evict() to remove the bob from the cache
modules/org.openbravo.service.json/src/org/openbravo/service/json/DefaultJsonDataService.java
--- a/modules/org.openbravo.service.json/src/org/openbravo/service/json/DefaultJsonDataService.java	Thu Jul 02 10:54:26 2015 +0200
+++ b/modules/org.openbravo.service.json/src/org/openbravo/service/json/DefaultJsonDataService.java	Fri Jul 03 16:04:19 2015 +0200
@@ -702,13 +702,21 @@
         }
 
         // refresh the objects from the db as they can have changed
+        // put the refreshed objects into a new array as we are going to retrieve them using
+        // OBDal.getInstance().get as performs better than OBDal.getInstance().getSession().refresh
+        // See issue https://issues.openbravo.com/view.php?id=30308
+        final List<BaseOBObject> refreshedBobs = new ArrayList<BaseOBObject>();
         for (BaseOBObject bob : bobs) {
-          OBDal.getInstance().getSession().refresh(bob);
+          // Remove the bob instance from the session cache with evict
+          OBDal.getInstance().getSession().evict(bob);
+          // With get() we retrieve the object from db as we have cleared it from cache with evict()
+          BaseOBObject refreshedBob = OBDal.getInstance().get(bob.getEntityName(), bob.getId());
           // if object has computed columns refresh from the database too
-          if (bob.getEntity().hasComputedColumns()) {
+          if (refreshedBob.getEntity().hasComputedColumns()) {
             OBDal.getInstance().getSession()
-                .refresh(bob.get(Entity.COMPUTED_COLUMNS_PROXY_PROPERTY));
+                .refresh(refreshedBob.get(Entity.COMPUTED_COLUMNS_PROXY_PROPERTY));
           }
+          refreshedBobs.add(refreshedBob);
         }
 
         // almost successfull, now create the response
@@ -716,7 +724,7 @@
         final DataToJsonConverter toJsonConverter = OBProvider.getInstance().get(
             DataToJsonConverter.class);
         toJsonConverter.setAdditionalProperties(JsonUtils.getAdditionalProperties(parameters));
-        final List<JSONObject> jsonObjects = toJsonConverter.toJsonObjects(bobs);
+        final List<JSONObject> jsonObjects = toJsonConverter.toJsonObjects(refreshedBobs);
 
         if (sendOriginalIdBack) {
           // now it is assumed that the jsonObjects are the same size and the same location