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

刚好做了一个mysql的repeatable read事务隔离级别的测试,帖出来大家看看:
[code]
准备工作:
CREATE TABLE `t` (
`id` int(5) NOT NULL default '0',
`name` varchar(255) default NULL,
`version` int(5) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC

insert into t (id,name,version) values (1,'c1',1);


client1:
start transaction;

mysql> SELECT @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set (0.00 sec)

mysql> select * from t where id = 1;
+----+------+---------+
| id | name | version |
+----+------+---------+
| 1 | c1 | 1 |
+----+------+---------+
1 row in set (0.00 sec)


client2:
start transaction;
mysql> select * from t where id = 1;
+----+------+---------+
| id | name | version |
+----+------+---------+
| 1 | c1 | 1 |
+----+------+---------+
1 row in set (0.00 sec)


client1:

mysql> update t set name='c1_1',version=2 where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from t where id =1;
+----+------+---------+
| id | name | version |
+----+------+---------+
| 1 | c1_1 | 2 |
+----+------+---------+
1 row in set (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.02 sec)

client2:

//因为repeatable read,只看到client1提交前的数据
mysql> select * from t where id=1 and version=1;
+----+------+---------+
| id | name | version |
+----+------+---------+
| 1 | c1 | 1 |
+----+------+---------+
1 row in set (0.00 sec)


mysql> update t set name='c2',version=3 where id = 1 and version = 1;
//影响行数为0
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0

mysql> update t set name='c2',version=3 where id = 1 and version = 2;
//影响行数为1,说明更新语句的执行是看到提交的数据的
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
总结:在mysql,repeatable read级别的事务,只保证看到的数据是一个镜像.
就算该行数据被别的事务修改,当该事务也对数据进行修改,在提交时也不会产生任何错误.
解决的办法:
1.在应用端使用乐观锁的机制
2.或者使用select .. for update或者SELECT ... LOCK IN SHARE MODE来对数据加.[/code]
相关知识见:
[url]http://dev.mysql.com/doc/refman/5.0/en/innodb-lock-modes.html[/url]
[url]http://dev.mysql.com/doc/refman/5.0/en/innodb-transaction-isolation.html[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值