【发布时间】:2012-10-06 09:55:05
【问题描述】:
更新:现在有用于 Mysql 和 Postgres 的 SKIP LOCKED 和 NOWAIT。
下面是老问题。
我希望并发事务从表中选择一行,将其标记为“脏”,以便其他事务无法选择它,然后执行其余事务。
我在为此目的使用select... for update 时遇到了麻烦,因为第二笔交易也是如此。请提供一个选择不同行的不同事务的最小示例。
我的数据是:
mysql> select * from SolrCoresPreallocated;
+----+-------------+-----+-----+
| id | used_status | sid | cid |
+----+-------------+-----+-----+
| 1 | 0 | 0 | 400 |
| 2 | 0 | 0 | 401 |
| 3 | 0 | 0 | 402 |
| 4 | 0 | 0 | 403 |
| 5 | 0 | 0 | 404 |
| 6 | 0 | 0 | 405 |
+----+-------------+-----+-----+
6 rows in set (0.00 sec)
而且这些东西没有按预期工作:
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from SolrCoresPreallocated order by id limit 1 for update;
+----+-------------+-----+-----+
| id | used_status | sid | cid |
+----+-------------+-----+-----+
| 1 | 0 | 0 | 400 |
+----+-------------+-----+-----+
1 row in set (0.00 sec)
...set the used_status to 1
...perform the rest of the operations
...作为以后的第二笔交易
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from SolrCoresPreallocated order by id limit 1 for update;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)
【问题讨论】:
-
它到底做了什么你没想到的?
标签: mysql