Home > Database > Mysql Tutorial > body text

what is mysql transaction

藏色散人
Release: 2023-04-04 10:47:33
Original
5148 people have browsed it

Mysql transaction refers to the execution of a batch of operations on the database. In the same transaction, these operations will either all succeed or fail, and there will be no partial success; the transaction is an atomic operation and is A minimal execution unit can consist of one or more SQL statements.

what is mysql transaction

The operating environment of this tutorial: Windows 10 system, MySQL version 5.7, Dell G3 computer.

Detailed explanation of transactions

What is a transaction?

A transaction in a database refers to the execution of a batch of operations on the database. In the same transaction, these operations will either all succeed or fail, and there will be no partial success.

  • A transaction is an atomic operation. Is a minimum execution unit. Can be composed of one or more SQL statements
  • In the same transaction, when all SQL statements are successfully executed, the entire transaction is successful. If one SQL statement fails to execute, the entire transaction fails to execute.

For example:

For example, user A transfers 100 to user B. The process is as follows:

  1. Deduct 100 from account A
  2. Add 100 to account B

If supported by transactions, there are only two results in the above:

  1. The operation is successful: account A decreases by 100; account B increases 100
  2. Operation failed: neither account A nor B has changed

If there is no transaction support, an error may occur: Account A is reduced by 100, and the system hangs at this time , causing the B account not to add 100, and the A account to lose 100 out of thin air.

Several characteristics of transactions (ACID) - Key points

Atomicity(Atomicity)

The entire process of the transaction is as follows Atomic operations are the same. In the end, either all succeed or all fail. This atomicity is seen from the final result. From the final result, this process is indivisible.

Consistency(Consistency)

A transaction must change the database from one consistency state to another consistency state.

First review the definition of consistency. The so-called consistency means that the data is in a meaningful state, which is semantically rather thangrammatically. The most common example is money transfers. For example, if a sum of money is transferred from account A to account B, if the money in account A decreases but the money in account B does not increase, then we think that the data is in an inconsistent state at this time.

From the understanding of this paragraph, the so-called consistency means that from the actual business logic, the final result is correct and fully consistent with the programmer's expected result.

Isolation

The execution of a transaction cannot be interfered with by other transactions. That is, the operations and data used within a transaction are isolated from other concurrent transactions, and transactions executed concurrently cannot interfere with each other.

  • Here is the isolation level of the transaction:
    • Read uncommitted: read uncommitted
    • Read committed: read committed
    • Repeatable Read: repeatable read
    • Serializable

##Persistence(Durability)

Once a transaction Submit, his changes to the data in the database should be permanent. When the transaction is committed, the data will be persisted to the hard disk, and the modification will be permanent.

Transaction operations in Mysql

Transactions in MySQL are implicit transactions by default. When insert, update, and delete operations are performed, the database automatically starts the transaction, commits, or rolls back. affairs.

Whether to enable implicit transactions is controlled by the variable autocommit.

So transactions are divided into

Implicit transactions and Explicit transactions.

Implicit transactions

Transactions are automatically opened, submitted or rolled back, such as insert, update, delete statements. The opening, submission or rollback of transactions are automatically started, submitted or rolled back by mysql. controlling.

Check whether the variable autocommit is turned on for automatic submission

mysql> show variables like 'autocommit';+---------------+-------+| Variable_name | Value |+---------------+-------+| autocommit   | ON   |+---------------+-------+1 row in set, 1 warning (0.00 sec)
Copy after login
If autocommit is ON, it means that automatic submission is turned on.

Explicit transactions

Transactions need to be manually opened, submitted or rolled back, and are controlled by the developer themselves.

2 ways to manually control transactions:

Method 1:

Syntax:

//设置不自动提交事务set autocommit=0;//执行事务操作commit|rollback;
Copy after login
Example 1: Submit transaction operation, As follows:

mysql> create table test1 (a int);Query OK, 0 rows affected (0.01 sec)mysql> select * from test1;Empty set (0.00 sec)mysql> set autocommit=0;Query OK, 0 rows affected (0.00 sec)mysql> insert into test1 values(1);Query OK, 1 row affected (0.00 sec)mysql> commit;Query OK, 0 rows affected (0.00 sec)mysql> select * from test1;+------+| a   |+------+|   1 |+------+1 row in set (0.00 sec)
Copy after login
Example 2: Rollback transaction operation, as follows:

