Oracle中三种隔离代码(Isolation)

Oracle隔离属于乐观,相信别人不会改变数据。故Oracle的隔离是来隔离自己的。

与MsSql形成显明对比,Mssql是悲观的。故Mssql的隔离是来隔离别人的。

Oracle读永远不会加锁。而在MsSql中,除过(Read UnCommitted)外的读都会加锁。

Oracle读操作永远不会等待。而在MsSql中,只要对方加了X锁,读就必须要等待。

看看下面的示例即可。

Open 2 Sessions:

Session1 (It is not user sys, remember this)

Session2

1、Read Commited

S1:select id,name from tb1; 

        --------------

        1,'john'

        --------------

S1:set transaction isolation level read commited; //default mode

S2:update tb1 set name = 'tom' where id=1;

S1:select id,name from tb1; 

        -------------- 

        1,'john'  //it has not been changed;

        -------------- 

S2:commit;

S1:select id,name from tb1; 

        -------------- 

        1,'tom'  //it is changed now;

        -------------- 

2、Read Only

S1:select id,name from tb1; 

        --------------  

        1,'john'

        --------------  

S1:set transaction read only;

S2:update tb1 set name = 'tom' where id=1;

S2:commit;

S1:select id,name from tb1; 

        --------------  

       1,'john'  //it has not been changed;

        --------------  

S1:update tb1 set name = 'tom' where id=1

        --------------  

        error: you can't insert/delete/update under readonly mode

        --------------  

S1:commit;

S1:select id,name from tb1; 

        --------------  

        1,'Tom'  //it is changed now;

        --------------  

3、Serializable

S1:select id,name from tb1; 

        --------------   

        1,'john'

        --------------   

S1:set transaction isolation level serializable;

S2:update tb1 set name = 'tom' where id=1;

S2:commit;

S1:select id,name from tb1; 

        --------------   

        1,'john'  //it has not been changed;

        --------------   

S1:insert tb1(id,name) values (2,'mike');

S1:select id,name from tb1; 

        --------------   

        1,'john'  //it has not been changed;

        2,'mike'  //added by S1;

        --------------    

S1:commit;

S1:select id,name from tb1; 

        --------------   

        1,'tom'  //it is changed now;

        2,'mike'  //added by S1;

        --------------   

你可能感兴趣的:(Oracle中三种隔离代码(Isolation))