Jump to content


Do I need to LOCK tables?

  • Please log in to reply
2 replies to this topic

#1 dptr1988

  • Members
  • PipPipPip
  • Advanced Member
  • 372 posts

Posted 13 July 2006 - 06:42 PM

I'm working on a script that reads numbers out of a row in the table, adds/subtracts the numbers, then updates the row in the table. I was wondering if another script reads the same row before the first script updates that row and then updates that row before or after the first after the first script updats the row, if the row would be incorrect. I don't think that is easy to understand so let me try again with a scenario with exagerated timing.

Script 1 and Script 2 take all of the columns in row1 and add 1 to each column then update row1 with the new columns

Here is the table for the example:

    num1 num2 num3
row1 1    1    1

This is the sequence of scenario:

Seconds    Action
00            Script 1 reads all of the columns in row 1
01            Script 2 reads all of the columns in row 1
02            Script 1 is done processing and updates row1 with these values 2,2,2
03            Script 2 is done processing and updates row1 with these values 2,2,2

Is it possible for this scenario to happen? If it does happen, Would the value of row1 be 2,2,2 instead of 3,3,3 like I was wanting?

I haven't had trouble like this yet, but was worried that I might cause trouble.


Need more help with your project? One of the thousands of programmers, web designers or artists at <a href="http://www.rentacode...d_6764522">Rent A Coder</a> would be happy to help.

Disclaimer: Free advice is usually worth what you paid for it. ( or at least when it's coming from me! )

#2 effigy

  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 13 July 2006 - 06:49 PM

Look into transactions; example.
Regexp | Unicode Article | Letter Database

#3 fenway

  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 13 July 2006 - 10:16 PM

This can be a problem without transactions / table locks; you won't get a consistent read otherwise, and you may get some inconsistencies.  There are some operations that are atomic, but most aren't.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users