mysql的repeatable read事务隔离级别的测试

转自:http://nihongye.javaeye.com/blog/28827

  1. 准备工作:   
  2.  CREATE TABLE `t` (                                          
  3.           `id` int(5) NOT NULL default '0',                         
  4.           `name` varchar(255default NULL,                         
  5.           `version` int(5default NULL,                            
  6.           PRIMARY KEY  (`id`)                                       
  7.         ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC     
  8.   
  9. insert into t (id,name,version) values (1,'c1',1);   
  10.   
  11. client1:   
  12. start transaction;   
  13.   
  14. mysql> SELECT @@tx_isolation;   
  15. +-----------------+   
  16. | @@tx_isolation  |   
  17. +-----------------+   
  18. | REPEATABLE-READ |   
  19. +-----------------+   
  20. 1 row in set (0.00 sec)   
  21.   
  22. mysql> select * from t where id = 1;   
  23. +----+------+---------+   
  24. | id | name | version |   
  25. +----+------+---------+   
  26. |  1 | c1   |       1 |   
  27. +----+------+---------+   
  28. 1 row in set (0.00 sec)   
  29.   
  30. client2:   
  31. start transaction;   
  32. mysql> select * from t where id = 1;   
  33. +----+------+---------+   
  34. | id | name | version |   
  35. +----+------+---------+   
  36. |  1 | c1   |       1 |   
  37. +----+------+---------+   
  38. 1 row in set (0.00 sec)   
  39.   
  40. client1:   
  41.   
  42. mysql> update t set name='c1_1',version=2 where id = 1;   
  43. Query OK, 1 row affected (0.00 sec)   
  44. Rows matched: 1  Changed: 1  Warnings: 0  
  45.   
  46. mysql> select * from t where id =1;   
  47. +----+------+---------+   
  48. | id | name | version |   
  49. +----+------+---------+   
  50. |  1 | c1_1 |       2 |   
  51. +----+------+---------+   
  52. 1 row in set (0.00 sec)   
  53.   
  54. mysql> commit;   
  55. Query OK, 0 rows affected (0.02 sec)   
  56.   
  57. client2:   
  58.   
  59. //因为repeatable read,只看到client1提交前的数据   
  60. mysql> select * from t where id=1 and version=1;   
  61. +----+------+---------+   
  62. | id | name | version |   
  63. +----+------+---------+   
  64. |  1 | c1   |       1 |   
  65. +----+------+---------+   
  66. 1 row in set (0.00 sec)   
  67.   
  68. mysql> update t set name='c2',version=3 where id = 1 and version = 1;   
  69. //影响行数为0   
  70. Query OK, 0 rows affected (0.00 sec)   
  71. Rows matched: 0  Changed: 0  Warnings: 0  
  72.   
  73. mysql> update t set name='c2',version=3 where id = 1 and version = 2;   
  74. //影响行数为1,说明更新语句的执行是看到提交的数据的   
  75. Query OK, 1 row affected (0.00 sec)   
  76. Rows matched: 1  Changed: 1  Warnings: 0  
  77. 总结:在mysql,repeatable read级别的事务,只保证看到的数据是一个镜像.   
  78. 就算该行数据被别的事务修改,当该事务也对数据进行修改,在提交时也不会产生任何错误.   
  79. 解决的办法:   
  80. 1.在应用端使用乐观锁的机制   
  81. 2.或者使用select .. for update或者SELECT ... LOCK IN SHARE MODE来对数据加.  
  82. 相关知识见:
    http://dev.mysql.com/doc/refman/5.0/en/innodb-lock-modes.html
    http://dev.mysql.com/doc/refman/5.0/en/innodb-transaction-isolation.html 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值