u LoV
u LoV

Reputation: 1

MySQL DeadLock on Delete and Insert

in Laravel I have a service which is not wrapped in a transaction and involves both INSERT ... ON DUPLICATE KEY UPDATE and DELETE statements targeting the same table, customers.

INSERT INTO customers (columns...) VALUES (values...) ON DUPLICATE KEY UPDATE ...;

delete from customers where customers.id in (103,112,114,119,121...).

into a single MySQL database.

When I trigger multiple requests to these endpoints simultaneously, I encounter deadlocks. I understand that INSERT and DELETE operations should lock the data rows they affect, and the next transaction should wait until these locks are released. I'm facing deadlocks even though these operations are not wrapped around a DB transaction statement.

Can anyone explain why DELETE and INSERT ... ON DUPLICATE KEY UPDATE operations cause deadlocks and how to prevent these deadlocks?

Upvotes: 0

Views: 20

Answers (0)

Related Questions