更新:现在有SKIP LOCKED
MysqlNOWAIT
和 Postgres。
下面是老问题。
我希望并发事务从表中选择一行,将其标记为“脏”,以便其他事务无法选择它,然后执行其余的事务。
我在使用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)