Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO function lastInsertId and race condition

Tags:

php

mysql

pdo

Suppose that in my system an user U1 create a new database entry in table A and I use lastInsertId function to get the ID (auto_increment field) of such entry, but almost at the same time (a little later) another user U2 do the same.

In this case, does lastInsertId return for U1 the ID of the entry added by the other user, since it is the most recent?

Thanks.

like image 950
BrunoB Avatar asked Feb 02 '13 21:02

BrunoB


1 Answers

I believe you should only worry about race conditions in case your using a single connection for different threads. If your using a different connection for each request, you should be fine. Checkout http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html for more details.

like image 65
Muc Avatar answered Oct 17 '22 10:10

Muc