首頁  >  文章  >  資料庫  >  MySQL 的存储过程中请求一个锁对象

MySQL 的存储过程中请求一个锁对象

WBOY
WBOY原創
2016-06-07 14:54:301328瀏覽

无详细内容 MySQL mysqlmysql delimiter $$mysqlmysql CREATE PROCEDURE myProc() - BEGIN - DECLARE lock_result INT; - IF get_lock('sp_critical_section_lock',60) THEN - /* This block can only be run by one user at a time*/ - SELECT 'got lock'; -

MySQL
mysql>
mysql> delimiter $$
mysql>
mysql> CREATE PROCEDURE myProc()
    -> BEGIN
    ->     DECLARE lock_result INT;
    ->     IF get_lock('sp_critical_section_lock',60) THEN
    ->        /* This block can only be run by one user at a time*/
    ->        SELECT 'got lock';
    ->        /* Critical code here */
    ->       SET lock_result=release_lock('sp_critical_section_lock');
    ->     ELSE
    ->        SELECT 'failed to acquire lock';
    ->        /* Error handling here */
    ->     END IF;
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql> call myProc();
+----------+
| got lock |
+----------+
| got lock |
+----------+
1 row in set (0.02 sec)

Query OK, 0 rows affected (0.02 sec)

mysql>
mysql> drop procedure myProc;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn