details: https://code.openbravo.com/erp/devel/pi/rev/e893c16b5687 changeset: 35189:e893c16b5687 user: Sandra Huguet <sandra.huguet <at> openbravo.com> date: Mon Dec 10 16:26:15 2018 +0100 summary: fixed issue 39755 change lockForNoKeyUpdate to return the instance with the lock
*Retrieves an object from the database getting a lock "for no key update" for the indicated object. Change method name, parameters and return. *Improve and modify the javadoc with the required changes. details: https://code.openbravo.com/erp/devel/pi/rev/05948dd8851c changeset: 35190:05948dd8851c user: Sandra Huguet <sandra.huguet <at> openbravo.com> date: Mon Dec 10 16:29:15 2018 +0100 summary: fixed issue 39672 use the new method getObjectLockForNoKeyUpdate diffstat: modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_TransactionProcess.java | 24 +----- src/org/openbravo/dal/service/OBDal.java | 33 ++++++--- 2 files changed, 27 insertions(+), 30 deletions(-) diffs (112 lines): diff -r 616f94939565 -r 05948dd8851c modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_TransactionProcess.java --- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_TransactionProcess.java Wed Dec 05 17:47:18 2018 +0100 +++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_TransactionProcess.java Mon Dec 10 16:29:15 2018 +0100 @@ -24,10 +24,7 @@ import org.apache.commons.lang.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.hibernate.LockOptions; -import org.hibernate.Session; import org.hibernate.criterion.Restrictions; -import org.hibernate.query.Query; import org.openbravo.advpaymentmngt.APRM_FinaccTransactionV; import org.openbravo.advpaymentmngt.dao.AdvPaymentMngtDao; import org.openbravo.advpaymentmngt.utility.FIN_Utility; @@ -172,7 +169,10 @@ && getConversionRateDocument(transaction).size() == 0) { insertConversionRateDocument(transaction); } - final FIN_FinancialAccount financialAccount = lockFinAccount(transaction.getAccount()); + + FIN_FinancialAccount financialAccount = OBDal.getInstance().getObjectLockForNoKeyUpdate( + transaction.getAccount()); + financialAccount.setCurrentBalance(financialAccount.getCurrentBalance().add( transaction.getDepositAmount().subtract(transaction.getPaymentAmount()))); transaction.setAprmProcessed("R"); @@ -242,7 +242,8 @@ transaction.setStatus(transaction.getDepositAmount().compareTo( transaction.getPaymentAmount()) > 0 ? "RPR" : "PPM"); } - final FIN_FinancialAccount financialAccount = lockFinAccount(transaction.getAccount()); + FIN_FinancialAccount financialAccount = OBDal.getInstance().getObjectLockForNoKeyUpdate( + transaction.getAccount()); financialAccount.setCurrentBalance(financialAccount.getCurrentBalance() .subtract(transaction.getDepositAmount()).add(transaction.getPaymentAmount())); transaction.setAprmProcessed("P"); @@ -366,17 +367,4 @@ } return confirmation; } - - private static FIN_FinancialAccount lockFinAccount(FIN_FinancialAccount account) { - StringBuilder queryStr = new StringBuilder( - "select a from FIN_Financial_Account a where id = :id"); - final Session session = OBDal.getInstance().getSession(); - final Query<FIN_FinancialAccount> query = session.createQuery(queryStr.toString(), - FIN_FinancialAccount.class); - query.setParameter("id", account.getId()); - query.setMaxResults(1); - query.setLockOptions(LockOptions.UPGRADE); - OBDal.getInstance().getSession().evict(account); - return query.uniqueResult(); - } } diff -r 616f94939565 -r 05948dd8851c src/org/openbravo/dal/service/OBDal.java --- a/src/org/openbravo/dal/service/OBDal.java Wed Dec 05 17:47:18 2018 +0100 +++ b/src/org/openbravo/dal/service/OBDal.java Mon Dec 10 16:29:15 2018 +0100 @@ -43,6 +43,7 @@ import org.openbravo.base.structure.BaseOBObject; import org.openbravo.base.structure.ClientEnabled; import org.openbravo.base.structure.OrganizationEnabled; +import org.openbravo.base.util.Check; import org.openbravo.dal.core.DalUtil; import org.openbravo.dal.core.OBContext; import org.openbravo.dal.core.SessionHandler; @@ -729,25 +730,33 @@ } /** - * Creates a lock for no key update + * Retrieves an object from the database getting a lock "for no key update" for the indicated + * object. Note that the object entering in the method is evicted and a new object is created. + * Before calling this method, is a must check if changes have been made to the object previously. + * In this case a flush must be done. * - * @param entity + * @param object * the type to create the query for - * @param id - * identifier of the record + * @return the new object getting a lock "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()); - } + @SuppressWarnings("unchecked") + public <T extends BaseOBObject> T getObjectLockForNoKeyUpdate(T object) { + Entity entity = object.getEntity(); + + Check.isTrue(entity.getIdProperties().size() == 1, "Expected entity with a single ID. " + + entity + " has " + entity.getIdProperties().size()); 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; + String sql = "SELECT " + entity.getIdProperties().get(0).getColumnName() + " FROM " + + entity.getTableName() + " WHERE " + entity.getIdProperties().get(0).getColumnName() + + " = :id FOR " + lockType; - getSession().createNativeQuery(sql).setParameter("id", id).list(); + Session session = getSession(); + session.evict(object); + session.createNativeQuery(sql).setParameter(BaseOBObject.ID, object.getId()).uniqueResult(); + return OBDal.getInstance().get((Class<T>) entity.getMappingClass(), object.getId()); + } } \ No newline at end of file _______________________________________________ Openbravo-commits mailing list Openbravo-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbravo-commits