I am learning transactions in mysql. I cannot understand behaviour. Isolation level for my db is repeatable-read. Database schema:
creade database my_db;
use my_db;
create table locktest(
id integer,
val integer,
primary key (id)
);
insert into locktest (id, val) values (3,6);
I created two users. User1 does next:
start transaction;
select * from locktest where id = 3;
get id = 3, val = 6;
Then, user2 updates:
update locktest set val = 7 where id = 3;
select * from locktest where id = 3;
result is id = 3, val = 7;
user1 selects:
select * from locktest where id = 3;
id = 3, val = 7
So, my question why can user2 update the table, should not be it locked by user1? And why in last select from user1 it gets updated values, should not be it consistent according to repeatable read isolation level ?
Do you need innodb, see this answer .
You should enclose your SQL operation into transactions to ensure isolation, its enough to enclose User 1
operations for your tests. Remember to execute commands in two separate sessions, for example opening MySQL tool for twice, you can't test this with phpmyadmin:
User 1: User 2:
SET SESSION TRANSACTION
ISOLATION LEVEL
REPEATABLE READ;
START TRANSACTION; START TRANSACTION;
select *
from locktest where id = 3;
update locktest
set val = 7 where id = 3;
select * from locktest
where id = 3;
select *
from locktest where id = 3;
--Result must be the same than
--previus query.
COMMIT;
COMMIT;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.