首页 >数据库 >mysql教程 >MySQL 的存储过程中请求一个锁对象

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

大家讲道理
大家讲道理原创
2016-11-12 09:35:151346浏览

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