1、设置非自动提交 set autocommit=0;  这时候 for update才会起作用

2、一般用法 set autocommit=0;  for update(加锁)  ;  commit/rollback; set autocommit=1;

首先看一下,set autocommit=0 后,执行哪些语句会自动加锁,加的是什么锁?

测试环境:5.6.16   innnoDB引擎 非自动提交方式(即 set autocommit=0;)

测试过程:执行 select * from w_help ;这个语句,并不会加锁 其他命令窗口一样可以增删改查;测试代码如下

DELIMITER $$

DROP PROCEDURE IF EXISTS `test_release`.`test_Lock`$$

CREATE DEFINER=`encysys48`@`%` PROCEDURE `test_Lock`(IN    v_id    VARCHAR(20),IN    v_flag    VARCHAR(10))
BEGIN
    set autocommit=0;
    select t.flag from test_lock t where t.hf_serial = v_id for update;
    /*update test_lock t
      set t.flag = v_flag
        where t.hf_serial = v_id;*/
    -- insert into w_help(char_content) values ('aaaaaaaaaaaaaaaaaaaa') for update;
    -- delete from w_help where w_help.id = '451';
    select * from w_help ;
    select sleep(v_flag);
    commit;
    set autocommit=1;
    END$$

DELIMITER ;
存储过程 加锁后SLEEP

相关文章:

  • 2021-10-05
  • 2022-12-23
  • 2021-10-26
  • 2022-01-12
  • 2022-12-23
  • 2021-07-30
  • 2021-04-04
猜你喜欢
  • 2021-10-30
  • 2021-12-03
  • 2021-06-06
  • 2021-11-28
  • 2021-04-29
  • 2022-01-01
相关资源
相似解决方案