标签:
准备表结构:
建立innodb引擎的表t_i
CREATE TABLE `t_i` (
`a` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
建立myisam引擎的表t_m
CREATE TABLE `t_m` (
`a` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8
事务的开启分以下两种方式:
查看默认的自动提交是否开启:
(root@localhost) [wzy]> show variables like ‘autocommit‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
1 row in set (0.00 sec)
当默认情况是自动提交时,需要使用如下命令开启innodb的事务
(root@localhost) [wzy]> begin;
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [wzy]> insert into t_i values(1);
Query OK, 1 row affected (0.00 sec)
(root@localhost) [wzy]> select * from t_i;
+------+
| a |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
(root@localhost) [wzy]> commit;
Query OK, 0 rows affected (0.01 sec)
最后使用commit提交事务
关闭默认的自动提交事务
(root@localhost) [wzy]> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [wzy]> show variables like ‘autocommit‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | OFF |
+---------------+-------+
1 row in set (0.00 sec)
在这种情况下就不用begin开启事务,直接执行DML语句
(root@localhost) [wzy]> insert into t_i values(2);
Query OK, 1 row affected (0.00 sec)
(root@localhost) [wzy]> commit;
Query OK, 0 rows affected (0.00 sec)
注意:如果已经关闭自动提交事务,就不要再使用begin开启事务,否则可能出现就算提交事务,在其它session里也看不到数据。
问题:在同一个事务里分别向innodb表和myisam表里插入数据,但没有提交事务,请问在另外一个事务里能否看到myisam表里的数据?
使用上面创建的两张表测试,在session1里执行如下语句:
(root@localhost) [wzy]> show variables like ‘autocommit‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | OFF |
+---------------+-------+
1 row in set (0.00 sec)
(root@localhost) [wzy]> insert into t_i values(3);
Query OK, 1 row affected (0.00 sec)
(root@localhost) [wzy]> insert into t_m values(1);
Query OK, 1 row affected (0.00 sec)
(root@localhost) [wzy]> select * from t_i;
+------+
| a |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
(root@localhost) [wzy]> select * from t_m;
+------+
| a |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
在session2里执行如下语句,查看结果
(root@localhost) [wzy]> select * from t_i;
+------+
| a |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
(root@localhost) [wzy]> select * from t_m;
+------+
| a |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
可见,在开启事务的情况下,向myisam引擎表插入数据,就算没有提交事务,在另外的事务里也能看到数据。
InnoDB事务的开启
标签:
原文地址:http://my.oschina.net/yangphere/blog/522309