MySQL read commited and repeatable read reason

Source: Internet
Author: User

Read commited and Repeatable read

On the question of consistency reading. Refer to MySQL technology insider--INNODB storage Engine

Session 1

Mysql> begin;

Query OK, 0 rows Affected (0.00 sec)

Mysql> select * from T2;

+------+

| ID |

+------+

| 1 |

+------+

1 row in Set (0.00 sec)

At the same time to Session2:

Mysql>

Mysql>

Mysql> begin;

Query OK, 0 rows Affected (0.00 sec)

Mysql> select * from T2;

+------+

| ID |

+------+

| 1 |

+------+

1 row in Set (0.00 sec)

mysql> update T2 set id=2;

Query OK, 1 row Affected (0.00 sec)

Rows matched:1 changed:1 warnings:0

Mysql>

Mysql> select * from T2;

+------+

| ID |

+------+

| 2 |

+------+

1 row in Set (0.00 sec)

Mysql> commit;

Query OK, 0 rows affected (0.01 sec)

And back to Session1:

If it is

Mysql> SELECT @ @tx_isolation;

+----------------+

| @ @tx_isolation |

+----------------+

| read-committed |

+----------------+

1 row in Set (0.00 sec)

Mysql> select * from T2;

+------+

| ID |

+------+

| 2 |

+------+

1 row in Set (0.00 sec)

In Session1 of the same thing, two queries T2, you will see the results,

If it is:

Mysql> SELECT @ @tx_isolation;

+-----------------+

| @ @tx_isolation |

+-----------------+

| Repeatable-read |

+-----------------+

1 row in Set (0.00 sec)

In Session 1, you see

Mysql> begin;

Query OK, 0 rows Affected (0.00 sec)

Mysql> select * from T2;

+------+

| ID |

+------+

| 2 |

+------+

1 row in Set (0.00 sec)

Mysql> select * from T2;

+------+

| ID |

+------+

| 2 |

+------+

1 row in Set (0.00 sec)

is the same result.

This is due to MySQL's consistent non-locking read.

Read operations do not wait for a row to be locked if the read row is in preparation for another thing to perform an update or delete operation

Release, InnoDB go back and read a snapshot of the data. Because there is no need to wait for access to the X lock on the line, it is called non-locking read.

Snapshot data refers to the previous version of the data (there may be multiple versions) of the row. is achieved by Undo, and Undo is

To roll back data in things, so there is no additional overhead for snapshot data.

In repeatable read, the snapshot reads always read the row version data at the beginning of the thing, and in read commited,

is to read the latest snapshot, so a query will see no results.


MySQL read commited and repeatable read reason

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.