[MySQL learning articles] --- affairs

[MySQL Learning Chapter]-Transaction

Introduce a transaction

Suppose that in the process of bank transfer, you need to execute the SQL statement for reducing money first, and then execute the SQL statement for adding money, but in the event of an accident in the middle, someone will suffer losses

UPDATE bank SET money=money-200 WHERE id='119';
#中间出现意外,钱减了,但是另一方没有加上
UPDATE bank SET money=money+200 WHERE id='911';

At this time, it is necessary to introduce a transaction to solve the problem to ensure that the two parts are a whole.

What is a transaction?

Multiple sets of operations, either all succeed or all fail

起点(start transaction):开启事务
	(中间失败:回滚 到 起点)rollback;
终点(commit):提交  当前事务已经结束
START TRANSACTION ;
UPDATE bank SET money=money-200 WHERE id='119';
UPDATE bank SET money=money+200 WHERE id='911';
COMMIT;

In other words, when the commit is executed, the data in the database is actually changed.

Four characteristics of the transaction:

  1. Atomicity (automic): Multiple groups of operations in the same transaction are inseparable and must be a whole

  2. Consistent: The total amount before the transaction operation is consistent with that after the transaction operation

  3. Isolation (ISOLATION): multiple transactions do not interfere with each other

  4. Persistence (durable): Once the data enters the database or table after submission, it will exist forever

Four isolation levels

Four isolation levels (from low to high): read uncommitted, read committed, repeatbale, Serializable The
higher the isolation level, the worse the performance

View mysql software isolation level

SELECT @@transaction_isolation;#(8.0以后)
select @@tx_isolation;#(8.0以前)

Modify the default isolation level of mysql software: SET GLOBAL TRANSACTION ISOLATION LEVEL isolation level;

SET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED;

Different isolation levels will cause different problems


Dirty read :

When the MySQL transaction isolation level is read uncommitted (read uncommitted), dirty reads will be triggered.

One transaction can read another uncommitted transaction: two transactions are opened at the same time, if transaction A adds, deletes, changes, and checks, but it is not committed, transaction B can read it.

Solve dirty read, when the level can be changed to read committed, it will cause non-repeatable read
Dirty read


Non-repeatable

When the MySQL transaction isolation level is read committed (read-only has been committed), non-repeatable reads are triggered.

In the same transaction, the results of multiple readings are inconsistent: when you start a transaction in the statistical financial statements, another transaction changes the financial data in the table. After the transaction is submitted, the statistical value of the statistical transaction is different each time.

How to solve non-repeatable read: change the level to repeatable read

Non-repeatable


Virtual reading (phantom reading)

When the level is repeatable, it will cause phantom reading (phantom reading)

Non-repeatable reading is due to data changes, while false reading is due to the increase in the number of data items, which affects the statistical value.
False reading


Guess you like

Origin blog.csdn.net/DREAM_yao/article/details/108168308