MySQL(8) - MySQL的事务机制
MySQL数据库的事务机制
1.1.事务的概念和特性
1.2.事务的隔离级别
repeatable read是mysql默认的事务隔离级别
#事务A #事务A,临时修改工资,未commit, START TRANSACTION; UPDATE t_emp SET sal=1;#事务B,查看工资,查看不到未提交数据 START TRANSACTION; SELECT empno,ename,sal FROM t_emp; COMMIT;
#事务B,修改事务隔离级别,可以查看未提交数据 SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; START TRANSACTION; SELECT empno,ename,sal FROM t_emp;
#事务A,临时修改工资,未commit, START TRANSACTION; UPDATE t_emp SET sal=1;#事务B,修改事务隔离级别,无法查看未提交数据 SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; START TRANSACTION; SELECT empno,ename,sal FROM t_emp;
#事务A,修改工资,commit, START TRANSACTION; UPDATE t_emp SET sal=1; COMMIT;#事务B,修改事务隔离级别,重复读取涨工资之前的数据 SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ; START TRANSACTION; SELECT empno,ename,sal FROM t_emp;
#事务A,修改工资,commit, START TRANSACTION; UPDATE t_emp SET sal=1; COMMIT;#事务B,修改事务隔离级别序列化,等待上一次事务结束 SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE; START TRANSACTION; SELECT empno,ename,sal FROM t_emp;