Skip to content

Commit

Permalink
[impr-OpenMage#103] Significant deadlock potential in cataloginventor…
Browse files Browse the repository at this point in the history
…y/stock resource model
  • Loading branch information
colinmollenhour authored and edannenberg committed Aug 17, 2020
1 parent fa6f377 commit 8265e90
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions app/code/core/Mage/CatalogInventory/Model/Resource/Stock.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,24 @@ public function getProductsStock($stock, $productIds, $lockRows = false)
$productTable = $this->getTable('catalog/product');
$select = $this->_getWriteAdapter()->select()
->from(array('si' => $itemTable))
->join(array('p' => $productTable), 'p.entity_id=si.product_id', array('type_id'))
->where('stock_id=?', $stock->getId())
->where('product_id IN(?)', $productIds)
->forUpdate($lockRows);
return $this->_getWriteAdapter()->fetchAll($select);
$rows = $this->_getWriteAdapter()->fetchAll($select);

// Add type_id to result using separate select without FOR UPDATE instead
// of a join which causes only an S lock on catalog_product_entity rather
// than an X lock. An X lock on a table causes an S lock on all foreign keys
// so using a separate query here significantly reduces the number of
// unnecessarily locked rows in other tables, thereby avoiding deadlocks.
$select = $this->_getWriteAdapter()->select()
->from($productTable, array('entity_id', 'type_id'))
->where('entity_id IN(?)', $productIds);
$typeIds = $this->_getWriteAdapter()->fetchPairs($select);
foreach ($rows as &$row) {
$row['type_id'] = $typeIds[$row['product_id']];
}
return $rows;
}

/**
Expand Down

0 comments on commit 8265e90

Please sign in to comment.