question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Advisory Lock returns `null`

See original GitHub issue

When attempting to obtain an advisory lock: https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_get-lock

I’m receiving a null response instead of the 0 or 1 as specified by the MySQL documentation and as seen when I issue the query through the MySQL client.

screenshot 2018-01-05 16 50 37
import test from 'ava'

import mysql from 'mysql2'

const pool = mysql.createPool({
  connectionLimit: 10,
  host: 'localhost',
  user: 'root',
  port: 3306,
  database: 'test',
  password: '',
})

const foo = () => new Promise((resolve) => {
  resolve(1)
})

const multiQuery = () => new Promise((resolve, reject) => {
  const sql = `
    SELECT GET_LOCK('test2', -1) AS test3;
  `
  pool.query(sql, (err, res) => (err == null ? resolve(res) : reject(err)))
})

test(async (t) => {
  const response = await multiQuery()
  console.log(response)
  t.is(await foo(), 1)
})

Response: screenshot 2018-01-05 16 52 52

Is this expected?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
scull7commented, Jan 6, 2018

@sidorares thank you for your help

0reactions
scull7commented, Jan 6, 2018

It is an issue with MariaDB vs MySql. MariaDB doesn’t seem to support the negative timeout.

From the MySql Docs

A negative timeout value means infinite timeout. The lock is exclusive. While held by one session, other sessions cannot obtain a lock of the same name.

The MariaDB Docs omit that line:

Tries to obtain a lock with a name given by the string str, using a timeout of timeout seconds. Returns 1 if the lock was obtained successfully, 0 if the attempt timed out (for example, because another client has previously locked the name), or NULL if an error occurred (such as running out of memory or the thread was killed with mysqladmin kill).

I apologize, I did not notice the difference.

Results when using a positive integer for the timeout: screenshot 2018-01-05 18 35 42

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advisory Lock from the nodejs mysql and mysql2 driver ...
Both implementations return NULL if an error occurred, however, the MariaDB does not support the negative timeout.
Read more >
How do PostgreSQL advisory locks work - Vlad Mihalcea
Session-level advisory locks need to be released explicitly. Even if advisory locks are reentrant, if you acquired the same lock twice, you need ......
Read more >
PostgreSQL Advisory Locks - Netguru
Advisory locks. Those locks are database based, stored in the pg_locks table. This simple SQL query will return currently obtained locks:
Read more >
9.24. System Administration Functions - PostgreSQL
pg_advisory_unlock will release a previously-acquired exclusive session level advisory lock. It returns true if the lock is successfully released. If the lock ...
Read more >
MySQL 5.7 Reference Manual :: 12.15 Locking Functions
Returns 1 if the lock was obtained successfully, 0 if the attempt timed out (for example, because another client has previously locked the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found