1.写事物大多数人都是知道的,可是读事物晓得的就不多了,下面通过四个例子来说明只读事物4种隔离级别的区别
操作sql
SET autocommit=0;
BEGIN;
SELECT token_id FROM authentication_columbus WHERE id=0 FOR UPDATE;
INSERT INTO authentication_columbus(token_id,partner_name) VALUES ('666','radar1');
SELECT SLEEP(6);
SELECT token_id FROM authentication_columbus WHERE partner_name='radar1';
COMMIT;
1.read-uncommitted
set tx_isolation='read-uncommitted';
SELECT @@tx_isolation;
BEGIN;
SELECT token_id FROM authentication_columbus WHERE partner_name='radar1';
SELECT SLEEP(10);
SELECT token_id FROM authentication_columbus WHERE partner_name='radar1';
COMMIT;
2.read-committed
set tx_isolation='read-committed';
SELECT @@tx_isolation;
BEGIN;
SELECT token_id FROM authentication_columbus WHERE partner_name='radar1';
SELECT SLEEP(10);
SELECT token_id FROM authentication_columbus WHERE partner_name='radar1';
COMMIT;
3.repeatable-read
set tx_isolation='repeatable-read';
SELECT @@tx_isolation;
BEGIN;
SELECT token_id FROM authentication_columbus WHERE partner_name='radar1';
SELECT SLEEP(10);
SELECT token_id FROM authentication_columbus WHERE partner_name='radar1';
COMMIT;
大家试试,这几种执行结果的区别就秒懂了。
4.serializable
set tx_isolation='serializable';
SET autocommit=0;
BEGIN;
INSERT INTO authentication_columbus(token_id,partner_name) VALUES ('666','radar1');
SELECT SLEEP(10);
COMMIT;
set tx_isolation='serializable';
SELECT @@tx_isolation;
BEGIN;
SELECT COUNT(*) FROM authentication_columbus;
COMMIT;
set tx_isolation='serializable';
SELECT COUNT(*) FROM authentication_columbus;
通过执行以上三条,就知道了,开启读事物与不开启的区别,serializable 是加了一把表锁呀。