In-depth understanding of Mysql - transaction and lock principle

1. Definition of business

A transaction is a logical unit in the execution process of a database management system, consisting of a limited sequence of database operations. Among the storage engines of mysql, only InnoDB supports transactions.

Two, the four characteristics of business

  • Atomicity : Atomicity means that a transaction is an indivisible unit of work, and either all operations in a transaction occur or none occur. (InnoDB uses undo log to implement rollback operation)
  • Consistency : A transaction must transform the database from one consistent state to another.
  • Isolation : Transaction isolation means that when multiple users access the database concurrently, the transaction opened by the database for each user cannot be disturbed by the operation data of other transactions, and multiple concurrent transactions must be isolated from each other.
  • Durability : Durability means that once a transaction is committed, its changes to the data in the database are permanent, and then even if the database fails, it should not have any impact on it. (InnoDB uses redolog for persistence)

3. Transaction issues and isolation levels

When will the transaction be started: The client tool checks whether the transaction is automatically committed: SHOW GLOBAL VARIABLES LIKE "%autocommit%"; there are two levels of Global and session. on means to automatically open and commit transactions. Manually start transaction mode: use begin; or start transcation; command. If you manually end the transaction, use commit; or rollback; to roll back. The transaction will end after the rollback is committed.

Problems caused by business:

If multiple threads start their own transactions to operate data in the database, if isolation is not considered, the following problems will occur:

  • Dirty read : Refers to a transaction that reads uncommitted data from another transaction.

For example, there are two transactions. One transaction performs a transfer operation after opening the transaction. When the other party’s account transfer is completed, and the own account has not yet completed the deduction operation, the data of the two account information is queried by other transactions. At this time, it is called Dirty read.

  • Non-repeatable read : Read a certain row of data in the table in one transaction, and the results of multiple reads are different.

For example, when the bank checks the balance of account A, it checks the balance of 200 for the first time and prints a report. At this time, someone transfers 100 to account A and submits it; the bank checks the balance of A again and finds that it is 300. When printing other reports again, it is different from the balance displayed in the previous report.

The difference between non-repeatable reads and dirty reads is: dirty reads read uncommitted data from other transactions; non-repeatable reads read committed data from other transactions;

  • Phantom reading : refers to the reading of data inserted by other transactions in one transaction, resulting in inconsistency in reading before and after. (caused by insertion )

In a transaction, the same query range, due to the insert operation of other transactions, makes the number of result sets of the first query and the second query different, which is called phantom reading.

transaction isolation level

  • Read uncommitted (read uncommitted): the lowest level, none of the above conditions can be guaranteed.
  • Read committed (read committed): A transaction can only read data that has been committed by other transactions, and does not read uncommitted transaction data. Avoid dirty reads
  • Repeatable read (repeatable read): In the same transaction, the data result of reading the same data multiple times is the same. Dirty reads and non-repeatable reads can be avoided.
  • Serializable (serialization): It can avoid the occurrence of dirty reads, non-repeatable reads, and phantom reads. All transactions are executed serially, and there is no concurrency phenomenon, so all problems are solved.

Fourth, the implementation of InnoDB read consistency

  1. LBCC: When reading data, lock the data we want to operate, and do not allow other transactions to modify it. This is called lock-based concurrency control. Lock Based Concurency Control (LBCC).
  2. MVCC: When modifying data, we create a backup, or snapshot, of the data before modification. Just read the snapshot we created later, this is called Multi Version Concurrency Control (MVCC).

The idea of ​​MVCC is actually to be able to find the information of the data before the current transaction is started. After the transaction is started, any modifications made to the data by other transactions will be deleted, and the current transaction cannot be queried.

In InnoDB, InnoDB adds two hidden fields to each of our records.

DB_TRX_ID 6 bytes, records the current transaction number, and the new transaction number is incremented.

DB_ROLL_PTR 7 bytes: used to record that data is deleted, or becomes historical record data after being updated. It also stores the current transaction number.

For example: the above table, we are passing

For the first time, two records (transaction number 1) were created (the creation version number is 1, and the deletion version is undefined);

Open a query transaction (transaction number 2) for the second time (the version number is 2, and the query result is two items in step 1);

Open a new transaction (transaction number 3) for the third time, and add a new record (record with version number 3)

Open a new transaction for the fourth time (transaction number 4) and delete the record with id 2 (delete the version number in the record with id 2 and update it to the current transaction number 4);

For the fifth time, open a new transaction (transaction number 5) to modify the name column of the data with id 1, update the deleted version of the previous record to 5, and set the new creation version of the currently updated record to 5.

After the third, fourth, or fifth operations, each time you query in the second step of the transaction, the data is the two new data added in the previous first step. Because he only queries records with transaction numbers less than or equal to 2 for each query.

6. Types of InnoDB locks

Shared Locks : After we acquire the read lock of a row of data, we can use it to read the data, so it is also called a read lock. Manual locking: select xxxx where ... lock in share mode;

Exclusive locks (Exclusive Locks) : Exclusive locks are used to write data, so they are also called write locks. As long as a transaction acquires the write lock of a row of data, other transactions cannot acquire the read lock and exclusive lock of this row of data. Addition, deletion and modification will add an exclusive lock by default. For update can also manually add exclusive locks.

Intention lock : Intention lock is a lock on the table, which is divided into intention shared lock and intention exclusive lock . When there is a shared lock in a table, an intent shared lock will be added to the table before the shared lock is added. When there are records in the table that need to add an exclusive lock, an intention exclusive lock will be added to the table. Intention locks can be used to determine whether there is currently a lock on the table.

7. The principle and algorithm of InnoDB row lock

In a table without an index : using an exclusive lock will lock all records, because the query has no index, a full table scan will be performed, and then the hidden clustered index of each row will be locked.

Tables with primary key indexes: records with the same id are locked and conflict. Operations with different ids can be successfully locked.

Tables using auxiliary unique indexes : through unique index locking, the primary key index will also be locked. (Because the value of the primary key index is also stored in the auxiliary index, through the primary key index and then locked)

InnoDB's row lock is actually implemented by locking index records.

Lock algorithm:

Related concepts:

There is a table with a primary key index, in which there are 4 records 1 4 7 10, we call there are 4 Records here;

The area outside the Record we become the Gap gap;

The gap and the record on its right are called temporary construction intervals.

Record Lock: When we perform an equivalent query on a table with a primary key index or a unique index and exactly match a record, we use a record lock. For example where id = 1, 4 7 10.

Gap Lock: When the record we query does not exist, no matter whether we use range query or equivalent query, gap lock is used. Such as: where id>4 and id <7; where id =6;

Next-key Lock : When we use a range query, not only the Record record is hit, but also the Gap gap is included. In this case we use temporary locks. It is the default row locking algorithm in mysql.

Temporary lock example: select * from t where id>5 and id <7 for update; --lock (4,7] and (7,10]; 

select * from t where id>8 and id <=10 for update; --lock (7,10] and (10,+infinity); 

Mysql transaction isolation level default RR

Guess you like

Origin blog.csdn.net/liuhenghui5201/article/details/115567043