In the case of the transaction isolation level Read Committed:
Transaction 2 does not perform a commit and can read the newly added record to transaction 1. A phantom read is shown here.
When the transaction isolation level is changed to Repeatable-read, the ability to resolve the problem is to resolve the Phantom read.
Phantom Read definition: A transaction re-reads the previously retrieved data according to the same query criteria, but finds that other transactions have inserted new data that satisfies its query criteria.
Session1 Execution Transaction 1: Insert and commit only
Mysql> SELECT @ @global. tx_isolation;select @ @tx_isolation;
+-----------------------+
| @ @global. tx_isolation |
+-----------------------+
| read-committed |
+-----------------------+
1 row in Set (0.01 sec)
+----------------+
| @ @tx_isolation |
+----------------+
| read-committed |
+----------------+
1 row in Set (0.00 sec)
mysql> start transaction;
Query OK, 0 rows Affected (0.00 sec)
Mysql> select * from Sun;
+--------+------+
| name | Age |
+--------+------+
| Summer | 26 |
+--------+------+
1 row in Set (0.00 sec)
Mysql> INSERT into sun values (' AA ', ' 88 ');
Query OK, 1 row Affected (0.00 sec)
Mysql> commit;
session2 execution Transaction 2: Query only
Mysql> SELECT @ @global. tx_isolation;select @ @tx_isolation;
+-----------------------+
| @ @global. tx_isolation |
+-----------------------+
| read-committed |
+-----------------------+
1 row in Set (0.00 sec)
+----------------+
| @ @tx_isolation |
+----------------+
| read-committed |
+----------------+
1 row in Set (0.00 sec)
mysql> start transaction;
Query OK, 0 rows Affected (0.00 sec)
Mysql> select * from Sun;
+--------+------+
| name | Age |
+--------+------+
| Summer | 26 |
| AA | 88 |
+--------+------+
MYSQL5.7 Read COMMITTED transaction ISOLATION LEVEL study-phantom read occurs