mysql> set autocommit=0;Query OK, 0 rows affected (0.00 sec)mysql> insert into test1 values(2);Query OK, 1 row affected (0.00 sec)mysql> rollback;Query OK, 0 rows affected (0.00 sec)mysql> select * from test1;+------+| a   |+------+|   1 |+------+1 row in set (0.00 sec)
Copy after login
You can see that the above data is rolled back.

We restore autocommit back:

mysql> set autocommit=1;Query OK, 0 rows affected (0.00 sec)
Copy after login

Method 2:

Syntax:

start transaction;//开启事务//执行事务操作commit|rollback;
Copy after login
Example 1: Submit transaction operation, as follows:

mysql> select * from test1;+------+| a   |+------+|   1 |+------+1 row in set (0.00 sec)mysql> start transaction;Query OK, 0 rows affected (0.00 sec)mysql> insert into test1 values (2);Query OK, 1 row affected (0.00 sec)mysql> insert into test1 values (3);Query OK, 1 row affected (0.00 sec)mysql> commit;Query OK, 0 rows affected (0.00 sec)mysql> select * from test1;+------+| a   |+------+|   1 ||   2 ||   3 |+------+3 rows in set (0.00 sec)
Copy after login

上面成功插入了2条数据。

示例2:回滚事务操作,如下:

mysql> select * from test1;+------+| a   |+------+|   1 ||   2 ||   3 |+------+3 rows in set (0.00 sec)mysql> start transaction;Query OK, 0 rows affected (0.00 sec)mysql> delete from test1;Query OK, 3 rows affected (0.00 sec)mysql> rollback;Query OK, 0 rows affected (0.00 sec)mysql> select * from test1;+------+| a   |+------+|   1 ||   2 ||   3 |+------+3 rows in set (0.00 sec)
Copy after login

上面事务中我们删除了test1的数据,显示删除了3行,最后回滚了事务。

savepoint关键字

在事务中我们执行了一大批操作,可能我们只想回滚部分数据,怎么做呢?

我们可以将一大批操作分为几个部分,然后指定回滚某个部分。可以使用savepoin来实现,效果如下:

先清除test1表数据:

mysql> delete from test1;Query OK, 3 rows affected (0.00 sec)mysql> select * from test1;Empty set (0.00 sec)
Copy after login

演示savepoint效果,认真看:

mysql> start transaction;Query OK, 0 rows affected (0.00 sec)mysql> insert into test1 values (1);Query OK, 1 row affected (0.00 sec)mysql> savepoint part1;//设置一个保存点Query OK, 0 rows affected (0.00 sec)mysql> insert into test1 values (2);Query OK, 1 row affected (0.00 sec)mysql> rollback to part1;//将savepint = part1的语句到当前语句之间所有的操作回滚Query OK, 0 rows affected (0.00 sec)mysql> commit;//提交事务Query OK, 0 rows affected (0.00 sec)mysql> select * from test1;+------+| a   |+------+|   1 |+------+1 row in set (0.00 sec)
Copy after login

从上面可以看出,执行了2次插入操作,最后只插入了1条数据。

savepoint需要结合rollback to sp1一起使用,可以将保存点sp1到rollback to之间的操作回滚掉。

只读事务

表示在事务中执行的是一些只读操作,如查询,但是不会做insert、update、delete操作,数据库内部对只读事务可能会有一些性能上的优化。

用法如下:

start transaction read only;
Copy after login

示例:

mysql> commit;Query OK, 0 rows affected (0.00 sec)mysql> start transaction read only;Query OK, 0 rows affected (0.00 sec)mysql> select * from test1;+------+| a   |+------+|   1 ||   1 |+------+2 rows in set (0.00 sec)mysql> delete from test1;ERROR 1792 (25006): Cannot execute statement in a READ ONLY transaction.mysql> commit;Query OK, 0 rows affected (0.00 sec)mysql> select * from test1;+------+| a   |+------+|   1 ||   1 |+------+2 rows in set (0.00 sec)
Copy after login

只读事务中执行delete会报错。

事务中的一些问题(重点)

这些问题主要是基于数据在多个事务中的可见性来说的。也是并发事务产生的问题。

更新丢失

