标签:class == upd eve 分段 默认 sql 失败 variable
不支持事务的引擎:MyISAM、MEMORY/HEAP
查看某个引擎是否支持事务:show engines\G;
提交事务:
显式提交:commit
隐试提交:
BEGIN
START TRANSACTION
SET AUTOCOMMIT=1 / 其他非事务语句(DDL/DCL)。
| 隔离级别 | 脏读 | 不可重复读 | 幻读 |
| Read Uncommitted(读未提交) | 可能 | 可能 | 可能 |
| Read Commited(读已提交) | 不可能 | 可能 | 可能 |
| Repeatable read(可重复读) | 不可能 | 不可能 | 不可能(InnoDB特定条件下可能) |
| Serializable(串行) | 不可能 | 不可能 | 不可能 |
| 读未提交,Read uncommitted,RU隔离级别 | |
| session1 | session2 |
| SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED | |
| begin | begin |
| select * from t1 where c2=2 | 2 | 2 | 2 | |
|
| select * from t1 where c2=2 | 2 | 2 | 2 | |
|
| update t1 set c3=30 where c2=2 | |
| select * from t1 where c2=2 | 2 | 2 | 30 | 读到了t1还未提交的数据 |
|
| 读已提交,Read committed,RC隔离级别 | |
| session1 | session2 |
|
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED
|
|
| begin | begin |
| select * from t1 where c2=2 | 2 | 2 | 2 | |
|
| select * from t1 where c2=2 | 2 | 2 | 2 | |
|
| update t1 set c3=30 where c2=2 | |
| select * from t1 where c2=2 | 2 | 2 | 2 | t1未提交时,还是读取到旧数据 |
|
| select * from t1 where c2=2 for update 希望读取到最新版本,被阻塞,需等待 |
|
| commit | |
| select * from t1 where c2=2 | 2 | 2 | 30 | t1提交后,读取到新数据 |
|
| RC下的幻读 | |
| session1 | session2 |
| SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED | |
| begin | begin |
| select * from t1 where c2=2 | 2 | 2 | 2 | |
|
| select * from t1 where c2=2 | 2 | 2 | 2 | |
|
| insert into t1select 4,2,2 | |
| select * from t1 where c2=2 | 2 | 2 | 2 | t1未提交时,还是读取到旧数据 |
|
| select * from t1 where c2=2 for update 行锁等待 |
|
| commit | |
| select * from t1 where c2=2 | 2 | 2 | 2 | | 4 | 2 | 2 | ==>幻读 t1提交后,是否加了for update都可以读取到新数据 |
|
| 可重复读,Repeatable read,RR隔离级别 | |
| session1 | session2 |
| SET SESSION TRANSACTION ISOLATION LEVEL RPEATABLE READ | |
| begin | begin |
| select * from t1 where c2=2 | 2 | 2 | 2 | |
|
| select * from t1 where c2=2 | 2 | 2 | 2 | |
|
| update t1 set c3=30 where c2=2; commit; |
|
| select * from t1 where c2=2 | 2 | 2 | 2 | |
|
| select * from t1 where c2=2 for update | 2 | 2 | 30 | |
|
| select * from t1 where c2=2 | 2 | 2 | 2 | |
|
| RR隔离级别可以避免幻读 | |
| session1 | session2 |
| SET SESSION TRANSACTION ISOLATION LEVEL RPEATABLE READ | |
| begin | begin |
| select * from t1 where c2=2 | 2 | 2 | 2 | |
|
| select * from t1 where c2=2 | 2 | 2 | 2 | |
|
| insert into t1select 4,2,2 ERROR 1250(HY000):Lock wait timeout exceeded;try restarting transaction 锁住c2=2这个gap,不允许有新的写入 |
|
| 串行,serializable | |
| session1 | session2 |
| SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE | |
| begin | begin |
| select * from t1 where c2=2 | 2 | 2 | 2 | |
|
| select * from t1 where c2=2 | 2 | 2 | 2 | |
|
| update t1 set c3=20 where c2=20 ERROR 1250(HY000):Lock wait timeout exceeded;try restarting transaction |
|
标签:class == upd eve 分段 默认 sql 失败 variable
原文地址:http://www.cnblogs.com/liang545621/p/7518279.html