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. Constraint where both columns cannot be null, but one can

    ALTER TABLE TableA ADD CONSTRAINT CK_BothDepartsNotNull CHECK (departA IS NOT NULL OR departB IS NOT ...

  2. 数据库中,char 与 varchar2 的区别

    区别1: char 是定长的字符串 varchar2 是变长的字符串 区别2: name char(10)   zhangsan__ 如果实际长度不足10,使用空格补齐   name varchar2 ...

  3. POJ 1094 Sorting It All Out(经典拓扑+邻接矩阵)

    ( ̄▽ ̄)" //判环:当入度为0的顶点==0时,则有环(inconsistency) //判序:当入度为0的顶点仅为1时,则能得到有序的拓扑排序,否则无序 //边输入边判断,用contin ...

  4. PHP验证码程序

    [摘自网络,参考学习] <?php/** * vCode(m,n,x,y) m个数字 显示大小为n 边宽x 边高y * http://blog.qita.in * 自己改写记录session $ ...

  5. Java使用千分位并保留两位小数

    double d = 123456.789; DecimalFormat df = new DecimalFormat("#,##0.00"); System.out.printl ...

  6. 2016NEFU集训第n+3场 G - Tanya and Toys

    Description In Berland recently a new collection of toys went on sale. This collection consists of 1 ...

  7. JS中offsetTop、clientTop、scrollTop、offsetTop各属性介绍(转载)

    这里是JavaScript中制作滚动代码的常用属性 页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见 ...

  8. ios系统下的QQ浏览器jquert的BUG

    $(document).bind("click", function(e){ var $t = $(e.target); alert(333); if($t.is("p& ...

  9. 怎么查看window7的.net framework的版本

    第一步.打开控制面板,在大图标查看方式下,点击“程序和功能” 第二步.在程序和功能界面,点击左侧“打开或关闭Windows功能” 第三步.在打开或关闭Windows功能界面,通过拖动滚动条的方式,找到 ...

  10. 让ECSHOP模板支持转smarty时间戳

    找到includes/cls_template.php 在大约640行,加入: case 'time': $p = 'date("Y-m-d H:i:s",' . $p . ')' ...