"Elaborate PHP" sample chapter fourth edition Chapter 18 database abstraction layer PDO 9

18.7 PDO transaction

Transactions are mechanisms to ensure consistent database, or a series of inquiries, as a unit of an ordered set of database operations. If the group of all SQL statements operation is successful, the transaction is considered successful, the transaction is committed, its changes will apply to all other database processes. Even in the affairs of the group, only one link fails, the transaction is not successful, the entire transaction is rolled back, all operations of the transaction will be canceled. Transaction capabilities is an important part of enterprise-class database, because many business process involves multiple steps. If any of these steps fails, all steps should not happen. Transaction has four important features: Atomicity (Atomicity), consistency (Consistency), independence (Isolation) and persistent (Durability), i.e. ACID. Any work performed in a single transaction, even if it is carried out in stages, and will be able to ensure that the work will be safely applied to the database, and when it is committed, not otherwise affected by the connection.

 

18.7.1 MySQL transaction processing

In MySQL 4.0 and above are enabled by default in the transaction, but currently only MySQL InnoDB and BDB tables two data types only support transactions, two table types have the same characteristics, InnoDB table types have also richer than BDB features, speed and more fast, therefore, recommended to use InnoDB table type. Creating InnoDB table types actually creating any other type of process is similar to the table, if the database is not set the default table type, you need to explicitly specify when you create the table will be created as InnoDB type. Creating the Employees table type InnoDB employees, code as follows:

 

CREATE TABLE employees (...) TYPE = InnoDB; // Use the specified table type InnoDB TYPE

   

 

By default, MySQL is automatically submitted (autocommit) mode, which means that each statement executed will be written to the database immediately. If you use the table type of transaction security, it is undesirable behavior automatically submitted. To turn off the auto-commit in the current session, execute the command shown MySQL:

 

MySQL> SET AUTOCOMMIT = 0; // Close automatically filed in the current session

   

 

If auto-commit is opened, you must use the following statement to the beginning of a transaction; if auto-commit is off, you do not need to use this command, because when input a SQL statement, a transaction is automatically started.

 

MySQL> START TRANSACTION; // begin a transaction

   

 

After completing a set of transaction statements input, you can use the following statement to submit it to the database. In this way, the transaction can be seen in other user sessions.

 

MySQL> COMMIT; // submit a transaction to the database

   

 

If you change your mind, you can use the following statement to the database back to a previous state.

 

MySQL> ROOLBACK; // transaction will be rolled back, all operations will be canceled

   

 

Not every database supports transactions, PDO only provides transaction support to the database can execute transactions. When the connection is opened for the first time, PDO need to "automatically submitted (auto-commit)" operating mode. If you need a transaction, you must use the PDO object beginTransaction () method to start a business. If the underlying driver does not support transactions, it will throw a PDOException exception. You can use PDO object commit () or rollback () method to end a transaction, depending on whether the code running in the success of the transaction.

 

18.7.2 build an application transaction processing

For example, an online shopping process, choose a product, priced at $ 80, using online bank transfer payments. Assume that the user userA userB to the user account transfer, you need to subtract 80 yuan from userA account, plus 80 yuan to userB account. First, a database in a demo InnoDB type data table (Account), two for storing user account information, including its name and available cash data, and inserts the data recording userA and userB table, as follows below:

f058300af01b4733ae350ed2cb6f9e08.png

 

In the following example, this transfer process needs to execute two SQL commands, the real scene there will be other steps. In order to ensure data consistency, you need to put this process into a transaction, to ensure that data is not due to a failure to perform the steps being destroyed, the code is as follows:

b54e93f13c4b4c2fa743211720ed3cef.png

 

In the above example, the simulated process userA transfer $ 80 to userB. This process requires two update statements cooperate to complete, so the use of transaction processing to ensure the consistency of these two SQL statements for data manipulation. Updates were completed two very simple, but through these two update statements include call beginTransaction () and commit () in, try to execute it through the try block, before the change can be completed to ensure that other people can not see to change. If an error occurs, the catch block can roll back all the changes that have occurred since the beginning of the transaction, and prints an error message.

 

 

Guess you like

Origin www.cnblogs.com/itxdl/p/11416119.html