0

I am at the REPEATABLE-READ level.

Why does it make me wait?

I understand that all reads (SELECTs) at any level are non-blocking.

what am I missing?

Session 1:

mysql> lock tables users write;
Query OK, 0 rows affected (0.00 sec)

Session 2:

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from users where id = 1; // wait

Session 1:

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

Session 2:

mysql> select * from users where id = 1;
+----+-----------------+--------------------+------+---------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+------------+
| id | name            | email              | rol  | email_verified_at   | password                                                     | remember_token | created_at          | updated_at          | deleted_at |
+----+-----------------+--------------------+------+---------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+------------+
|  1 | Bella Lueilwitz | orlo19@example.com | NULL | 2022-08-01 17:22:29 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | MvMlaX9TQj     | 2022-08-01 17:22:29 | 2022-08-01 17:22:29 | NULL       |
+----+-----------------+--------------------+------+---------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+------------+
1 row in set (10.51 sec)

In this question the opposite is true

Why doesn't LOCK TABLES [table] WRITE prevent table reads?

  • 1
    https://dev.mysql.com/doc/refman/8.0/en/lock-tables.html : WRITE lock: ... Only the session that holds the lock can access the table. No other session can access it until the lock is released. – Akina Oct 27 '22 at 12:44
  • hi @Akina non-blocking queries do not work in this case because they are ONLY INNODB and LOCK TABLES is from the MySQL layer I guess, right? –  Oct 27 '22 at 13:10
  • LOCK acts independently of the engines. – Akina Oct 27 '22 at 13:25
  • `LOCK TABLES` is simply something not to use with InnoDB. Why are you using it? – Rick James Oct 27 '22 at 17:46
  • InnoDB has "transactions" to take care of virtually all situations where MyISAM needed `LOCK TABLES`. – Rick James Oct 27 '22 at 17:51

1 Answers1

1

You reference a question about MySQL 5.0 posted in 2013. The answer from that time suggests that the client was allowed to get a result that had been cached in the query cache. Since then, MySQL 5.6 and 5.7 disabled the query cache by default, and MySQL 8.0 removed the feature altogether. This is a good thing.

The documentation says:

WRITE lock:

  • Only the session that holds the lock can access the table. No other session can access it until the lock is released.

This was true in the MySQL 5.0 days too, but the query cache allowed some clients to get around it. But I guess it wasn't reliable even then, because if the client ran a query that happened not to be cached, I suppose it would revert to the documented behavior. Anyway, it's moot, because all currently supported versions of MySQL should have the query cache disabled or removed.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • Excellent @Bill Karwin the truth is I didn't know that about the cache nor that it had "direct" relation with this. In my humble opinion, I'm with you, it's better to LOCK and no cache, I think it's even more LOGICAL to LOCK, +1 and marked as a useful answer. –  Oct 27 '22 at 14:26
  • one last question @Bill Karwin, when we apply a LOCK TABLES table WRITE/READ, can we imagine (a mental model) applying a shared/exclusive lock (as appropriate) to each row of the table? –  Oct 27 '22 at 14:44
  • 1
    If it helps you to think of it that way, okay, but I'm not sure why you need to. In fact, it is not a lock on every row, it's a lock at the table level. – Bill Karwin Oct 27 '22 at 15:48
  • "In fact, it is not a lock on every row, it's a lock at the table level" - great, this helped me to understand it better, thanks @Bill Karwin, +1 –  Oct 27 '22 at 16:15