Skip to content

Commit

Permalink
Merge pull request #21 from snowio/reduce-deadlocks-bulk-saving-products
Browse files Browse the repository at this point in the history
Reduce deadlocks bulk saving products
  • Loading branch information
convenient authored Sep 26, 2023
2 parents 8d435bc + 8d0a30c commit 3a6004e
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion Model/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@ class ProductRepository implements ProductRepositoryInterface
private \SnowIO\AttributeOptionCode\Model\ProductDataMapper $productDataMapper;
private \Magento\Store\Model\StoreManagerInterface $storeManager;
private \Magento\Catalog\Model\ResourceModel\Product $productResourceModel;
private \Magento\Framework\App\ResourceConnection $resourceConnection;

public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterface $vanillaRepository,
ProductDataMapper $productDataMapper,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Model\ResourceModel\Product $productResourceModel
\Magento\Catalog\Model\ResourceModel\Product $productResourceModel,
\Magento\Framework\App\ResourceConnection $resourceConnection
) {
$this->vanillaRepository = $vanillaRepository;
$this->productDataMapper = $productDataMapper;
$this->storeManager = $storeManager;
$this->productResourceModel = $productResourceModel;
$this->resourceConnection = $resourceConnection;
}

public function save(ProductInterface $product, $saveOptions = false)
Expand All @@ -38,6 +41,17 @@ public function save(ProductInterface $product, $saveOptions = false)
);
}

/**
* Helps to reduce deadlocks saving products at volume
*
* @link https://dev.mysql.com/doc/refman/8.0/en/innodb-transaction-isolation-levels.html
* @link https://dev.mysql.com/doc/refman/8.0/en/innodb-deadlocks-handling.html
* @link https://github.com/magento/magento2/blob/15ea8fdeecca7f4164362c8f50fb1ad64245df98/app/code/Magento/Indexer/Model/ResourceModel/AbstractResource.php#L145
*/
$this->resourceConnection->getConnection()
->query('set session transaction isolation level read committed');


$this->productDataMapper->replaceOptionCodesWithOptionIds($product);
$this->vanillaRepository->save($product, $saveOptions);
}
Expand Down

0 comments on commit 3a6004e

Please sign in to comment.