丢失更新就是两个不同的事务(或者Java程序线程)在某一时刻对同一数据进行读取后,先后进行修改。导致第一次操作数据丢失。

第一类丢失更新 :A,B 事务同时操作同一数据,A先对改数据进行了更改,B再次更改时失败然后回滚,把A更新的数据也回滚了。(事务撤销造成的撤销丢失)

第二类丢失更新:A,B 事务同时操作同一数据,A先对改数据进行了更改,B再次更改并且提交,把A提交的数据给覆盖了。(事务提交造成的覆盖丢失)

脏读

一个事务在执行的过程中读取到了其他事务还没有提交的数据。 这个还是比较好理解的。

两个事务同时操作同一数据,A事务对该数据进行了修改还没提交的时候,B事务访问了该条事务,并且使用了该数据,此时A事务回滚,那么B事务读到的就是脏数据。

比如事务1,修改了某个数据 事务2,刚好访问了事务1修改后的数据

此时事务1,回滚了操作 事务2,读到还是回滚前的数据

读已提交

从字面上我们就可以理解,即一个事务操作过程中可以读取到其他事务已经提交的数据。

事务中的每次读取操作,读取到的都是数据库中其他事务已提交的最新的数据(相当于当前读)

不可重复读

在同一事务中,多次读取同一数据返回的结果有所不同,换句话说,后续读取可以读到另一事务已提交的更新数据。相反,“可重复读” 在同一事务中多次读取数据时, 能够保证所读数据一样, 也就是后续读取不能读到另一事务已提交的更新数据。

这种情况发生 在一个事务内多次读同一数据。A事务查询某条数据,该事务未结束时,B事务也访问同一数据并进行了修改。那么在A事务中的两 次读数据之间,由于第二个事务的修改,那么第一个事务两次读到的的数据可能是不一样的。

事务1,查询某个数据 事务2,修改了某个数据,提交

事务1,再次查询这个数据

这样事务1两次查询的数据不一样,称为不可重复读

可重复读

一个事务操作中对于一个读取操作不管多少次,读取到的结果都是一样的。

幻读

脏读、不可重复读、可重复读、幻读,其中最难理解的是幻读

以mysql为例:

  • 幻读现象例子:

    • 可重复读模式下,比如有个用户表,手机号码为主键,有两个事物进行如下操作
    • 事务A操作如下: 1、打开事务 2、查询号码为X的记录,不存在 3、插入号码为X的数据,插入报错(为什么会报错,先向下看) 4、查询号码为X的记录,发现还是不存在(由于是可重复读,所以读取记录X还是不存在的)
    • 事物B操作:在事务A第2步操作时插入了一条X的记录,所以会导致A中第3步插入报错(违反了唯一约束)
    • 上面操作对A来说就像发生了幻觉一样,明明查询X(A中第二步、第四步)不存在,但却无法插入成功
    • 幻读可以这么理解:事务中后面的操作(插入号码X)需要上面的读取操作(查询号码X的记录)提供支持,但读取操作却不能支持下面的操作时产生的错误,就像发生了幻觉一样。
  • 看第二种解释:

    • 事务A在操作一堆数据的时候,事务B插入了一条数据,A事务再次(第二次)查询,发现多了一条数据,像是幻觉。与不可重复读类似,不同的是一个是修改删除操作,一个是新增操作。

如果还是理解不了的,继续向下看,后面后详细的演示。

事务的隔离级别

当多个事务同时进行的时候,如何确保当前事务中数据的正确性,比如A、B两个事物同时进行的时候,A是否可以看到B已提交的数据或者B未提交的数据,这个需要依靠事务的隔离级别来保证,不同的隔离级别中所产生的效果是不一样的。

事务隔离级别主要是解决了上面多个事务之间数据可见性及数据正确性的问题。(或者说为了解决并发控制可能产生的异常问题,数据库定义了四种事务的隔离级别)

隔离级别分为4种:

  1. 读未提交:READ-UNCOMMITTED
  2. 读已提交:READ-COMMITTED
  3. 可重复读:REPEATABLE-READ
  4. 串行:SERIALIZABLE

上面4中隔离级别越来越强,会导致数据库的并发性也越来越低。

查看隔离级别

