The following article mainly describes the PHP (PHP training PHP tutorial) + mysql (MySQL certified MySQL training) transaction operation of the actual application code demonstration, we all know in the actual lamp application, PHP is generally used by the ADODB to operate the MySQL database, the following gives ADODB the corresponding code for easy access to everyone:
Starttrans ();
In fact, the query called by GetOne can also be placed directly into the rowlock, here just to demonstrate the effect can be more obvious.
$adodb->rowlock (' book ', ' book_id = 123 '); $bookNumber = $adodb->getone ("Select Book_number from book WHERE book_id = 123"); $adodb->execute ("UPDATE book SET book_numberbook_number = book_number-1 WHERE book_id = 123"); $adodb->completetrans (); // ... ? >
Where the Rowlock method is to invoke the for update to implement the row lock, you may want to "for update" directly into the $adodb->getone () called the SQL statement to implement the row lock function, yes, that is true, However, not all databases use the "for UPDATE" syntax to implement row lock functionality, such as Sybase using the "HOLDLOCK" syntax to implement row locking, so for your database abstraction layer to maintain portability, I still advise you to use Rowlock to implement the row lock function, As for portability, give it to ADODB, well, it's a little far away, and here we are today.
The above is a description of the code demonstration of PHP + MySQL transaction operations, and hopefully will bring you some help in this regard.
Report:
There is a Settransactionmode () method in ADODB that enables you to set the isolation level of the transaction as follows:
Settransactionmode allows you-pass in the transaction mode-to-use for all subsequent transactions for that connection s Ession. Note:if You has persistent connections and using MySQL or MSSQL, you might has to explicitly reset your transaction mod E at the beginning for each page request. This is a supported in PostgreSQL, MSSQL, MySQL with InnoDB and Oci8 currently. For example: $db->settransactionmode ("SERIALIZABLE"); $db->begintrans (); $db->execute (...); $db->execute (...); $db->commitrans (); $db->settransactionmode (""); Restore to default $db->starttrans (); $db->execute (...); $db->execute (...); $db->completetrans (); Supported values to pass in: * READ UNCOMMITTED (allows dirty reads, but fastest) * Read COMMITTED (default Postgres, MSSQ L and OCI8) * repeatable READ (default MySQL) * SERIALIZABLE (slowest and most restrictive)
The above related content is the PHP + MySQL transaction operation code demonstration of the introduction, hope you can gain.
Actual application code Demo for PHP + MySQL transaction operation