related to issue 39755 code review changes
authorSandra Huguet <sandra.huguet@openbravo.com>
Wed, 05 Dec 2018 17:17:46 +0100
changeset 35209 1c1d34a46d47
parent 35208 26214ce6e789
child 35210 58849d9510ba
related to issue 39755 code review changes

Not necessary to return the id. Only creates the lock.
Use createNativeQuery and improve code.
src/org/openbravo/dal/service/OBDal.java
--- a/src/org/openbravo/dal/service/OBDal.java	Tue Dec 04 15:47:59 2018 +0100
+++ b/src/org/openbravo/dal/service/OBDal.java	Wed Dec 05 17:17:46 2018 +0100
@@ -21,8 +21,6 @@
 
 import java.io.Serializable;
 import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -737,40 +735,19 @@
    *          the type to create the query for
    * @param id
    *          identifier of the record
-   * @return id
    */
-  public String lockForNoKeyUpdate(Entity entity, String id) {
-    final String RDBMS = new DalConnectionProvider(false).getRDBMS();
-    String strSQL;
-    if ("ORACLE".equals(RDBMS)) {
-      strSQL = "SELECT * FROM " + entity.getTableName() + " WHERE " + entity.getTableName()
-          + "_id =? FOR UPDATE";
-    } else {
-      strSQL = "SELECT * FROM " + entity.getTableName() + " WHERE " + entity.getTableName()
-          + "_id =? FOR NO KEY UPDATE";
+  public void lockForNoKeyUpdate(Entity entity, String id) {
+    if (entity.getIdProperties().size() != 1) {
+      throw new IllegalArgumentException("Expected entity with a single ID. " + entity + " has "
+          + entity.getIdProperties().size());
     }
-    PreparedStatement sqlQuery = null;
-    ResultSet rs = null;
-    try {
-      sqlQuery = new DalConnectionProvider(false).getPreparedStatement(strSQL);
-      sqlQuery.setString(1, id);
-      sqlQuery.execute();
-      sqlQuery.setMaxRows(1);
-      rs = sqlQuery.getResultSet();
-      while (rs.next()) {
-        return rs.getString(1);
-      }
-    } catch (Exception e) {
-      log.error("Error when executing query", e);
-    } finally {
-      try {
-        if (sqlQuery != null) {
-          sqlQuery.close();
-        }
-      } catch (Exception e) {
-        log.error("Error when closing statement", e);
-      }
-    }
-    return null;
+
+    String rdbms = new DalConnectionProvider(false).getRDBMS();
+    String lockType = "ORACLE".equals(rdbms) ? "UPDATE" : "NO KEY UPDATE";
+
+    String sql = "SELECT 1 FROM " + entity.getTableName() + " WHERE "
+        + entity.getIdProperties().get(0).getColumnName() + " = :id FOR " + lockType;
+
+    getSession().createNativeQuery(sql).setParameter("id", id).list();
   }
 }
\ No newline at end of file