mysql> show variables like 'transaction_isolation';+-----------------------+----------------+| Variable_name     | Value      |+-----------------------+----------------+| transaction_isolation | READ-COMMITTED |+-----------------------+----------------+1 row in set, 1 warning (0.00 sec)
Copy after login
Copy after login

隔离级别的设置

分2步骤,修改文件、重启mysql,如下:

修改mysql中的my.init文件,我们将隔离级别设置为:READ-UNCOMMITTED,如下:

# 隔离级别设置,READ-UNCOMMITTED读未提交,READ-COMMITTED读已提交,REPEATABLE-READ可重复读,SERIALIZABLE串行transaction-isolation=READ-UNCOMMITTED
Copy after login
Copy after login

以管理员身份打开cmd窗口,重启mysql,如下:

C:\Windows\system32>net stop mysql
mysql 服务正在停止..mysql 服务已成功停止。

C:\Windows\system32>net start mysql
mysql 服务正在启动 .mysql 服务已经启动成功。
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

各种隔离级别中会出现的问题

隔离级别 脏读可能性 不可重复读可能性 幻读可能性
READ-UNCOMMITTED
READ-COMMITTED
REPEATABLE-READ
SERIALIZABLE

下面我们来演示一下,各种隔离级别中可见性的问题,开启两个窗口,叫做A、B窗口,两个窗口中登录mysql。

READ-UNCOMMITTED:读未提交

将隔离级别置为READ-UNCOMMITTED:

# 隔离级别设置,READ-UNCOMMITTED读未提交,READ-COMMITTED读已提交,REPEATABLE-READ可重复读,SERIALIZABLE串行transaction-isolation=READ-UNCOMMITTED
Copy after login
Copy after login

重启mysql:

C:\Windows\system32>net stop mysql
mysql 服务正在停止..mysql 服务已成功停止。

C:\Windows\system32>net start mysql
mysql 服务正在启动 .mysql 服务已经启动成功。
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

查看隔离级别:

mysql> show variables like 'transaction_isolation';+-----------------------+----------------+| Variable_name     | Value      |+-----------------------+----------------+| transaction_isolation | READ-UNCOMMITTED |+-----------------------+----------------+1 row in set, 1 warning (0.00 sec)
Copy after login

先清空test1表数据:

delete from test1;select * from test1;
Copy after login
Copy after login
Copy after login
Copy after login

按时间顺序在2个窗口中执行下面操作:

时间 窗口A 窗口B
T1 start transaction;
T2 select * from test1;
T3
start transaction;
T4
insert into test1 values (1);
T5
select * from test1;
T6 select * from test1;
T7
commit;
T8 commit;

A窗口如下:

mysql> start transaction;Query OK, 0 rows affected (0.00 sec)mysql> select * from test1;Empty set (0.00 sec)mysql> select * from test1;+------+| a   |+------+|   1 |+------+1 row in set (0.00 sec)mysql> commit;Query OK, 0 rows affected (0.00 sec)
Copy after login

B窗口如下:

mysql> start transaction;Query OK, 0 rows affected (0.00 sec)mysql> insert into test1 values (1);Query OK, 1 row affected (0.00 sec)mysql> select * from test1;+------+| a   |+------+|   1 |+------+1 row in set (0.00 sec)mysql> commit;Query OK, 0 rows affected (0.00 sec)
Copy after login
Copy after login

看一下:

T2-A:无数据,T6-A:有数据,T6时刻B还未提交,此时A已经看到了B插入的数据,说明出现了脏读

T2-A:无数据,T6-A:有数据,查询到的结果不一样,说明不可重复读

结论:读未提交情况下,可以读取到其他事务还未提交的数据,多次读取结果不一样,出现了脏读、不可重复读、幻读

READ-COMMITTED:读已提交

将隔离级别置为READ-COMMITTED

# 隔离级别设置,READ-UNCOMMITTED读未提交,READ-COMMITTED读已提交,REPEATABLE-READ可重复读,SERIALIZABLE串行transaction-isolation=READ-COMMITTED
Copy after login

重启mysql:

C:\Windows\system32>net stop mysql
mysql 服务正在停止..mysql 服务已成功停止。

C:\Windows\system32>net start mysql
mysql 服务正在启动 .mysql 服务已经启动成功。
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

查看隔离级别:

