Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1031953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:00:39+00:00 2026-05-16T14:00:39+00:00

The goal is to have a PHP script with a certain section of the

  • 0

The goal is to have a PHP script with a certain section of the code that can only be executed by one thread/process at a time.

Some restrictions:

  • semaphores not available on my system
  • The manual mentions that flock() cannot be relied on in multi-threaded servers, so flock() is out. (confirmed this)

So thought it would be possible (why not?) to use MySQL for synchronization, but I’m getting unexpected results.

To test, I have two browser tabs open with the script test.php

mysql_connect($dbhost, $dbusername, $dbpassword);
mysql_select_db($database_name);

$sql = "SELECT my_val FROM `my_table` WHERE `my_var`='status' ";
$result = mysql_query($sql)or die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
echo "status is:".$row['my_val']."<br>\n";
mysql_free_result($result);

if ($row['my_val']=='RUNNING') die ('process is already running!');

// entering critical section

$sql = "UPDATE `my_table` SET `my_val`='RUNNING' WHERE `my_var`='status' ";
mysql_query ($sql);


sleep(10); // do some work here.
echo 'finished work<br>';

// leave critical section. let the others know
$sql = "UPDATE `my_table` SET `my_val`='NOT_RUNNING' WHERE `my_var`='status' ";
mysql_query ($sql);

The table is:

CREATE TABLE `my_table` (
  `my_var` varchar(255) NOT NULL default '',
  `my_val` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`my_var`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `my_table`
VALUES (
'status', 'NOT_RUNNING'
) 

I go to the first browser tab and access the script. I wait 5 seconds, go to the other tab and access the script so that both scripts run in parallel. I expect the first tab to wait, and the 2nd tab should exit after the 1st query. However, both tabs wait on the sleep() line. This means that the first query always returns ‘NOT_RUNNING’ .

Some odd things:

  • When I repeat the above experiment, I run both tabs in FireFox, and then in a 3rd different browser type, say Chrome, then it works! (status is set to RUNNING while the sleeping, script exits early when status is RUNNING )
  • When I repeat the above experiment using two different command line windows, and run the script from the command line, it works!
  • I check phpMyAdmin while it is waiting, status gets updated correctly.

I have tried everything, locking the table, transactions, SET GLOBAL TRANSACTION ISOLATION LEVEL READ UNCOMMITTED, SET autocommit=1;, yet always the same result.

Does anyone know what is happening here? Is there any good solution for this problem?

Systems tested:
– Win, MySQL 5.0, php 5.3
– Linux, MySQL 5.0.91-community-log, php 5.2.12

I’m totally stuck here, thanks for taking a look!


UPDATE:

Thanks for submitting your answers – I still cannot solve this problem.
Here is the code with GET_LOCK() and session_write_close() as suggested:
I have also tried row level locking, transactions and different isolation levels. Perhaps it cannot be done?

session_write_close();

mysql_connect($dbhost, $dbusername, $dbpassword);
mysql_select_db($database_name);

$sql = "CREATE TABLE IF NOT EXISTS `my_table`  (
  `my_var` varchar(255) NOT NULL default '',
  `my_val` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`my_var`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
";
mysql_query($sql)or die(mysql_error());

mysql_query ("SELECT get_lock('my_lock', 100)");

$sql = "SELECT my_val FROM `my_table` WHERE `my_var`='status' ";
$result = mysql_unbuffered_query($sql)or die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
echo "status is:".$row['my_val']."<br>\n";
mysql_free_result($result);

if ($row['my_val']=='RUNNING') die ('process is already running!');

// entering critical section

$sql = "UPDATE `my_table` SET `my_val`='RUNNING' WHERE `my_var`='status' ";
mysql_query ($sql);

sleep(10); // do some work here.
echo 'finished work<br>';

// leave critical section. let the others know
//$sql = "UPDATE `my_table` SET `my_val`='NOT_RUNNING' WHERE `my_var`='status' ";
$sql = "REPLACE INTO `my_table` (`my_var`,`my_val`) VALUES ('status', 'NOT_RUNNING')";

mysql_query ($sql);
$result = mysql_query($sql)or die(mysql_error());


mysql_query ("SELECT release_lock('my_lock')");

die();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:00:40+00:00Added an answer on May 16, 2026 at 2:00 pm

    The second browser window is not hanging on the sleep command. It’s blocking because sessions are blocking (and hence it’s trying to open the same session). You can close the current session if you don’t need it anymore (and hence don’t want it to block) with session_write_close…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script that does the following: Uses file_get_contents() to get content
end goal is to have the bash run something like php script.php argument1 argument2
My Goal I would like to have a main processing thread (non GUI), and
I am using PHP and Mysql I have PHP script in which I rollback
I have written the following in my reginsert.php. The goal is to take the
I'm working on a Silverlight Polling control. My goal is to have a Button
I have an Events table whose goal is to store actions done by web
I have a decimal number (let's call it goal ) and an array of
So my goal here is to have a single search field in an application
I have a fairly simple addition to the HTTP standard. An ambitious goal I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.