I am using Magento 1.7.0.2 Community Edition and I have encountered a big problem - deadlocks and "Lock wait timeout exceeded" errors. Problem exists while specific CRON tasks are executed
So my question - is there a way how to avoid this(whether on PHP, MySQL or server(we use nginx) level)?
I came across this issue whilst trying to import more than five or six products at once. There is more information on deadlocks available here.
To solve this problem I had to place my database queries in SERIALIZABLE transactions where possible, like so:
$adapter = Mage::getModel('core/resource')->getConnection('core_write');
// Commit any existing transactions (use with caution!)
if ($adapter->getTransactionLevel > 0) {
$adapter->commit();
}
$adapter->query('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE');
$product->save(); // etc
Transaction example:
$adapter = Mage::getModel('core/resource')->getConnection('core_write');
// Commit any existing transactions (use with caution!)
if ($adapter->getTransactionLevel > 0) {
$adapter->commit();
}
$adapter->query('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE');
$adapter->beginTransaction();
try {
$adapter->query(/* SQL goes here */);
$adapter->commit();
} catch (Exception $e) {
// Rollback on fail always
$adapter->rollBack();
throw $e;
}
If you require any further help on this, feel free to let me know.