php - How to detect implicit commit in MySQL? -


I would like to know that it is possible to detect broken transactions due to false or not committed. Currently I do not know how to check that transactions in my application work without any committed commitment. Apart from this, I can not see that my changes are not falsely committed.

It seems that the easiest way to control MySQL is to throw the error instead of making a mistake, but I do not know how to configure it.

Sometimes the problem is difficult to see in code, so MySQL error can be an effective way to protect transactions.

The examples below will show how much underlying commitment can be hidden

code with code:

  public function loadDataToDb (XMLReader $ productXML ) {$ This- & gt; Stats = $ this- & gt; GetProductImporter () - & gt; InitStats (); ($ ProductXML-> read ()) {if ($ productXML-> node type == XMLReader :: ELEMENT & $ productXML-> name == 'product') {$ doc = new DOMDocument ( '1.0', 'UTF-8'); $ Product = simplexml_import_dom ($ doc- & gt; Imported ($ productXML-> extension (), true)); $ This- & gt; GetDBConnection () - & gt; BeginTransaction (); {$ This-> GetStorage () - & gt; Try the adproduct ($ product); $ This- & gt; GetDBConnection () - & gt; Committed (); $ This- & gt; Statistics ['valid_skus'] [trim ($ product- gt; gt; gt; gt; gt; gt; gt; gt; & gt; sku)] = true; } Hold (exception $ E) {$ this-> Milan (DDBC connections) - & gt; Rollback (); $ This- & gt; Stats ['invalid_skus'] [trim ($ product- gt; gt; gt; gt; gt; gt; gt; gt; gt; & gt; sku)] = true; $ This- & gt; Product log ('product {'. $ Product- gt; gt;} omitted: '. $ E); }}}}  

Fixed code:

  public function loadDataToDb (XMLReader $ productXML) {$ this- & gt; Stats = $ this- & gt; GetProductImporter () - & gt; InitStats (); $ Storage = $ this- & gt; Get (); ($ ProductXML-> read ()) {if ($ productXML-> node type == XMLReader :: ELEMENT & $ productXML-> name == 'product') {$ doc = new DOMDocument ( '1.0', 'UTF-8'); $ Product = simplexml_import_dom ($ doc- & gt; Imported ($ productXML-> extension (), true)); $ This- & gt; GetDBConnection () - & gt; BeginTransaction (); {$ Storage- & gt; Try the add product ($ product); $ This- & gt; GetDBConnection () - & gt; Committed (); $ This- & gt; Statistics ['valid_skus'] [trim ($ product- gt; gt; gt; gt; gt; gt; gt; gt; & gt; sku)] = true; } Hold (exception $ E) {$ this-> Milan (DDBC connections) - & gt; Rollback (); $ This- & gt; Stats ['invalid_skus'] [trim ($ product- gt; gt; gt; gt; gt; gt; gt; gt; gt; & gt; sku)] = true; $ This- & gt; Product log ('product {'. $ Product- gt; gt;} omitted: '. $ E); Explanation: The function creates this storage object in the first call of $$ -> getStorage (), which it trunks into __ constructor. So the transaction is broken and it is not possible to find out without debugging.  

In such cases PDO exceptions will be very helpful.

If you use another catch block before the general exception and capture the PDO exception? Public function loadDataToDb (XMLReader $ productXML)

{$ this-> Statistics = $ this-> getProductImporter () -> Intestates (); $ Collection = $ the-> getstorage ();

  while ($ productXML-> (read) () (if ($ productXML-> node type == XMLReader :: ELEMENT & amp; amp;; ProductXML-> name == 'product') {$ doc = new DOMDocument ('1.0', 'UTF-8'); $ Product = simplexml_import_dom ($ doc-> Importation ($ productXML-> Extension () is true) ); $ This- & gt; getDBConnection () - & gt; startTransaction (); {$ Storage-> Add Product ($ product); $ this-> GetDBConnection () - & gt; committed (); $ This- & gt; Stats ['legitimate_skus'] [trim ($ product-> spo); = true;} hold (PDOException $ pdo) {$ this-> getDBConnection () -> rollback (); $ This-> Stats ['invalid_scus'] [trim ($ product-> gt; gt; gt; gt; gt; gt; & gt; F; $ The-> ProductLog ('Product database error: {'. $ Product- & gt; sku.}} Dropped: '. $ E);} hold (exception $ e) {$ this- & gt; ; Product log ('Product error:'. $ E);}}}  

}


Comments