mysql 5.7. GET_LOCK not working any more

Viewed 1287

After upgrading to mysql 5.7 GET_LOCK stopped working as it used on mysql 5.5 and as I expected to work. I am aware of the changes regarding the GET_LOCK in 5.7. as described here.

When I execute the same script from the cmd line twice – with small pause in between – it works as expected: First one acquires the lock and second one doesn't.

When I execute same php script via browser twice – with small pause in between – both return that they successfully acquired the lock. This result is not what I expected and it is different from version 5.5 and from my understanding of the GET_LOCK as described in 5.7 documentation.

  • PHP is running as module (phpinfo() shows Server API: Apache 2.0 Handler ).
  • PHP version is: 7.0.20
  • Mysql version is: 5.7.18 OS is
  • CentOS 7.

This is the example script locktest.php:

<?php


$host = 'localhost';
$db   = 'enter_your_db';
$user = 'enter_your_username';
$pass = 'enter_your_password';
$charset = 'utf8mb4';


$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
    PDO::ATTR_PERSISTENT => false
];
$pdo = new PDO($dsn, $user, $pass, $opt);

echo "pid=(".getmypid().")\n";

$stmt = $pdo->query('SELECT connection_id() as connid');
$row = $stmt->fetch();

echo "mysql connection id =(".$row['connid'].")\n";


$stmt = $pdo->query('SELECT GET_LOCK("foobar", 2)');
$row = $stmt->fetch();


var_dump($row);
echo "\n\n";
sleep(10);

When this script is run from cmd line – I get what I expect: Run php -q locktest.php from one terminal. Then immediately after from another terminal window.

The first one will return:

pid=(18378)
mysql connection id =(71)
array(1) {
  ["GET_LOCK("foobar", 2)"]=>
  int(1)
}

(please note that GET_LOCK result is 1)

The second one will return (started while the first one was still running):

pid=(18393)
mysql connection id =(73)
array(1) {
  ["GET_LOCK("foobar", 2)"]=>
  int(0)
}

(please note that GET_LOCK result is 0 – as expected, and different pid and mysql connection id).

When the same script is started twice from the browser – it reports that both scripts successfully obtained the lock. First returns:

pid=(11913) mysql connection id =(74) array(1) { ["GET_LOCK("foobar", 2)"]=> int(1) } 

Second returns (while first one is still running):

pid=(11913) mysql connection id =(75) array(1) { ["GET_LOCK("foobar", 2)"]=> int(1) } 

Please note that pids are the same, but mysql connection id is different, and GET_LOCK result is not as expected since both returned 1.

Now I am confused. Different mysql connections (as returned by CONNECTION_ID()) is used and this suggests different mysql sessions. And according to the mysql documentation it is possible to obtain more locks with same name from SAME session, but here I have different mysql sessions, right?

I even put PDO::ATTR_PERSISTENT => false although that is a default value.

The only difference between output from cmd line and browser are the pids (Different pids from two executed php scripts from cmd line, and same pids from two executed php scripts from the browser).

Any thoughts what is happening? For now, to me, it seems like a major issue since locking quietly stopped working. :(

Thanks.

0 Answers
Related