Thread safety of MySQL's Select Last_Insert_ID
Asked Answered
M

1

28

I'm trying to make some SQL Server code also run on MySQL, and I just hit this land mine. Google says the normal approach is to simply do your insert and then select last_insert_ID() to find out what got written.

This does not strike me as safe in a multi-user environment, though. There's a narrow window there where another user could insert something and cause a bad return value. How do I safely insert and obtain the key of the inserted record?

Mangle answered 20/6, 2015 at 22:59 Comment(0)
H
44

From LAST_INSERT_ID(), LAST_INSERT_ID(expr)

The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.

So unless your inserts for multiple users would happen to be made over the same database connection, you have nothing to worry about.

Hemangioma answered 20/6, 2015 at 23:7 Comment(7)
I missed that little per-connection detail.Mangle
This is a great answer. The OP should still take heed however when creating a modern application that utilizes a connection pool. Connection pools address the slow performance of creating a new connection for each DBMS interaction. Connection pools will behind-the-scenes hold on to connections without closing them so they can be re-used by other components to save performance. While the last_insert_ID() will work for you if you do not use a connection pool and manage your own connections on a per transaction basis, it is not a solution for those utilizing connection pooling.Jens
@Jens you said last_insert_id has problem in connection pool, right? I try to test this case. I am using spring, mysql, mybatis, c3p0 as connection pool. set both maxPoolSize and minPoolSize of c3p0 as 1, insert multiple records in a loop with the help of thread. But I found that there is no problem in this case. So can you tell me how to simulate what your said?Bismuthic
if your maxPoolSize is 1, is it still a pool though ?Readable
That also applies for a bash echo insert ? In Bash i need two echo's. One for Insert and Second for Last_Inserted_Id.Castrate
What about Node.js? Usually the Node.js server (being single-threaded) keeps a single DB connection. Is it safe in this case? I guess it depends on which order the concurrent connections to the Node.js server make queries to the DB, e.g. if connection A makes INSERT A, then connection B makes INSERT B, then when INSERT A completes and its callback its executed it invokes another query SELECT LAST_INSERT_ID(); -- A, at this point if INSERT B has completed before sending the command SELECT LAST_INSERT_ID(); -- A, then I guess that A could obtain the wrong ID of B.Backwardation
Really safe in a high volume situation is using a sp that wraps insertion in a transaction returning last_insert_id() thereby isolating it from other insertion attempts. Please correct me if insert with AI values and LAST_INSERT_ID() are thread safe. The insert and the select last_insert_id are not monolitic are they? dbaHeliostat

© 2022 - 2024 — McMap. All rights reserved.