sourcecode

mariadb의 select+update 트랜잭션을 원자적으로 실행할 수 없습니다.

copyscript 2023. 7. 28. 22:31
반응형

mariadb의 select+update 트랜잭션을 원자적으로 실행할 수 없습니다.

mariadb의 "tasks" 테이블을 사용하여 python 스크립트를 병렬화하려고 합니다. 여기서 각 행은 작업을 설명하고 실행 중인 각 스크립트 인스턴스는 다음과 같은 단순화된 python + peewee 코드를 수행합니다.

with db.transaction() as txn: 
   result = list(Table.raw("select id from tasks where started=0 limit 1"))
   if len(result) == 0:
      sys.exit("nothing left to do")

   my_next_task = result[0]

   # mark this task as started
   Table.raw("update tasks set started=1 where id=%s" % my_next_task.id)

# process my_next_task

그러나 스크립트의 두 인스턴스를 동시에 시작하면 두 인스턴스 모두 동일한 작업을 시작합니다.이 경우에 거래가 어떻게 작동하는지 제가 오해하고 있는 것입니까?

이거 먹어봐요.다음 코드는 작업을 실행하기 전에 하나의 스레드만 잠금을 획득하도록 보장됩니다.

1. 트랜잭션을 수행한다는 사실은 한 번에 하나의 스레드만 업데이트할 수 있는지 확인합니다.

2.시작 = 0을 확인해야 합니다.이렇게 하면 하나의 스레드만 업데이트할 수 있습니다.

with db.transaction() as txn: 
   result = list(Table.raw("select id from tasks where started=0 limit 1"))

   // The update query should return number of rows updated. So if a thread updates the task, it should return 1, which means it acquired a lock. If not, it means the thread didn't acquire the lock
   got_lock = Table.raw("update tasks set started=1 where id=%s and started = 0" % my_next_task.id) > 0 

   if len(result) == 0:
      sys.exit("nothing left to do")

   my_next_task = if(got_lock) result[0] else return

언급URL : https://stackoverflow.com/questions/36536725/cant-make-selectupdate-transaction-in-mariadb-execute-atomically

반응형