mysql different isolation level transactions insert data


foreword

Relevant questions were asked during the interview and made a record of the questions

1. Problems

The problem is this. Under the default isolation level of the database, transaction A inserts a piece of data without committing, transaction B inserts a piece of data, submits, and the table id is auto-incremented. If the table is empty, what are the ids of transaction A and transaction B? .

2. Test

2.1. RR level, mysql default level

2.1.1 Open a mysql window, transaction A

execute code block

start transaction;
insert into a(age) value(1);

2.1.2 Open another window, transaction B

implement

start transaction;
insert into a(age) value(2);
commit;

View the results as follows
insert image description here

2.1.3 Submit transaction A, and then look at the results as follows

insert image description here
It can be seen that the id of transaction A is 1, and the id of transaction B is 2, which has nothing to do with the commit.

2.2 Test under READ-COMMITTED level

2.2.1 Modify the my.ini file of the database to modify the isolation level

transaction-isolation=READ-COMMITTED

restart mysql

2.2.2 Check whether the isolation level has been modified

View isolation level commands

show variables like 'transaction_isolation';

insert image description here
It can be seen that it has taken effect.

2.2.3 Start transaction A

start transaction;
insert into a(age) value(3);

2.2.4 Start transaction B

start transaction;
insert into a(age) value(4);
commit;

2.2.5 View Results

insert image description here

2.2.6 commit transaction A, and then view the results

insert image description here
You can see that the rc level is the same as the rr level.

2.3 Test under READ-UNCOMMITTED

2.3.1 The code is as follows

Transaction A

start transaction;
insert into a(age) value(5);

Transaction B

start transaction;
insert into a(age) value(6);
commit;

2.3.2 View Results

insert image description here
It can be seen that all have been inserted, and the commit transaction A is the same, and the order is from transaction A to transaction B, and the id increases.

2.4 Test under SERIALIZABLE

2.4.1 Code

Transaction A

start transaction;
insert into a(age) value(7);

transaction

start transaction;
insert into a(age) value(8);
commit;

2.4.2 View Results

insert image description here
After committing transaction A and looking at the results,
insert image description here
you can see that the id of the transaction that started first is incremented by one.

Summarize

1. MySQL's database self-increment id has nothing to do with the isolation level of the database. As long as the transaction is started, the id will increase by 1, and subsequent transactions will be executed on the original basis.
2. The former oracle colleague's test is different from mysql. Whoever submits first will add 1. I haven't tested this. If you are just interested, you can test it yourself.

Guess you like

Origin blog.csdn.net/qq_34526237/article/details/131465763