Quantcast
Channel: User Schwern - Stack Overflow
Viewing all articles
Browse latest Browse all 581

Answer by Schwern for Unable to reproduce primary key error on 'create if not exists sql'

$
0
0

This is a classic race condition, "check then do". Consider what happens if two database connections try to do the same thing at roughly the same time.

Connection AConnection B
if not exists...
if not exists...
true
true
insert...
insert...
success
duplicate

To avoid this, "do then check". Do the insert and catch the duplicate key error.


Viewing all articles
Browse latest Browse all 581

Trending Articles