Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Skip MySql Query if it takes longer than a second

Tags:

php

mysql

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)?

like image 601
Lonnie Best Avatar asked Jun 09 '12 04:06

Lonnie Best


1 Answers

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 .

like image 141
Ismael Vacco Avatar answered Oct 01 '22 01:10

Ismael Vacco