I’m curious whether this is a possibility. I have a stored procedure which Inserts and then retrieves the last insert id. What if 2 users both use the procedure at the same time, is something like this possible?
User 1
User 2
Insert 1
Insert 2
GetsLastid 2
GetsLastid 2
Could the 2 calls of the stored procedure interlace the sequence of the insert queries? Or will one take lead?
Thank you!
That’s not a problem. From the fine manual:
So the
last_insert_id()value is always per-session (AKA connection) and you have two sessions in play, the can’t interfere with each other’slast_insert_id()values.That said, it is a good idea to grab the
last_insert_id()value and store it in a variable as soon after the INSERT as possible. If you do something else that does an INSERT behind your back — say you call another procedure that has logging added two months down the road and that logging does an INSERT — you will lose thelast_insert_id()value that you want.