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

call test_Lock(1,10)  -- 调用存储过程

在释放锁之前,下面的语句都是可以执行的,说明表并没有加锁

select * from w_help;
insert w_help(char_content) values("abcd");
update w_help t
set t.char_content = ''
where t.id = "458";
delete from w_help -- where w_help.id = '458'

执行 insert into w_help(char_content) values ('aaaaaaaaaaaaaaaaaaaa') ;这个语句,只会给一行加锁,(值得一提的是 这个语句后面不能跟 for update 否则会报错,不知道是我写的语法问题,还是根本就不能加)  insert into w_help(char_content) values ('aaaaaaaaaaaaaaaaaaaa') for update; 就会报错;

其他事务语句执行情况如下:

select * from w_help;  -- 可以执行
insert w_help(char_content) values("abcd"); -- 可以执行
update w_help t
set t.char_content = ''
where t.id = "464"; -- 可以执行
delete from w_help where w_help.id = ''; -- 可以执行
delete from w_help -- 不能执行会等待锁释放后执行

执行 update w_help t
     set t.char_content = v_flag
    where t.id ="476"; 这个语句,只会给一行加锁,(前提是指明了主键)

其他事务语句执行情况如下:

select * from w_help;  -- 可以执行
insert w_help(char_content) values("abcd"); -- 可以执行
update w_help t
set t.char_content = ''
where t.id = "479"; -- 可以执行
delete from w_help where w_help.id = ''; -- 可以执行
delete from w_help -- 不能执行

如果不指明主键 则会锁住整个表,其他事务语句执行情况如下:

select * from w_help;  -- 可以执行
insert w_help(char_content) values("abcd"); -- 可以执行并且不会被update
update w_help t
set t.char_content = ''
where t.id = "489"; -- 不能执行 等待锁被释放后继续执行
delete from w_help where w_help.id = ''; -- 不能执行 等待锁被释放后继续执行
delete from w_help -- 不能执行 等待锁被释放后继续执行

 执行 delete from w_help where w_help.id = '490';这个语句 产生行级锁,

其他事务语句执行情况如下:

select * from w_help;  -- 可以执行
insert w_help(char_content) values("abcd"); -- 可以执行
update w_help t
set t.char_content = ''
where t.id = "490"; -- 被锁定的唯一一行 不能执行
delete from w_help where w_help.id = ''; -- 不是被锁定的那一行,可以执行
delete from w_help -- 不能执行

还有一个奇葩的问题 select * from w_help for update; 并不能限制其他线程读取数据;可以限制其他线程加锁(即其他线程便不能加锁了)

这样的的话怎么防止读脏数据就是个问题了;不过我是在两个连接中测试的,即一个连接执行此语句,另一个连接仍然可以读数据,但是不可以修改!

所以,如果修改数据,select 时一定要 for update,否则就会有致命错误!

最后附上测试用的乱七八糟的代码,不值得看,只是做个备份而已,说不定哪时会用到;

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 w_help t
set t.char_content = v_flag ;
-- where t.id ="476";
-- insert into w_help(char_content) values ('aaaaaaaaaaaaaaaaaaaa') ;*/
delete from w_help where w_help.id = '';
-- select * from w_help ;
select sleep(v_flag);
commit;
set autocommit=1;
END$$ DELIMITER ; --
select * from w_help; -- 可以执行
insert w_help(char_content) values("abcd"); -- 可以执行
update w_help t
set t.char_content = ''
where t.id = "490"; -- 被锁定的唯一一行 不能执行
delete from w_help where w_help.id = ''; -- 不是同一行,可以执行
delete from w_help -- 不能执行
update w_help t
set t.char_content = "666" where t.id = "489";

乱七八糟