mysql> show variables like 'transaction_isolation';+-----------------------+----------------+| Variable_name     | Value      |+-----------------------+----------------+| transaction_isolation | READ-COMMITTED |+-----------------------+----------------+1 row in set, 1 warning (0.00 sec)
Copy after login
Copy after login

先清空test1表数据:

delete from test1;select * from test1;
Copy after login
Copy after login
Copy after login
Copy after login

按时间顺序在2个窗口中执行下面操作:

时间 窗口A 窗口B
T1 start transaction;
T2 select * from test1;
T3
start transaction;
T4
insert into test1 values (1);
T5
select * from test1;
T6 select * from test1;
T7
commit;
T8 select * from test1;
T9 commit;

A窗口如下:

mysql> start transaction;Query OK, 0 rows affected (0.00 sec)mysql> select * from test1;Empty set (0.00 sec)mysql> select * from test1;Empty set (0.00 sec)mysql> select * from test1;+------+| a   |+------+|   1 |+------+1 row in set (0.00 sec)mysql> commit;Query OK, 0 rows affected (0.00 sec)
Copy after login

B窗口如下:

mysql> start transaction;Query OK, 0 rows affected (0.00 sec)mysql> insert into test1 values (1);Query OK, 1 row affected (0.00 sec)mysql> select * from test1;+------+| a   |+------+|   1 |+------+1 row in set (0.00 sec)mysql> commit;Query OK, 0 rows affected (0.00 sec)
Copy after login
Copy after login

看一下:

T5-B:有数据,T6-A窗口:无数据,A看不到B的数据,说明没有脏读

T6-A窗口:无数据,T8-A:看到了B插入的数据,此时B已经提交了,A看到了B已提交的数据,说明可以读取到已提交的数据

T2-A、T6-A:无数据,T8-A:有数据,多次读取结果不一样,说明不可重复读

结论:读已提交情况下,无法读取到其他事务还未提交的数据,可以读取到其他事务已经提交的数据,多次读取结果不一样,未出现脏读,出现了读已提交、不可重复读、幻读

REPEATABLE-READ:可重复读

将隔离级别置为REPEATABLE-READ

# 隔离级别设置,READ-UNCOMMITTED读未提交,READ-COMMITTED读已提交,REPEATABLE-READ可重复读,SERIALIZABLE串行transaction-isolation=REPEATABLE-READ
Copy after login
Copy after login

重启mysql:

C:\Windows\system32>net stop mysql
mysql 服务正在停止..mysql 服务已成功停止。

C:\Windows\system32>net start mysql
mysql 服务正在启动 .mysql 服务已经启动成功。
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

查看隔离级别:

mysql> show variables like 'transaction_isolation';+-----------------------+----------------+| Variable_name     | Value      |+-----------------------+----------------+| transaction_isolation | REPEATABLE-READ |+-----------------------+----------------+1 row in set, 1 warning (0.00 sec)
Copy after login
Copy after login

先清空test1表数据:

delete from test1;select * from test1;
Copy after login
Copy after login
Copy after login
Copy after login

按时间顺序在2个窗口中执行下面操作:

时间 窗口A 窗口B
T1 start transaction;
T2 select * from test1;
T3
start transaction;
T4
insert into test1 values (1);
T5
select * from test1;
T6 select * from test1;
T7
commit;
T8 select * from test1;
T9 commit;
T10 select * from test1;

A窗口如下:

mysql> start transaction;Query OK, 0 rows affected (0.00 sec)mysql> select * from test1;Empty set (0.00 sec)mysql> select * from test1;Empty set (0.00 sec)mysql> select * from test1;Empty set (0.00 sec)mysql> commit;Query OK, 0 rows affected (0.00 sec)mysql> select * from test1;+------+| a   |+------+|   1 ||   1 |+------+2 rows in set (0.00 sec)
Copy after login

B窗口如下:

mysql> start transaction;Query OK, 0 rows affected (0.00 sec)mysql> insert into test1 values (1);Query OK, 1 row affected (0.00 sec)mysql> select * from test1;+------+| a   |+------+|   1 ||   1 |+------+2 rows in set (0.00 sec)mysql> commit;Query OK, 0 rows affected (0.00 sec)
Copy after login

看一下:

T2-A、T6-A窗口:无数据,T5-B:有数据,A看不到B的数据,说明没有脏读

