When studying CRMEB multi-merchant, I saw the transaction and recorded my personal understanding of the transaction. Click on theThe source codeTo understandCRMEB more merchants, account Demo Password crmeb.com OthersThe open source projectSource:To learn moreLittle Sister QR code
I. What is a transaction.
- Before mysql 5.5, the default mysql engine was MyISAM. After mysql 5.5, InnoDB was introduced. One of the differences between InnoDB and mysql is that It supports transactions.
- A transaction is a basic unit of processing that executes once, either completely or not at all.
The properties of transactions (ACID).
- A, Atomicity. It can be understood as the basic unit of matter, the basic unit of our data processing operations, must be a whole indivisible.
- C) Consistency. Consistency refers to the database will change from the original consistent state to another consistent state after the transaction operation. This means that the database integrity constraint cannot be broken when a transaction is committed or when a transaction is rolled back.
- I is Isolation. It means that each transaction is independent of each other and is not affected by the execution of other transactions. This means that a transaction is not visible to other transactions until it is committed.
- D, for Durability. Changes made to the data after a transaction commits are persistent, even in the event of a system failure, such as a system crash or a storage medium failure. Because the database log is updated when the transaction is complete, you can use the log to restore the system to the state of the last successful update. (This is where rollback logs and redo logs come in)
Three. The use of transactions.
Start transaction or begin # savePoint # Create a savepoint in a transaction that can be rolled back to a certain point later. Commint # Commit a transaction that is done and changes to the database are permanent. Rollback # transaction rollback Rollback to [savepoint] # Release savepoint set Transaction Set the isolation level of the transactionCopy the code
-
There are two ways of transaction: implicit transaction and explicit transaction. The default of database is implicit transaction.
1.1. Implicit transactions. After each database change, the database is directly modified and the results are saved without the need for manual commit command, such as general add, delete, change operations;
1.2. To display the transaction, you need to commit the transaction manually after the last step.
Mysql > create table user(-> name varchar(255), -> primary key (name) ->) engine=InnoDB; Query OK, 0 rows affected (0.08 SEC) # mysql> begin; Query OK, 0 rows affected (0.00 SEC); Query OK, 1 row affected (0.01 SEC) Records: 1 Duplicates: 0 Warnings: Mysql > select * from user; mysql> select * from user; + -- -- -- -- -- -- -- -- + | name | + -- -- -- -- -- -- -- -- + | zhang SAN | + -- -- -- -- -- -- -- -- + 1 row in the set (0.00 SEC)Copy the code
Here we use mysql visualization tools to view, or open a terminal to view
Mysql > select * from user; mysql> select * from user; Empty set mysql>Copy the code
We return to the previous terminal commit transaction, in order to query.
Mysql > insert into user select 'mysql '; Query OK, 1 row affected (0.01 SEC) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from user; + -- -- -- -- -- -- -- -- + | name | + -- -- -- -- -- -- -- -- + | zhang SAN | + -- -- -- -- -- -- -- -- + 1 row in the set (0.00 SEC) # mysql > commit; Query OK, 0 rows affected (0.04 SEC) mysql>Copy the code
Query at another terminal
mysql> select * from user; Empty set mysql> select * from user; + -- -- -- -- -- -- -- -- + | name | + -- -- -- -- -- -- -- -- + | zhang SAN | + -- -- -- -- -- -- -- -- + 1 row in the set (0.00 SEC) mysql >Copy the code
If the transaction is not committed, and the transaction is rollback, the entire transaction will be rolled back. If the transaction is not committed, the transaction will rollback. The data of ‘Triple’ still does not exist the end of the transaction, which is the consistency of the transaction, the state before and after is consistent.
So that’s my personal understanding of things. If there is a wrong understanding of the place, please advise, to help you please point a link point a praise.