473,385 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

PHP and mySQL concurrency problem

I'm not really sure if my problem lies in the PHP code or the SQL...
but here it is:

begin();

$query = "UPDATE Nodes "
."SET NodeStatus = 1 "
."WHERE NodeID = $NodeID AND NodeStatus = 0";

$result = mysql_query($query);

if ($result == null || $result == false) {
rollback();
return;
}
In the Nodes table, there should be only one instance of a node with a
particular NodeID.

So, presumably, user1 will hit the UPDATE and change the NodeStatus to
1. Then, when user2 hits the UPDATE, it should fail, since the
NodeStatus =1 (and a condition of the update is that the NodeStatus
=0).

However, when multiple users simultaneously (at least down to the
second, according to logs) access the method, they ALL succeed in the
UPDATE query. I don't understand how this can happen. I'm assuming
that the UPDATE operation is atomic in mySQL.

I feel like I shouldn't have to use locks.. but I'm considering it
with the terrible results so far.

Jun 18 '07 #1
7 3967
On Jun 18, 9:25 am, evanp...@gmail.com wrote:
I'm not really sure if my problem lies in the PHP code or the SQL...
but here it is:

begin();

$query = "UPDATE Nodes "
."SET NodeStatus = 1 "
."WHERE NodeID = $NodeID AND NodeStatus = 0";

$result = mysql_query($query);

if ($result == null || $result == false) {
rollback();
return;

}

In the Nodes table, there should be only one instance of a node with a
particular NodeID.

So, presumably, user1 will hit the UPDATE and change the NodeStatus to
1. Then, when user2 hits the UPDATE, it should fail, since the
NodeStatus =1 (and a condition of the update is that the NodeStatus
=0).

However, when multiple users simultaneously (at least down to the
second, according to logs) access the method, they ALL succeed in the
UPDATE query. I don't understand how this can happen. I'm assuming
that the UPDATE operation is atomic in mySQL.

I feel like I shouldn't have to use locks.. but I'm considering it
with the terrible results so far.
Single statements are atomic. Unless you need to execute multiple
statements there's no reason to use transactions. And I wouldn't be
surprised if MySQL was able to execute that particular statement in
less than a second.

Jun 18 '07 #2
ev******@gmail.com wrote:
I'm not really sure if my problem lies in the PHP code or the SQL...
but here it is:

begin();

$query = "UPDATE Nodes "
."SET NodeStatus = 1 "
."WHERE NodeID = $NodeID AND NodeStatus = 0";

$result = mysql_query($query);

if ($result == null || $result == false) {
rollback();
return;
}
In the Nodes table, there should be only one instance of a node with a
particular NodeID.

So, presumably, user1 will hit the UPDATE and change the NodeStatus to
1. Then, when user2 hits the UPDATE, it should fail, since the
NodeStatus =1 (and a condition of the update is that the NodeStatus
=0).

However, when multiple users simultaneously (at least down to the
second, according to logs) access the method, they ALL succeed in the
UPDATE query. I don't understand how this can happen. I'm assuming
that the UPDATE operation is atomic in mySQL.

I feel like I shouldn't have to use locks.. but I'm considering it
with the terrible results so far.
Not finding any rows to update is not a failure. It's a success with no
rows updated.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 18 '07 #3
ev******@gmail.com wrote:
I'm not really sure if my problem lies in the PHP code or the SQL...
but here it is:

begin();

$query = "UPDATE Nodes "
."SET NodeStatus = 1 "
."WHERE NodeID = $NodeID AND NodeStatus = 0";

$result = mysql_query($query);

if ($result == null || $result == false) {
rollback();
return;
}
In the Nodes table, there should be only one instance of a node with a
particular NodeID.

So, presumably, user1 will hit the UPDATE and change the NodeStatus to
1. Then, when user2 hits the UPDATE, it should fail, since the
NodeStatus =1 (and a condition of the update is that the NodeStatus
=0).

However, when multiple users simultaneously (at least down to the
second, according to logs) access the method, they ALL succeed in the
UPDATE query. I don't understand how this can happen. I'm assuming
that the UPDATE operation is atomic in mySQL.

I feel like I shouldn't have to use locks.. but I'm considering it
with the terrible results so far.
P.S. Please crosspost - don't multipost.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 18 '07 #4
I don't have the whole code pasted, but there is an INSERT that is
dependent on whether the UPDATE succeeds or fails - that is why I'm
using transactions.

Jun 18 '07 #5
Thank you.

So I'm assuming I'll have to run a following SELECT statement to test
whether the UPDATE was successful?
Jun 18 '07 #6
Nevermind. I found a better solution.

Thank you again.

Jun 18 '07 #7
evanpeck wrote:
Nevermind. I found a better solution.
It's always nice to post what that "better solution" is for the benefit of
those who find this thread in a few months time.

I'm guessing it involves executing the UPDATE and then asking MySQL how
many rows were changed.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 114 days, 23:33.]

dict, thes & ency
http://tobyinkster.co.uk/blog/2007/0...ict-thes-ency/
Jun 18 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

133
by: jonathan | last post by:
hey all, I realize that this question might pop up from time to time, but I haven't seen it a while and things might of changed, so - Right now (July 2004) how does mysql stand up in...
4
by: Charlie Williams | last post by:
I am having difficulty performing updates and deletions on an Access database using the Update() method of the OleDBDataAdapter. I can insert rows without a problem, but I get a concurrency...
57
by: Bing Wu | last post by:
Hi all, I am running a database containing large datasets: frames: 20 thousand rows, coordinates: 170 million row. The database has been implemented with: IBM DB2 v8.1
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
7
by: William E Voorhees | last post by:
I'm updating an Access database in a windows multi-user environment. I'm using disconnected data I read data from an Access Data table to a data object I update the data object from a...
1
by: Ike | last post by:
Recently, I began using a different MySQL verver (i.e. different machine as well as different version#, going from 4.12a to 4.1.9 max). The following query used to work: select firstname,...
3
by: John | last post by:
Hi I have a vs 2003 winform data app. All the data access code has been generated using the data adapter wizard and then pasted into the app. The problem I have is that I am getting a data...
2
jeffbroodwar
by: jeffbroodwar | last post by:
Hi all ! I'm currently working on a project that involves webservices..... the problem is with my database, i can't make it process two rs.updates at the same time. it throws an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.