2

My table's definition is

CREATE TABLE auto_inc (

id int(11) NOT NULL AUTO_INCREMENT,

PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=latin1

At first there are four rows:

| id |

| 1 |

| 2 |

| 3 |

| 4 |

I opened session 1 and executed

#session 1
set transaction isolation level REPEATABLE READ
start transaction;
select * from auto_inc

return four rows 1,2,3,4.And then I opened another session 2 and executed

#session 2
insert into auto_inc(`id`) values(null)

and insert success.Back to session 1 I executed

#session 1
select * from auto_inc;#command 1
select * from auto_inc for update;#command 2

command 1 return four rows 1,2,3,4.But command 2 return 1,2,3,4,5.Could anyone gives me some clues why command 2 will see session 2's insertion? Thanks in advance!

1
  • As "undo area" is not allowed to be shared-locked. So, command 2 retrieves fresh data ignoring snapshots causing "phantom read". Commented Oct 27, 2020 at 16:04

1 Answer 1

2
  1. why session 2 can insert new data ?

under REPEATABLE READ the second SELECT is guaranteed to see the rows that has seen at first select unchanged. New rows may be added by a concurrent transaction, but the existing rows cannot be deleted nor changed.

https://stackoverflow.com/a/4036063/3020810

  1. why session 1 can see the insertion?

under REPEATABLE READ, Consistent reads within the same transaction read the snapshot established by the first read.If you want to see the “freshest” state of the database, use either the READ COMMITTED isolation level or a locking read, and a select ... for update is a locking read.

Consistent Nonlocking Reads: https://dev.mysql.com/doc/refman/5.6/en/innodb-consistent-read.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.