1
votes

The default isolation level of MySQL transaction is 'Repeatable Read'.

According to another stackoverflow question( Difference between read commit and repeatable read) "Repeatable read is a higher isolation level, that in addition to the guarantees of the read committed level, it also guarantees that any data read cannot change, if the transaction reads the same data again, it will find the previously read data in place, unchanged, and available to read."

Here is my test database;

mysql> select * from people;
+------+---------+
| name | howmany |
+------+---------+
| alex |     100 |
| bob  |     100 |
+------+---------+

slow.sql

START TRANSACTION;

SELECT @new_val := howmany FROM people WHERE name = 'alex';
SELECT SLEEP(10);
SET @new_val = @new_val - 5;
UPDATE people SET howmany = @new_val WHERE name = 'alex';

COMMIT;

fast.sql

START TRANSACTION;

SELECT @new_val := howmany FROM people WHERE name = 'alex';
--  SELECT SLEEP(10);
SET @new_val = @new_val - 5;
UPDATE people SET howmany = @new_val WHERE name = 'alex';

COMMIT;

If I run slow.sql, and before it returns I run fast.sql multiple times. fast.sql will print 95, 90, 85....

I think repeatable read isolation level should make fast.sql fail to run or I misunderstand 'repeatable read'.

I'm running MySQL 5.7 from Ubuntu 16.10.

Thanks very much.

3

3 Answers

1
votes

Repeatable read isolation level guarantees consistency within a single transaction. You are executing multiple transactions. For the behaviour your expecting you would need to look into locking reads. See here for more info. https://dev.mysql.com/doc/refman/5.7/en/innodb-transaction-isolation-levels.html#isolevel_repeatable-read

2
votes

If not wrong then Repeatable Read talks about consistent reads within the same transaction and not with other transaction. From MySQL Documentation

  • REPEATABLE READ

This is the default isolation level for InnoDB. Consistent reads within the same transaction read the snapshot established by the first read. This means that if you issue several plain (nonlocking) SELECT statements within the same transaction, these SELECT statements are consistent also with respect to each other.

0
votes

If some data changes during your transaction by process outside of it, it won't have any effect on data read in said transaction.

I don't see how fast.sql would fail to run because of this isolation level (or any isolation).