T8-A:无数据,此时B已经提交了,A看不到B已提交的数据,A中3次读的结果一样都是没有数据的,说明可重复读

结论:可重复读情况下,未出现脏读,未读取到其他事务已提交的数据,多次读取结果一致,即可重复读。

幻读演示

将隔离级别置为REPEATABLE-READ

# 隔离级别设置,READ-UNCOMMITTED读未提交,READ-COMMITTED读已提交,REPEATABLE-READ可重复读,SERIALIZABLE串行transaction-isolation=REPEATABLE-READ
Copy after login
Copy after login

重启mysql:

C:\Windows\system32>net stop mysql
mysql 服务正在停止..mysql 服务已成功停止。

C:\Windows\system32>net start mysql
mysql 服务正在启动 .mysql 服务已经启动成功。
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

查看隔离级别:

mysql> show variables like 'transaction_isolation';+-----------------------+----------------+| Variable_name     | Value      |+-----------------------+----------------+| transaction_isolation | REPEATABLE-READ |+-----------------------+----------------+1 row in set, 1 warning (0.00 sec)
Copy after login
Copy after login

准备数据:

mysql> create table t_user(id int primary key,name varchar(16) unique key);Query OK, 0 rows affected (0.01 sec)mysql> insert into t_user values (1,'路人甲Java'),(2,'路人甲Java');ERROR 1062 (23000): Duplicate entry '路人甲Java' ***\*for\**** key 'name'mysql> select * from t_user;Empty set (0.00 sec)
Copy after login

上面我们创建t_user表,name添加了唯一约束,表示name不能重复,否则报错。

按时间顺序在2个窗口中执行下面操作:

时间 窗口A 窗口B
T1 start transaction;
T2
start transaction;
T3
– 插入路人甲Java
insert into t_user values (1,‘路人甲Java’);
T4
select * from t_user;
T5 – 查看路人甲Java是否存在
select * from t_user where name=‘路人甲Java’;

T6
commit;
T7 – 插入路人甲Java
insert into t_user values (2,‘路人甲Java’);

T8 – 查看路人甲Java是否存在
select * from t_user where name=‘路人甲Java’;

T9 commit;

A窗口如下:

mysql> start transaction;Query OK, 0 rows affected (0.00 sec)mysql> select * from t_user where name='路人甲Java';Empty set (0.00 sec)mysql> insert into t_user values (2,'路人甲Java');ERROR 1062 (23000): Duplicate entry '路人甲Java' ***\*for\**** key 'name'mysql> select * from t_user where name='路人甲Java';Empty set (0.00 sec)mysql> commit;Query OK, 0 rows affected (0.00 sec)
Copy after login

B窗口如下:

mysql> start transaction;Query OK, 0 rows affected (0.00 sec)mysql> insert into t_user values (1,'路人甲Java');Query OK, 1 row affected (0.00 sec)mysql> select * from t_user;+----+---------------+| id | name      |+----+---------------+|  1 | 路人甲Java   |+----+---------------+1 row in set (0.00 sec)mysql> commit;Query OK, 0 rows affected (0.00 sec)
Copy after login

看一下:

A想插入数据路人甲Java,插入之前先查询了一下(T5时刻)该用户是否存在,发现不存在,然后在T7时刻执行插入,报错了,报数据已经存在了,因为T6时刻B已经插入了路人甲Java。

然后A有点郁闷,刚才查的时候不存在的,然后A不相信自己的眼睛,又去查一次(T8时刻),发现路人甲Java还是不存在的。

此时A心里想:数据明明不存在啊,为什么无法插入呢?这不是懵逼了么,A觉得如同发生了幻觉一样。

SERIALIZABLE:串行

SERIALIZABLE会让并发的事务串行执行(多个事务之间读写、写读、写写会产生互斥,效果就是串行执行,多个事务之间的读读不会产生互斥)。

读写互斥:事务A中先读取操作,事务B发起写入操作,事务A中的读取会导致事务B中的写入处于等待状态,直到A事务完成为止。

表示我开启一个事务,为了保证事务中不会出现上面说的问题(脏读、不可重复读、读已提交、幻读),那么我读取的时候,其他事务有修改数据的操作需要排队等待,等待我读取完成之后,他们才可以继续。

写读、写写也是互斥的,读写互斥类似。

