I would like to know if it is possible to detect broken transactions caused by implicit commit or not. I currently don't know how to check if transactions are working in my application without implicit commit. Also, I don’t see if implicit commits have changed my changes.
It seems like the easiest way to control this is to get MySQL to throw an error instead of an implicit commit, but I don't know how to configure it.
Sometimes it is difficult to see a problem in the code, so a MySQL error can be an effective way to protect transactions.
The example below shows how much hidden commit can be hidden.
Implicit commit code:
public function loadDataToDb(XMLReader $productXML)
{
$this->stats = $this->getProductImporter()->initStats();
while ($productXML->read()) {
if ($productXML->nodeType == XMLReader::ELEMENT && $productXML->name == 'product') {
$doc = new DOMDocument('1.0', 'UTF-8');
$product = simplexml_import_dom($doc->importNode($productXML->expand(), true));
$this->getDBConnection()->beginTransaction();
try {
$this->getStorage()->addProduct($product);
$this->getDBConnection()->commit();
$this->stats['valid_skus'][trim($product->sku)] = true;
} catch (Exception $e) {
$this->getDBConnection()->rollBack();
$this->stats['invalid_skus'][trim($product->sku)] = true;
$this->productLog('Product {' . $product->sku . '} skipped:' . $e);
}
}
}
}
Fixed Code:
public function loadDataToDb(XMLReader $productXML)
{
$this->stats = $this->getProductImporter()->initStats();
$storage = $this->getStorage();
while ($productXML->read()) {
if ($productXML->nodeType == XMLReader::ELEMENT && $productXML->name == 'product') {
$doc = new DOMDocument('1.0', 'UTF-8');
$product = simplexml_import_dom($doc->importNode($productXML->expand(), true));
$this->getDBConnection()->beginTransaction();
try {
$storage->addProduct($product);
$this->getDBConnection()->commit();
$this->stats['valid_skus'][trim($product->sku)] = true;
} catch (Exception $e) {
$this->getDBConnection()->rollBack();
$this->stats['invalid_skus'][trim($product->sku)] = true;
$this->productLog('Product {' . $product->sku . '} skipped:' . $e);
}
}
}
}
: $this- > getStorage() , __constructor. , , .
pdo .