Ignoring locked row in a MySQL query

前端 未结 2 1800
暗喜
暗喜 2020-12-10 07:26

I have one table that is read at the same time by different threads.

Each thread must select 100 rows, execute some tasks on each row (unrelated to the database) th

2条回答
  •  悲&欢浪女
    2020-12-10 08:18

    Even though it is not the best solution, as there is no way that I know to ignore locked rows, I select a random one and try to obtain a lock.

    START TRANSACTION;
    SET @v1 =(SELECT myId FROM tests.table WHERE status is NULL LIMIT 1);
    SELECT * FROM tests.table WHERE myId=@v1 FOR UPDATE; #<- lock
    

    Setting a small timeout for the transaction, if that row is locked the transaction is aborted and I try another one. If I obtain the lock, I process it. If (bad luck) that row was locked, it is processed and the lock is released before my timeout, I then select a row that has already been 'processed'! However, I check a field that my processes set (e.g. status): if the other process transaction ended OK, that field tells me that work has already been done and I do not process that row again.

    Every other possible solution without transactions (e.g. setting another field if the row has no status and ... etc.) can easily provide race conditions and missed processes (e.g. one thread abruptly dies, the allocated data is still tagged, while a transaction expires; ref. comment here

    Hope it helps

提交回复
热议问题