0

GET_LOCK() in MySQL, accepts a negative value for timeout and is interpret as infinite timeout.

MySQL Documentation: GET_LOCK()

Tries to obtain a lock with a name given by the string str, using a timeout of timeout seconds. 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.

But in MariaDB I can't find the way to replicate that infinite timeout, because there's nothing specified in the documentation.

MariaDB Documentation: GET_LOCK()

GET_LOCK(str,timeout) [...] str is case insensitive for GET_LOCK() and related functions. If str is an empty string or NULL, GET_LOCK() returns NULL and does nothing. timeout is rounded to the closest integer.

I could replicate GET_LOCK() with infinite timeout of MySQL in MariaDB?

1
  • Try a very high timeout.
    – wchiquito
    Apr 12, 2018 at 18:07

1 Answer 1

3

MariaDB does not accept negative values for GET_LOCK:

MariaDB [test]> do get_lock('test', -1);
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [test]> show warnings;
+---------+------+-----------------------------------------------------+
| Level   | Code | Message                                             |
+---------+------+-----------------------------------------------------+
| Warning | 1411 | Incorrect timeout value: '-1' for function get_lock |
+---------+------+-----------------------------------------------------+
1 row in set (0.00 sec)

What you can do is use 0xffffffff to emulate the same behavior in MariaDB:

MariaDB [test]> do get_lock('test', 0xffffff);
Query OK, 0 rows affected (1.85 sec)
3
  • Brilliant! where is the doc of this "trick"? its quite surprising that accepts an hexadecimal value. Thank you soo much
    – BigHelmet
    Apr 13, 2018 at 8:49
  • there is not need to document this. One can always emulate "infinite" with "extremely large". 2^32-1 seconds is 136 years, which is good enough for infinite. you can use larger values, second parameter is actually a double, so you can set lock to wait until the end of our galaxy. Apr 13, 2018 at 9:47
  • 1
    @BigHelmet - To quell your 'surprise': (1) The function accepts an expression. (2) An expression can be simply a constant number. (3) Hex is a way to represent a number. (This logic applies in many places.)
    – Rick James
    May 3, 2018 at 1:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.