5

I have a php page that needs to write to a database sometimes at the beginning of the script.

During a scheduled mysqldump backup, the page isn't accessible, because it is trying to write to a table that is locked.

During the lock, MySql queues those sql updates, and after the backup is complete, they do get executed as desired. Yet my php page won't display until the backup is complete.

However, since the updates do not return anything to the script, I wish they wouldn't hang up my script during the lock; I hope there's a way I can tell php to just send the sql update to mysql and don't worry about confirmation that the update completed.

I want php to send the query to mysql, and then just continue on to the next line of php if the update doesn't complete in one second.

I'm running php on a windows server, so I understand that some type of threading is not an option.

If there is way to set a timeout on that query (specifically), that might be ideal, but I don't want to renege the sql update altogether if it times out. I still want MySQL to process it after the lock is gone.

What would you recommend (given that removing the sql updates altogether is not an option)?

8
  • Not possible. PHP has no control over the actual query call mechanics. PHP is not threaded, regardless of which OS it's running on. Commented Jun 9, 2012 at 5:08
  • If PHP has some type of timeout function that could encapsulated any other function-call while placing a timeout on the encapsulated function, would that technically be multi-threaded? Commented Jun 9, 2012 at 5:28
  • No. PHP has no support for threading whatsoever, and most likely will never become multithreaded without a fundamental rewrite of the whole language - e.g. PHP v6. Most of the PHP core libraries are not thread safe. Commented Jun 9, 2012 at 5:32
  • Are you aware of any solution to where I can specify within the sql statement to return true after one second if the update hasn't completed? Commented Jun 9, 2012 at 5:37
  • 1
    nope. nothing within PHP. Your best bet might be to put in a queueing system - set a flag when the dump starts, any upates which come in while the flag's hot get stuck in a queue, then process then after the flag's disabled. Commented Jun 9, 2012 at 5:43

3 Answers 3

3

One solution, using PDO, start a connect with timeout, begin a transaction and case you have lock, the timeout will close the connection and will be rollback.

For timeout in PDO, to use PDO::ATTR_TIMEOUT .

Sign up to request clarification or add additional context in comments.

Comments

3

For others that may come across this thread, I ultimately wrote this function to detect if the table is locked, and only do the SQL updates if it is NOT locked:

function isLocked($databaseName,$tableName)
    {
        $sql = "show open tables from `".$databaseName."` where `table`=`".tableName."` and In_use > 0;";
        $result = @mysql_query($sql);
        if (mysql_num_rows($result) != 0 )
        {
            return true;
        }
        else
        {
            return false;
        }
    }

(note: This function assumes the database connection is already established)

This works, because I've modified the code so that the sql updates will ultimately get done the next time the page is loaded without a lock in place.

Comments

1

i think the only way to do that, is split you script in pieces and using ajax calls, you can send the big update and continue with the rest of the script that doesn't depend with that process, if all script depend of the result of the update, the only way is make a nice loading ...

1 Comment

That's a good idea, but I need these sql updates to happen, even if the user doesn't have javascript enabled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.