MySql 加锁问题的更多相关文章

  1. Mysql加锁过程详解

    1.背景 MySQL/InnoDB的加锁分析,一直是一个比较困难的话题.我在工作过程中,经常会有同事咨询这方面的问题.同时,微博上也经常会收到MySQL锁相关的私信,让我帮助解决一些死锁的问题.本文, ...

  2. MySQL 加锁处理分析

    1    背景    1 1.1    MVCC:Snapshot Read vs Current Read    2 1.2    Cluster Index:聚簇索引    3 1.3    2P ...

  3. MySQL 加锁处理分析 转

    MySQL 加锁处理分析  转 http://hedengcheng.com/?p=771 十二 13th, 2013 发表评论 | Trackback   1    背景    1 1.1    M ...

  4. 转载-MySQL 加锁处理分析

    MySQL 加锁处理分析 发表于 2013 年 12 月 13 日 由 hedengcheng 1    背景    1 1.1    MVCC:Snapshot Read vs Current Re ...

  5. MySQL加锁分析

    参考:MySQL 加锁处理分析.该文已经讲的很详尽了,也易懂,下面仅仅是个人做的总结. 一. 背景 1.1 隔离级别 1.2 加锁过程 逐条处理,逐条加锁. 1.3 两阶段锁2PL 1.4 gap锁 ...

  6. MySQL 加锁处理分析-转载

    来自何登成的技术博客     1.1    MVCC:Snapshot Read vs Current Read    2 1.2    Cluster Index:聚簇索引    3 1.3     ...

  7. Mysql加锁过程详解(8)-理解innodb的锁(record,gap,Next-Key lock)

    Mysql加锁过程详解(1)-基本知识 Mysql加锁过程详解(2)-关于mysql 幻读理解 Mysql加锁过程详解(3)-关于mysql 幻读理解 Mysql加锁过程详解(4)-select fo ...

  8. Mysql加锁过程详解(9)-innodb下的记录锁,间隙锁,next-key锁

    Mysql加锁过程详解(1)-基本知识 Mysql加锁过程详解(2)-关于mysql 幻读理解 Mysql加锁过程详解(3)-关于mysql 幻读理解 Mysql加锁过程详解(4)-select fo ...

  9. Mysql加锁过程详解(1)-基本知识

    Mysql加锁过程详解(1)-基本知识 Mysql加锁过程详解(2)-关于mysql 幻读理解 Mysql加锁过程详解(3)-关于mysql 幻读理解 Mysql加锁过程详解(4)-select fo ...

  10. Mysql加锁过程详解(2)-关于mysql 幻读理解

    Mysql加锁过程详解(1)-基本知识 Mysql加锁过程详解(2)-关于mysql 幻读理解 Mysql加锁过程详解(3)-关于mysql 幻读理解 Mysql加锁过程详解(4)-select fo ...

随机推荐

  1. apache动态添加模块

    Apache已经安装完毕并投入运行,但是后来却发现部分模块没有加载,当然有两个方法: 1. 一是完全重新编译Apache, 再安装 2. 编译模块为SO文件,使用LoadModule指令加载扩展模块. ...

  2. 使用Angular构建单页面应用(SPA)

    什么是SPA?看下图就是SPA: 下面说正经的,个人理解SPA就是整个应用只有一个页面,所有的交互都在一个页面完成,不需要在页面之间跳转. 单页面的好处是更快的响应速度,更流畅的用户体验,甚至和桌面应 ...

  3. SOJ 1210 二叉树

    1210. 二叉树 Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description 在众多的数据结构中,二叉树是一种特殊而重要的结构,有 ...

  4. MVC3+EF4.1学习系列(六)-----导航属性数据更新的处理

    通过上一篇的学习 我们已经知道怎么查询关系 这篇就来说说怎么导航属性数据更新时的处理 以及EF又会为我们生成哪些SQL~ 老规矩 先看下今天的图 添加和修改页面基本就是这样 这节的内容相对简单~~ 主 ...

  5. FZU 1894 志愿者选拔 单调队列

    训练赛的题…… 暴力一波明显超时…… 最近刚学stl 感觉是优先队列 但还是太会用…… 以后可以试一下优先队列…… 比赛之后百度了一下 发现是单调队列…… 看起来挺简单的 也算个模版题吧…… 总之思路 ...

  6. Chapter 14_4 使用_ENV

    因为_ENV是一个普通的变量,我们可以像其他变量一样去对它进行赋值和访问. _ENV = nil 上面的赋值操作,将会使得在它之后的代码块不能直接访问全局变量.不过,对控制你的代码所使用的变量有用处. ...

  7. [css3动画]渐隐渐现

    测试 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...

  8. apicloud教程

    http://community.apicloud.com/bbs/forum.php?mod=viewthread&tid=15939

  9. selinux策略开发

    所用软件: 1.setools -->seaudit  读取日志以确定所需权限 2.Reference Policy  -->  https://github.com/TresysTech ...

  10. CSS3的box-sizing属性

    盒模型的宽度,在 IE5.x 以及 Quirks 模式的 IE6/7 中,将 border 与 padding 都包含在 width 之内 W3C标准中的盒模型宽度为内容宽度,不包括内边距paddin ...