[drupal-support] How to use MySQL last_insert_id function with locking?

Chris Johnson chris at tinpixel.com
Thu Jul 21 21:08:13 UTC 2005


KZ Mai wrote:
> I was under the impression that when drupal uses a single shared MySQL
> connection, using last_insert_id to get the newly created rows in
> tables may cause problems.
> 
> Say in my drupal module,
> User A tries to create a new row in a table, before my module calls
> last_insert_id.  User B creates a new row in the same table.
> 
> Is it possible that urser A gets back the id which is associated with
> the row User B created?

No.  To repeat what I wrote previously in another way:  yes, Drupal may use 
only one connection, but that is one connection PER INSTANCE of the web server 
(e.g. child processes if you are using Apache on a Unix system).  Each 
instance of the web server will finish the Drupal PHP code it is executing for 
one HTTP request before it services another.  The only way User B can even 
create a row before User A gets the last ID back is to use a DIFFERENT MySQL 
connection.




More information about the drupal-support mailing list