mysql事务简单测试

时间:2014-05-08 09:28:34   收藏:0   阅读:429

Auth: jin
Date: 20140507

一、事务控制
默认情况下,MySQL是自动提交(autocommit)的,如果需要通过明确的commit和rollblack来提交和回滚事务,那么需要通过明确的事务控制命令来开始事务。
SET AUTOCOMMIT, START TRANSACTION,COMMIT和ROLLBACK等语句支持本地事务。
1、开始一个项新的事务
START TRANSACTION 或BEGIN语句可以开始一项新的事务。
2、提交回滚事务
COMMIT和ROLLBACK用来提交回滚事务
3、在事务提交或回滚之后的操作
CHAIN和RELEASE 字句分别用来定义在事务提交或回滚之后的操作。
CHAIN会立即开启一个新事务,并且和刚才的事务具有相同的隔离级别
RELEASE 会断开和客户端的链接。
4、修改当前的链接的提交方式
SET AUTOCOMMIT 可以修改当前的链接的提交方式,如果设置了SET AUTOCOMMIT=0, 则设置之后的所用事务都需要通过明确的命令提交或回滚。

实例
CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(25) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
注意要innodb,MyISAM不支持事务
session 1
mysql> insert into t1(name) value (‘diege‘);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+----+-------+
| id | name |
+----+-------+
| 1 | diege |
+----+-------+
1 row in set (0.00 sec)

session 2
mysql> select * from t1;
+----+-------+
| id | name |
+----+-------+
| 1 | diege |
+----+-------+
1 row in set (0.00 sec)

mysql> SET AUTOCOMMIT=0;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t1(name) value (‘lily‘);
Query OK, 1 row affected (0.00 sec)

session 1
mysql> select * from t1;
+----+-------+
| id | name |
+----+-------+
| 1 | diege |
+----+-------+
1 row in set (0.00 sec)
数没有新的数据,因为事物没有提交。
session 2
mysql> commit;
Query OK, 0 rows affected (0.02 sec)
session 1
mysql> select * from t1;
+----+-------+
| id | name |
+----+-------+
| 1 | diege |
| 2 | lily |
+----+-------+
2 rows in set (0.00 sec)
提交事物后有数据了

二、事务回滚
在事务中可以通过定义SAVEPOINT,指定回滚事务的一个部分,但是不能指定提交事务的一个部分。事务回滚需要启动一个事务
session A
mysql> select * from t1;
+----+-------+
| id | name |
+----+-------+
| 1 | diege |
| 2 | lily |
+----+-------+
2 rows in set (0.00 sec)
mysql> SET AUTOCOMMIT=0;
Query OK, 0 rows affected (0.00 sec)
开始一个事务
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t1(name) value (‘tom‘);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+----+-------+
| id | name |
+----+-------+
| 1 | diege |
| 2 | lily |
| 3 | tom |
+----+-------+
3 rows in set (0.00 sec)

定义一个savapoint点名为backup1
mysql> savepoint backup1;
Query OK, 0 rows affected (0.00 sec)
再插入一条数据
mysql> insert into t1(name) value (‘json‘);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1;
+----+-------+
| id | name |
+----+-------+
| 1 | diege |
| 4 | json |
| 2 | lily |
| 3 | tom |
+----+-------+
4 rows in set (0.00 sec

回滚到刚才定义的savepoint
mysql> rollback to savepoint backup1;
Query OK, 0 rows affected (0.01 sec)
查看回滚后数据情况
mysql> select * from t1;
+----+-------+
| id | name |
+----+-------+
| 1 | diege |
| 2 | lily |
| 3 | tom |
+----+-------+
3 rows in set (0.00 sec

这时其他session session B数据情况
mysql> select * from t1;
+----+-------+
| id | name |
+----+-------+
| 1 | diege |
| 2 | lily |
+----+-------+
2 rows in set (0.00 sec)
数据没有变化
回到session A提交事务
mysql> commit;
Query OK, 0 rows affected (0.00 sec
session B 再查看数据
mysql> select * from t1;
+----+-------+
| id | name |
+----+-------+
| 1 | diege |
| 2 | lily |
| 3 | tom |
+----+-------+
3 rows in set (0.00 sec)
可以看到新的数据,并且只有savapoint之前的数据。

 

mysql事务简单测试,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!