TCL Transaction Control Language

Many people don't understand the affairs of msql very well, of course, including me, so today I have explored the affairs of mysql, and the experience is recorded as follows:

First of all, the concept of transaction, what is transaction?

Transaction: A transaction is composed of one or more sql statements in a single unit. In this unit, each mysql statement is interdependent. The entire single unit as an indivisible whole, if a SQL statement in the unit fails or produces an error, the entire unit will be rolled back, and the affected data will return to the state before the start of the transaction. If all the units in the unit If the sql statements are executed successfully, the transaction is executed smoothly.

ACID properties of the transaction:

1. Atomicity:

Atomicity means that the transaction is an indivisible unit of work, and the operations in the transaction either all happen or none happen.

2. Consistency:

The transaction must transform the database from one consistent state to another consistent state.

3. Isolation:

Transaction isolation means that the execution of a transaction cannot be interfered by other transactions, that is, the internal operations and data used by a transaction are isolated from other concurrent transactions, and each transaction executed concurrently cannot disturb each other.

4, endurance:

Persistence means that once a transaction is committed, its changes to the data in the database are permanent, and other subsequent operations and database failures should not have any impact on it.

The creation of the transaction:

Implicit transaction: the transaction has no obvious opening and closing mark

Such as insert, update, delete

Explicit transaction: transaction has obvious opening and closing marks

Prerequisite: You must first set the automatic submission function to be disabled

set autocommit=0; (Only valid for the current, you need to reset it when you use it next time)

1、set autocommit=0

start transactions; optional

2. Write SQL statements in the transaction (select, insert, update, delete)

Statement 1;

Statement 2;

....

3. Step 3:

End the transaction

commit; commit the transaction

rollback; roll back the transaction

savepoint; save node

Give an example to demonstrate:

savepoint的使用

set autocommit=0; #关闭自动提交

start transaction;#开始事务

delete from account where id=25;#具体操作

savepoint a;#保存节点

delete from account where id=28;#事务的具体操作

rollback to a;#回滚到节点a

个人理解这个更像是快照或者锚点的意思,在需要的位置保存一个节点,然后需要回滚时,随时都可以回到节点保存时的状态。

In addition, for multiple transactions running at the same time, when these transactions access the same data in the database, if the necessary isolation mechanism is not adopted, it will cause various concurrency problems:

1. Dirty read: For two transactions T1 and T2, after T1 reads a field that has been updated by T2 but has not yet been committed, if T2 rolls back, the content read by T1 is temporary and invalid.

2. Non-repeatable read: For two transactions T1, T2, T1 read a field, then T2 updates the field, and then T1 reads the same field again, the value is different.

3. Phantom read: For two transactions, T1, T2, and T1 read a field from a table, and then T2 inserts some new rows in the table . After that, if T1 reads the same table again, it will There will be a few more lines.

Isolation of database transactions:

The database system must have the ability to isolate and run various transactions concurrently, so that they will not affect each other and avoid various concurrency problems.

The degree of isolation between a transaction and other transactions is called the isolation level. The database specifies a variety of transaction isolation levels, and different isolation levels correspond to different levels of interference. The higher the isolation level, the better the data consistency, but the weaker the concurrency.

The mysql database supports 4 transaction isolation levels, the default transaction isolation level of mysql is repeatable read

1. Read uncommitted (dirty read, phantom read, non-repeatable read)

2. Read has been submitted (solve dirty read)

3. Repeatable reads (solve dirty reads and non-repeatable reads, but not phantom reads)

4. Serialization (solve all problems), but the performance is relatively low

 So choose the default isolation level (repeatable read).

 

 

Guess you like

Origin blog.csdn.net/weixin_42575020/article/details/113526651
TCL