MySQL Forums
Forum List  »  InnoDB

Minimal example of using select… for update to isolate rows
Posted by: Jesvin Vattamattom
Date: October 06, 2012 08:46AM

I want concurrent transactions to select the next "free" row from a table, marking it as "dirty" so that other transactions **cannot select it**. For example, I want a transaction T1 to select row 1 and while it continues, T2 must select row 2 and proceed. It must not select row 1 as it is dirty.

I had trouble using `select... for update` as the second transaction causes a lock wait as it contends for same row.

Please provide a minimal example for **different transactions to select distinct rows**.

--------------------
Here is my failed approach:

My data is:

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)

And T1 gets it's read:

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)


...but T2 fails, it gives me a lock wait timeout. How do I make it select the next free row cleanly?

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)

Options: ReplyQuote


Subject
Views
Written By
Posted
Minimal example of using select… for update to isolate rows
2819
October 06, 2012 08:46AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.