Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is mysql_insert_id thread safe?

Tags:

c

mysql

I'm using MySQL C API to build a db client application, and i need to get the last autoincremented value in a INSERT statement, so mysql_insert_id does.

But this client is multithreaded and a piece of code like this:

mysql_query(conn, query_string); value = mysql_insert_id(conn);

I don't know what will it return, this query autogenerated id or something else. Any clues?

Thanks in advance.

like image 388
Manuel Abeledo Avatar asked Sep 06 '25 08:09

Manuel Abeledo


1 Answers

According to the documentation mysql_insert_id will return an id of the last successful insert on the current connection.

So if you use the same connection for several threads it will not be thread safe.

like image 68
idstam Avatar answered Sep 09 '25 04:09

idstam