这个类似于java中的java.util.concurrent.lock.ReentrantReadWriteLock类产生的效果。

下面演示读写互斥的效果。

将隔离级别置为SERIALIZABLE

# 隔离级别设置,READ-UNCOMMITTED读未提交,READ-COMMITTED读已提交,REPEATABLE-READ可重复读,SERIALIZABLE串行transaction-isolation=SERIALIZABLE
Copy after login

重启mysql:

C:\Windows\system32>net stop mysql
mysql 服务正在停止..mysql 服务已成功停止。

C:\Windows\system32>net start mysql
mysql 服务正在启动 .mysql 服务已经启动成功。
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

查看隔离级别:

mysql> show variables like 'transaction_isolation';+-----------------------+--------------+| Variable_name     | Value     |+-----------------------+--------------+| transaction_isolation | SERIALIZABLE |+-----------------------+--------------+1 row in set, 1 warning (0.00 sec)
Copy after login

先清空test1表数据:

delete from test1;select * from test1;
Copy after login
Copy after login
Copy after login
Copy after login

按时间顺序在2个窗口中执行下面操作:

时间 窗口A 窗口B
T1 start transaction;
T2 select * from test1;
T3
start transaction;
T4
insert into test1 values (1);
T5 commit;
T6
commit;

Run the above commands in chronological order and you will find that T4-B will be blocked until T5-A is completed.

The above demonstration is the effect of read-write mutual exclusion. You can write about the effect of write-read and write-write mutual exclusion yourself.

It can be seen that transactions can only be executed serially. There are no problems with dirty reads, non-repeatable reads, and phantom reads in serial situations.

Summary

  • Read Uncommitted(Read Uncommitted)

    • Read Uncommitted is the isolation level The lowest transaction level. Under this isolation level, one transaction will read updated but uncommitted data from another transaction. If the other transaction rolls back, the data read by the current transaction will be dirty data. This is called dirty read.
  • Read Committed

    • Under the Read Committed isolation level, a transaction may encounter non-repeatable reads (Non Repeatable Read) question. Non-repeatable reading means reading the same data multiple times within a transaction. If another transaction happens to modify this data before the transaction ends, then in the first transaction, the data read twice It may be inconsistent.
  • Repeatable Read

    • Under the Repeatable Read isolation level, a transaction may encounter phantom read (Phantom Read) The problem. Phantom reading means that in a transaction, the first time you query a certain record, you find that it does not exist. However, when you try to update this non-existent record, you can actually succeed, and when you read the same record again, it works magically. appeared. Phantom reading is a record that has not been read and is thought not to exist, but in fact it can be updated successfully, and after the update is successful, it will appear when it is read again.
  • Serializable

    • Serializable is the strictest isolation level. Under the Serializable isolation level, all transactions are executed in sequence, so dirty reads, non-repeatable reads, and phantom reads will not occur.
    • Although transactions under the Serializable isolation level have the highest security, because the transactions are executed serially, the efficiency will be greatly reduced and the performance of the application will be drastically reduced. If there is no particularly important situation, the Serializable isolation level is generally not used.

Default isolation level: If no isolation level is specified, the database will use the default isolation level. In MySQL, if you use InnoDB, the default isolation level is Repeatable Read.

About the choice of isolation level

  1. You need to have a good understanding of the phenomena caused by various isolation levels, and then you can make the choice with ease
  2. The higher the isolation level, the lower the concurrency. For example, the highest level SERIALIZABLE will cause things to be executed serially, and concurrent operations will become serial, which will directly reduce system performance.
  3. The specific choice needs to be based on the specific business.
  4. READ-COMMITTED is usually used more often.

Summary

  1. Understand the 4 characteristics of transactions: atomicity, consistency, isolation, and durability
  2. Master Introduction to common commands for transaction operations
  3. set autocommit can set whether to turn on automatic transaction submission
  4. start transaction: turn on the transaction
  5. start transaction read only: turn on the read-only transaction
  6. commit: Commit the transaction
  7. rollback: Roll back the transaction
  8. savepoint: Set the save point
  9. rollback to save point: You can roll back to a save point
  10. Master the 4 isolation levels and understand their characteristics
  11. Dirty read, non-repeatable read, phantom read

[Related recommendations: mysql video tutorial

The above is the detailed content of what is mysql transaction. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template