MySQL isolation level detailed explanation and examples

MySQL isolation level detailed explanation and examples

I just know the isolation level of things, but I have never operated it once.

Dirty Read: A transaction has updated a piece of data, and another transaction reads the same data at this time. For some reason, the previous transaction RollBacks the operation, so the data read by the latter transaction will be incorrect.

Non-repeatable read: The data is inconsistent between two queries in a transaction. This may be because a transaction updated the original data between the two queries.

Phantom Read: The number of data entries in two queries of a transaction is inconsistent. For example, one transaction queries several columns of data, while another transaction inserts several new columns of data at this time. In the next query, the previous transaction will find several columns of data that it did not have before.

4 isolation levels of MySQL

read uncommitted : read data that has not been committed: neither problem is solved
Read committed: Read committed data: Can solve dirty reads ---- Oracle default
Repeatable read: Reread read: can solve dirty read and non-repeatable read - MySQL default
serializable: Serialization: can solve dirty reads, non-repeatable reads and virtual reads - equivalent to locking the table

Transaction Isolation Level Dirty Read Non-repeatable read Phantom Read
read-uncommitted yes yes yes
Non-repeatable read (read-committed) no yes yes
Repeatable-read no no yes
serializable no no no

Create a data table:

create table shuzhi
(
  id mediumint(8) primary key,
  name varchar(30),
  shuzhi mediumint(10)
);
alter table shuzhi engine=innodb;
insert into shuzhi values(1,'aa',1000);
insert into shuzhi values(2,'bb',2000);
insert into shuzhi values(3,'cc',3000);
insert into shuzhi values(4,'dd',4000);
insert into shuzhi values(5,'ee',5000);
insert into shuzhi values(6,'ff',6000);
insert into shuzhi values(7,'gg',7000);
insert into shuzhi values(8,'hh',8000);

Start testing the four isolation levels of transactions. The first one: read uncommitted

Setting the isolation level

#Query the current isolation level SELECT @@tx_isolation
#Set the isolation level set session transaction isolation level [isolation level]
set session transaction isolation level read uncommitted

Start the first process first and start the transaction without querying it yet

Window 1
start transaction;

Then open the second process (terminal)
First open the transaction to modify a record without committing

Window 2
start transaction;
update shuzhi set shuzhi='8888' where id=7;

Go to the database to query and find that the value of id=7 is still 7000 and the value has not changed

insert image description here

Go to window 1 to query this record

Window 1
start transaction;
select * from shuzhi where id=7

insert image description here

It is found that the data read is the data submitted in window 2, not 7000

This is the end of this article about MySQL isolation level details and examples. For more information about MySQL isolation level, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • In-depth understanding of the four isolation levels of MySQL
  • Detailed explanation of the four transaction isolation levels in MySQL
  • MySQL detailed explanation of isolation level operation process (cmd)
  • Analyzing the four transaction isolation levels in MySQL through examples
  • Mysql transaction isolation level principle example analysis
  • Detailed explanation of the implementation principle of transaction isolation level in MySQL

<<:  Select does not support double click dbclick event

>>:  CSS container background 10 color gradient Demo (linear-gradient())

Recommend

A complete example of implementing a timed crawler with Nodejs

Table of contents Cause of the incident Use Node ...

Analyze how to automatically generate Vue component documentation

Table of contents 1. Current situation 2. Communi...

A guide to writing flexible, stable, high-quality HTML and CSS code standards

The Golden Rule Always follow the same set of cod...

How to preview pdf file using pdfjs in vue

Table of contents Preface think Library directory...

my.cnf parameter configuration to optimize InnoDB engine performance

I have read countless my.cnf configurations on th...

How to stop CSS animation midway and maintain the posture

Preface I once encountered a difficult problem. I...

Linux kernel device driver memory management notes

/********************** * Linux memory management...

Summary of Vue watch monitoring methods

Table of contents 1. The role of watch in vue is ...

Vue implements multi-grid input box on mobile terminal

Recently, the company has put forward a requireme...

UDP connection object principle analysis and usage examples

I wrote a simple UDP server and client example be...

Vue mobile terminal determines the direction of finger sliding on the screen

The vue mobile terminal determines the direction ...

mysql workbench installation and configuration tutorial under centOS

This article shares the MySQL Workbench installat...

Learn asynchronous programming in nodejs in one article

Table of Contents Introduction Synchronous Asynch...