MySQL可以由于连续的共享锁而锁定锁定吗?

发布于 2025-01-26 07:45:48 字数 284 浏览 4 评论 0原文

我有2个不同的交易,其中一个正在使用读取锁(共享 )的选择语句,而另一种用途写锁( for Update )。

假设他们正在尝试在同一行上获取锁。这是我试图理解正在发生的事情的情况。

假设我使用读取锁有连续的请求流,偶尔需要获取写锁。

这些锁是否使用FIFO策略来避免饥饿或其他一些策略(例如阅读锁),只要它可以获取锁定和写锁定锁会等待所有读数(即使在这种情况下是新的)。

我怀疑第二次可能发生,但我不确定。

我正在调查一个问题,但找不到有关此问题的好文件。

I have 2 different transactions where one is using read locks (FOR SHARE) for SELECT statements and the other uses write locks (FOR UPDATE).

Let's say they are trying to acquire the lock on the same row. Here's the scenario I'm trying to understand what's happening.

Let's say I have continuous stream of requests using the read locks and occasionally I need to acquire the write lock.

Are these locks using FIFO strategy to avoid starvation or some other strategy such as read locks would be acquired as long as it can acquire the lock and write lock would wait all the reads to drain (even the new ones in this case).

I'm suspecting 2nd might be happening but I'm not 100% sure.

I'm investigating an issue and couldn't find a good documentation about this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多彩岁月 2025-02-02 07:45:48

如果您缺乏文档,则可以尝试一个实验:

窗口1:

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

mysql> select * from tablename for share;
+---------------------+
| ldt                 |
+---------------------+
| 1969-12-31 16:00:00 |
+---------------------+
1 row in set (0.00 sec)

窗口2:

mysql> update tablename set ldt=now();
(hangs, waiting for lock)

窗口3:

mysql> select * from tablename for share;
(hangs, also waiting for lock)

这表明X-Lock请求阻止了后续的S锁请求。

50秒通过,然后是:

窗口2:

ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

然后立即:

窗口3:

mysql> select * from tablename for share;
+---------------------+
| ldt                 |
+---------------------+
| 1969-12-31 16:00:00 |
+---------------------+
1 row in set (41.14 sec)

窗口3中的选择在等待窗口2中的更新时被阻止。当更新时间时,窗口3中的选择可以继续进行。

If you lack documentation, you can try an experiment:

Window 1:

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

mysql> select * from tablename for share;
+---------------------+
| ldt                 |
+---------------------+
| 1969-12-31 16:00:00 |
+---------------------+
1 row in set (0.00 sec)

Window 2:

mysql> update tablename set ldt=now();
(hangs, waiting for lock)

Window 3:

mysql> select * from tablename for share;
(hangs, also waiting for lock)

This indicates that the X-lock request is blocking subsequent S-lock requests.

50 seconds passes, and then:

Window 2:

ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

And then immediately:

Window 3:

mysql> select * from tablename for share;
+---------------------+
| ldt                 |
+---------------------+
| 1969-12-31 16:00:00 |
+---------------------+
1 row in set (41.14 sec)

The select in window 3 was blocked while waiting for the update in window 2. When the update timed out, then the select in window 3 was able to proceed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文