PHP MySQLi Lock Tables Query Sometimes Fails

I am trying to lock some tables when I run some code and unlock tables when I finish.

process:

  • Run MySQL Query lock tables.
  • Run some PHP code.
  • Run MySQL Query unlock tables.

During this process, 9 times out of 10, everything works fine. Sometimes, when I run my query, a response is returned from MySQL, and PHP just waits for a response. Since there is no answer, I never get to steps 2 or 3 and the table remains locked indefinitely. I fulfill the same Lock Tables Query requirement in every single attempt.

I am blocking many tables. Below is an example of my actual table query. I use the same table several times with different aliases based on queries that I try to deny for access to tables.

$sql = "LOCK TABLES table1 as t1 WRITE
                    , table2 as t2 WRITE
                    , table3 WRITE
                    , table2 WRITE
                    , table4 WRITE
                    , table1 WRITE
                    , table5 WRITE
                    , table5 as t5 WRITE
                    , table6 as t6 WRITE
                    , table7 as t7 WRITE
                    , table7 WRITE
                    , table8 as t8 WRITE
                    , table9 t9 WRITE
                    , table10 t10 WRITE
                    , table11 t11 WRITE
                    , table12 WRITE;";

$this->mysqli = new mysqli(
    $this->credentials->server
    , $this->credentials->user
    , $this->credentials->password
    , $this->credentials->database
);

try{
    error_log('before first attempt lock tables '.$sql);
    $this->mysqli->query($sql);
    error_log('after first attempt lock tables');
} catch (Exception $e){
    error_log('Error locking tables: '.$e->getMessage());
}

error_log('After try catch.');
if($this->mysqli->error){
    error_log('lock table error: '.$this->mysqli->error);
}

When the process crashes, I see "before the first attempt to lock tables" in my PHP error log. I do not see any other error_log () calls. After some checking, I decided that this was because PHP did not receive a response from MySQL.

I never get into Catch Exception since MySQL does not return an error. MySQL returns nothing unless I manually destroy the MySQL lock process.

If I do not kill the process, the PHP code never stops waiting for a response from mysql.

+4
source share

Source: https://habr.com/ru/post/1659877/


All Articles