Openbravo Issue Tracking System - Openbravo ERP
View Issue Details
0033138Openbravo ERPA. Platformpublic2016-06-05 14:542016-08-26 08:07
shuehner 
alostale 
normalminorhave not tried
closedfixed 
5
 
3.0PR16Q3 
caristu
Core
No
0033138: OBCriteria.initialize() is not protected against multiple calls per instance
OBCriteria has an internal initialize() method which adds the usual client,org,isactive filters.

However that initialize() method is not protected against multiple calls per instance.

So calling it more than once adds those filters also multiple times.

Several are at least 2 sequences triggering this
a.) call .count() + .uniqueResult() manually from application as seen i.e. in WebPOS OrderLoader:
    final OBCriteria<Locator> locators = OBDal.getInstance().createCriteria(Locator.class);
    locators.add(Restrictions.eq(Locator.PROPERTY_ACTIVE, true));
    locators.add(Restrictions.eq(Locator.PROPERTY_WAREHOUSE, shipment.getWarehouse()));
    // note the count causes a query but the good thing is that it doesn't cause loading
    // additional bin locations if there are too many
    if (locators.count() == 1) {
      foundSingleBin = (Locator) locators.uniqueResult();

b.) But also .count() implementation itself calls initialize() and then uniqueResult() (calling it again).

Trace all SQL's done by OrderLoader starting at part in createShipmentLines
    if (locators.count() == 1) {
      foundSingleBin = (Locator) locators.uniqueResult();

That SQL run by the .count() already has 3 blocks of the client+org filters happening (2 copies by this bug) + additional one reported as issue 33132.

Then the next SQL run by the manual uniqueResult added 2 more copies of the filters. (one by itself and maybe 2nd one because of that same 33132).
Add isInitialized boolean or similar to OBCriteria.initialize()
Performance
related to defect 0033816 closed caristu Find a complete solution to protect OBCriteria.initialize() against multiple calls per instance 
has duplicate defect 0033132 closed platform OBCriteria (at least sometimes) add client + org filters twice 
causes defect 0033797 closed Triage Omni OMS Product selector in sales order lines, don't take the last price list version 
causes defect 00338143.0PR16Q4 closed caristu Initialization in OBCriteria should not be prevented under some circumstances 
Issue History
2016-06-05 14:54shuehnerNew Issue
2016-06-05 14:54shuehnerAssigned To => platform
2016-06-05 14:54shuehnerModules => Core
2016-06-05 14:54shuehnerTriggers an Emergency Pack => No
2016-06-05 14:54shuehnerRelationship addedrelated to 0033132
2016-06-05 15:14shuehnerTag Attached: page data grid
2016-06-05 15:14shuehnerTag Attached: Performance
2016-06-05 15:14shuehnerTag Detached: page data grid
2016-06-06 15:13shuehnerNote Added: 0087034
2016-06-08 13:33shuehnerRelationship replacedhas duplicate 0033132
2016-06-10 09:54alostaleAssigned Toplatform => alostale
2016-06-10 09:54alostaleReview Assigned To => caristu
2016-06-10 09:59hgbotCheckin
2016-06-10 09:59hgbotNote Added: 0087147
2016-06-10 09:59hgbotStatusnew => resolved
2016-06-10 09:59hgbotResolutionopen => fixed
2016-06-10 09:59hgbotFixed in SCM revision => http://code.openbravo.com/erp/devel/pi/rev/ec3e4492a91c88e84d09e34b9e3e487f13ad3cb8 [^]
2016-06-17 09:26caristuNote Added: 0087380
2016-06-17 09:26caristuStatusresolved => closed
2016-06-17 09:26caristuFixed in Version => 3.0PR16Q3
2016-06-17 19:38hudsonbotCheckin
2016-06-17 19:38hudsonbotNote Added: 0087601
2016-08-25 13:10caristuRelationship addedcauses 0033797
2016-08-25 15:56shuehnerRelationship addedcauses 0033814
2016-08-25 16:18caristuIssue cloned0033816
2016-08-25 17:24shuehnerIssue Monitored: shuehner
2016-08-26 08:07caristuRelationship addedrelated to 0033816

Notes
(0087034)
shuehner   
2016-06-06 15:13   
shuehner (administrator)
2016-06-06 15:12

Later checking shows that even just calling uniqueResult() internally falls back to .list() so doing double init already.

Example calling sequence:
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1716)
    at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
    at org.openbravo.dal.service.OBCriteria.list(OBCriteria.java:90)
    at org.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:369)
    at org.openbravo.dal.service.OBCriteria.uniqueResult(OBCriteria.java:129)
    at org.openbravo.dal.service.OBCriteria.count(OBCriteria.java:103)

And each of count + uniqueResult + list of the OBCriteria calls initialize()
(0087147)
hgbot   
2016-06-10 09:59   
Repository: erp/devel/pi
Changeset: ec3e4492a91c88e84d09e34b9e3e487f13ad3cb8
Author: Asier Lostalé <asier.lostale <at> openbravo.com>
Date: Fri Jun 10 09:58:42 2016 +0200
URL: http://code.openbravo.com/erp/devel/pi/rev/ec3e4492a91c88e84d09e34b9e3e487f13ad3cb8 [^]

fixed bug 33138: OBCriteria adds standard filters several times

  In some cases (ie. in count method), OBCriteria adds the standard filters for
  client, org and active several times.

  The problem was caused because initialize method can be invoked several times
  on the same criteria instance adding each time those filters.

  Fixed now by setting a flag on 1st initialization and checking it to prevent
  subsequent ones. Note this flag is not thread safe, but in any case OBCriteria
  was not safe before it: several parallel initialization could clash.

---
M src/org/openbravo/dal/service/OBCriteria.java
---
(0087380)
caristu   
2016-06-17 09:26   
Verified

Reviewed in pi@a3288a6ce595
(0087601)
hudsonbot   
2016-06-17 19:38   
A changeset related to this issue has been promoted main and to the
Central Repository, after passing a series of tests.

Promotion changeset: https://code.openbravo.com/erp/devel/main/rev/0dc7be081b1c [^]
Maturity status: Test