Premise: [laravel4] All interfaces provided by the server are called asynchronously, and the data table has a unique key
Scenario: During concurrent request processing, “Integrity Constraint Violation: 1062 Duplicate Entry” will appear in mysql database, such as two users requesting to register email at the same time
$ret = User::create(array( 'email' => "firstname.lastname@example.org" ));
I want these two concurrent requests to return successfully and return Email, how to avoid duplication of key values.
There are currently the following:
1) Before insertion, use select query, but this does not solve the problem.
2) use insert ignore
3) use insert on duplicate key
4) direct try catch
Which treatment is better?
It depends on your requirements. The Duplicate entry error is caused by duplicate primary keys. If uid is the primary key, then it should be considered whether there is any problem with uid generation algorithm or whether there is duplicate registration.