[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

mysql_insert_id() a 'Race Condition' ?

Posted on 2008-11-06
7
Medium Priority
?
696 Views
Last Modified: 2012-06-27
Is it possible for mysql_insert_id() to undergo a 'Race Condition' ? It would be my best guess that this function has exclusive process control (even in a hyperthreaded situation) such that only one client can call this at once (first in, first out queue with no acynchornous or concurrent behavior).

Is this true?

0
Comment
Question by:level9wizard
  • 3
  • 2
  • 2
7 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 1000 total points
ID: 22895860
mysql_insert_id() will give you the last auto id  on the current connection. If there are multiple processes running which have a mysql connection open AND all of them insert into a table, thereby generating an auto id, when you call mysql_insert_id() on each of these processes, you will get the auto id generated on each of the respective connections. So, if this were to happen:

proccess1: inserted a record (resulting in id 3)
proccess2: inserted a record (resulting in id 4)
process1: mysql_insert_id() (will return 3 - the last auto id of process 1 - NOT the max id on the table)
proccess3: inserted a record (resulting in id 5)
proccess4: inserted a record (resulting in id 6)
process2: mysql_insert_id() (will return 4 - the last auto id of process 2 - NOT the max id on the table)
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 22895870
it's actually not like that that it works.
it's the mysql server holding the information, per session/table, of the last generated autonumber, and it returns that information to the function.

but the result for you is the same: every session will get it's own generated id values via the mysql_insert_id() function.
0
 
LVL 11

Author Comment

by:level9wizard
ID: 22896427
It sounds like your answers differ, I think I've made this more complicated in my first question than it needs to be.

Let me be more to the point with a real example:

On one page I run a query produced from some $_POST'ed form data that inserts a row, and stores the mysql_insert_id()  into a PHP $_SESSION;

On the next page, some more information from the user is requested and the row is UPDATE'ed using WHERE the id is equal to my $_SESSION id.

My fear is that if person A was a little bit slow in their steps thru the two form pages, and person B came in and signed up after them, but was quicker to page two, would person B in fact be using the mysql_insert_id() from person A (since I'm storing it in my own session)?
0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22896491
as it will be your page that runs both the insert and the mysql_insert_id() on the same database session, that site browsing session will get IT's own mysql id. it will store that in it's own $_SESSION array, which again is dedicated to that user's site browsing session.
0
 
LVL 11

Author Comment

by:level9wizard
ID: 22896680
angelIII, so to the point - no, there's not chance Person B will get Person A's mysql_insert_id() ?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22896830
correct.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22897015
>>It sounds like your answers differ,
No:
>>>>you will get the auto id generated on each of the respective connections
>>>>but the result for you is the same: every session will get it's own generated id values via the mysql_insert_id() function.

>>there's not chance Person B will get Person A's mysql_insert_id()
No.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month19 days, 3 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question