time | session_1 | session_2 |
---------------------------------------------------------> | mysql> select * from tt3; Empty set (0.00 sec) | mysql> select * from tt3; Empty set (0.00 sec) |
mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> insert into tt3 values('1',1); Query OK, 1 row affected (0.03 sec) | ||
mysql> select * from tt3; Empty set (0.00 sec) | ||
mysql> commit; Query OK, 0 rows affected (0.05 sec) | ||
mysql> select * from tt3; +------+------+ | id | name | +------+------+ | 1 | 1.00 | +------+------+ 1 row in set (0.00 sec) | ||
mysql> insert into tt3 values('2',2); Query OK, 1 row affected (0.04 sec) 这个事务是按照自动提交执行的 | ||
mysql> select * from tt3; +------+------+ | id | name | +------+------+ | 1 | 1.00 | | 2 | 2.00 | +------+------+ 2 rows in set (0.00 sec) | ||
mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> insert into tt3 values('3',3); Query OK, 1 row affected (0.00 sec) mysql> commit and chain; Query OK, 0 rows affected (0.05 sec) 自动开始一个新的事务 mysql> insert into tt3 values('4',4); Query OK, 1 row affected (0.00 sec) | ||
mysql> select * from tt3; +------+------+ | id | name | +------+------+ | 1 | 1.00 | | 2 | 2.00 | | 3 | 3.00 | +------+------+ 3 rows in set (0.00 sec) | ||
mysql> commit; Query OK, 0 rows affected (0.06 sec) | ||
mysql> select * from tt3; +------+------+ | id | name | +------+------+ | 1 | 1.00 | | 2 | 2.00 | | 3 | 3.00 | | 4 | 4.00 | +------+------+ 4 rows in set (0.00 sec) |
time | session_1 | session_2 |
---------------------------------------------------------> | mysql> select * from tt3; Empty set (16.65 sec) | mysql> select * from tt3; Empty set (16.65 sec) |
mysql> lock table tt3 write; Query OK, 0 rows affected (0.00 sec) | ||
mysql> select * from tt3; 等待 | ||
mysql> insert into tt3 values('1',1); Query OK, 1 row affected (0.07 sec) | 等待 | |
mysql> rollback; Query OK, 0 rows affected (0.00 sec) | 等待 | |
mysql> start transaction; Query OK, 0 rows affected (0.00 sec) | 等待 | |
mysql> select * from tt3; +------+------+ | id | name | +------+------+ | 1 | 1.00 | +------+------+ 1 row in set (37.71 sec) 开始一个事务时,表锁被释放。 对lock方式加的表锁,不能通过rollback进行回滚。 |
time | session_1 | session_2 |
---------------------------------------------------------> | mysql> select * from tt3; +------+------+ | id | name | +------+------+ | 2 | 2.00 | | 3 | 3.00 | | 4 | 4.00 | +------+------+ 3 rows in set (0.00 sec) | mysql> select * from tt3; +------+------+ | id | name | +------+------+ | 2 | 2.00 | | 3 | 3.00 | | 4 | 4.00 | +------+------+ 3 rows in set (0.00 sec) |
mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> delete from tt3 where id = '2'; Query OK, 1 row affected (0.00 sec) | ||
mysql> select * from tt3; +------+------+ | id | name | +------+------+ | 3 | 3.00 | | 4 | 4.00 | +------+------+ 3 rows in set (0.00 sec) | mysql> select * from tt3; +------+------+ | id | name | +------+------+ | 2 | 2.00 | | 3 | 3.00 | | 4 | 4.00 | +------+------+ 3 rows in set (0.00 sec) | |
mysql> savepoint test; Query OK, 0 rows affected (0.00 sec) mysql> delete from tt3 where id = '3'; Query OK, 1 row affected (0.00 sec) | ||
mysql> select * from tt3; +------+------+ | id | name | +------+------+ | 4 | 4.00 | +------+------+ 3 rows in set (0.00 sec) | mysql> select * from tt3; +------+------+ | id | name | +------+------+ | 2 | 2.00 | | 3 | 3.00 | | 4 | 4.00 | +------+------+ 3 rows in set (0.00 sec) | |
mysql> rollback to savepoint test; Query OK, 0 rows affected (0.00 sec) | ||
mysql> select * from tt3; +------+------+ | id | name | +------+------+ | 3 | 3.00 | | 4 | 4.00 | +------+------+ 2 rows in set (0.00 sec) | mysql> select * from tt3; +------+------+ | id | name | +------+------+ | 2 | 2.00 | | 3 | 3.00 | | 4 | 4.00 | +------+------+ 3 rows in set (0.00 sec) | |
mysql> commit; Query OK, 0 rows affected (0.05 sec) | ||
mysql> select * from tt3; +------+------+ | id | name | +------+------+ | 3 | 3.00 | | 4 | 4.00 | +------+------+ 2 rows in set (0.00 sec) | mysql> select * from tt3; +------+------+ | id | name | +------+------+ | 3 | 3.00 | | 4 | 4.00 | +------+------+ 2 rows in set (0.00 sec) |
欢迎光临 中科因仑“3+1”工程特种兵精英论坛 (http://bbs.enlern.com/) | Powered by Discuz! X3.4 |