Refer to the website
Blog.csdn.net/moxigandash…
What is transaction processing
Transaction processing refers to the execution of multiple operations or commands. The successful execution of all commands means the success of the transaction, and the failure of any command means the failure of the transaction.Copy the code
Example: Not even $100
A wants to transfer 100 yuan to B, the transfer instruction of A has been successfully sent, but B fails to receive it due to unknown reasons. If the two orders are executed separately, THEN ACCOUNT A loses 100 yuan, but B does not receive 100 yuan again, which is obviously unreasonable. If the transfer of 100 yuan from A to B is treated as A transaction, then the whole transfer transaction will fail due to the failure of B receiving, and A will not lose 100 yuan, let alone increase 100 yuan by B. In this case, it is reasonable to "not give 100 yuan". Therefore, the transaction is not one of prosperity, but of total ruin.Copy the code
How to perform transaction processing and locking in MySQL database
As mentioned earlier, a transaction is not a one-sideshow, but a one-sideshow, so if any one task or instruction fails in a transaction, the entire transaction fails. How does that work? If all the tasks are successful, the task ends successfully and a COMMIT is performed. If any of the tasks fail, force a ROLLBACK to the initial state. Transaction processing involves three of the most important commands: BEGIN,ROLLBACK, and COMMIT, which declare the transaction to start,ROLLBACK, and confirm the COMMIT, respectively. ROLLBACK First set the storage engine of the table Customer to InnoDB and confirm the table data; SELECT * FROM customer; Transaction start; BEGIN; Delete table data; DELETE FROM customer; Look at the table data again; SELECT * FROM customer; Rollback to the initial state; ROLLBACK; Look at the table data again; SELECT * FROM customerCopy the code
If you change ROLLBACK to COMMIT, the transaction will COMMIT and the deleted records will not be restored.Copy the code