Analysis of PDO transactions in PHP and phppdo Transaction Processing
This article analyzes the PDO transaction processing in PHP. We will share this with you for your reference. The details are as follows:
Transaction processing has four features: atomicity, consistency, independence, and durability.
Not all databases support transaction processing. PDO provides transaction support for databases that can execute transaction processing.
Note the following When configuring transaction processing:
1. Disable Automatic submission of PDO;
$pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, false);
2. methods required to start a transaction;
$ Pdo-> beginTransaction (); // start a transaction $ pdo-> commit (); // submit the transaction $ pdo-> rollback (); // roll back the transaction
3. Generally, the transaction is run in the try... catch... statement, and the catch code segment is executed when the transaction fails.
<? Phptry {$ pdo-> beginTransaction (); // start a transaction $ row = null; $ row = $ pdo-> exec ("xxx "); // execute the first SQL if (! $ Row) throw new PDOException ('prompt message or execute Action'); // if an exception message or an action is displayed $ row = $ pdo-> exec ("xxx "); // execute the second SQL if (! $ Row) throw new PDOException ('prompt message or execute Action'); $ pdo-> commit ();} catch (PDOException $ e) {$ pdo-> rollback (); // execution failed, transaction rollback exit ($ e-> getMessage ();}?>
If an error occurs in the SQL statement in the transaction, all the SQL statements are not executed. When all SQL statements are correct, they are submitted for execution.