简体   繁体   中英

unique key exception on multiple inserts/updates

I have application that receives multiple requests from external sources (invoices from point-of-sale units). It gets tens of requests per second, and some of those requests are the same (have same request body).

request data is transformed and saved to two associated tables (foreign key). if record already exists (queried by unique composite key), record is updated, otherwise record is added.

the problem is that sometimes if two requests with same body are received at the same time app throws exception that unique key already exists and it can't insert it:

 SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

It's probably some kind of race condition in MySQL but can't figure it out

Whole process is wrapped in Laravel transaction. I tried setting different isolation levels. SERIALIZABLE resolves that issue but then I get lot of deadlock exceptions, and as I can see sometimes record is not saved at all.

这很简单,在这里发生的情况是,您有一些值声明为“ UNIQUE”或“ Primary Key”或类似的名称,并且您尝试再次插入,某些键限制它阻止了插入,这应该很好,请避免数据库中有重复的条目,但是您需要做的是检查您尝试插入的数据是否存在于数据库中,而不是存在于所有列之前,您应该索要您的键或键组合,但我无济于事如果我不知道数据或表格...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM