26

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Loren Pechtel
  • 8,945
  • 3
  • 33
  • 45

1 Answers1

42

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CBroe
  • 91,630
  • 14
  • 92
  • 150
  • I missed that little per-connection detail. – Loren Pechtel Jun 21 '15 at 00:21
  • 5
    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. – Russ Oct 10 '17 at 15:08
  • 2
    @Russ 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? – frank May 23 '18 at 08:36
  • if your maxPoolSize is 1, is it still a pool though ? – Tristan Feb 04 '19 at 13:49
  • That also applies for a bash `echo` insert ? In Bash i need two echo's. One for Insert and Second for Last_Inserted_Id. – TheAlphaGhost Feb 23 '20 at 09:56
  • 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`. – tonix Oct 16 '20 at 21:56