Oracle PL/SQL之SET TRANSACTION READ ONLY(事務隔離性)

來源:互聯網
上載者:User

SET TRANSACTION READ ONLY實際上是實現資料庫四大事務(ACID)中隔離性(Isolation)的一種手段,用來將資料的讀一致性定在某一時間點,即不管其他事務如何更改資料(不能在當前session中再使用自治事務),在當前事務中進行查詢的結果始終不變。由於Oracle的讀一致性是通過undo段來實現的,所以如果在此期間DML修改的資料量很大而undo空間設定過小可能會導致ORA-01555(快照過舊)錯誤。

 

Test Code:

Step 1, @session 1(SET TRANSACTION READ ONLY):

Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0<br />Connected as tuser1</p><p>SQL> select * from dept;</p><p>DEPTNO DNAME LOC<br />------ -------------- -------------<br /> 10 ACCOUNTING NEW YORK01<br /> 20 RESEARCH DALLAS<br /> 30 SALES CHICAGO<br /> 40 OPERATIONS BOSTON</p><p>SQL> SET TRANSACTION READ ONLY;</p><p>Transaction set

 

Step 2, @session 2(DML, insert a new record):

SQL> select * from dept;</p><p>DEPTNO DNAME LOC<br />------ -------------- -------------<br /> 10 ACCOUNTING NEW YORK01<br /> 20 RESEARCH DALLAS<br /> 30 SALES CHICAGO<br /> 40 OPERATIONS BOSTON</p><p>SQL> insert into dept(deptno) values(50);</p><p>1 row inserted</p><p>SQL> commit;</p><p>Commit complete</p><p>SQL> select * from dept;</p><p>DEPTNO DNAME LOC<br />------ -------------- -------------<br /> 10 ACCOUNTING NEW YORK01<br /> 20 RESEARCH DALLAS<br /> 30 SALES CHICAGO<br /> 40 OPERATIONS BOSTON<br /> 50

 

Step 3, @session 1(query the same object and get the same result):

SQL> select * from dept;</p><p>DEPTNO DNAME LOC<br />------ -------------- -------------<br /> 10 ACCOUNTING NEW YORK01<br /> 20 RESEARCH DALLAS<br /> 30 SALES CHICAGO<br /> 40 OPERATIONS BOSTON

 

Step 4, @session 1(autonomous transaction is not permitted):

SQL> declare<br /> 2 pragma AUTONOMOUS_TRANSACTION;<br /> 3 begin<br /> 4 update dept set loc = loc || '-XXX' where deptno=20;<br /> 5 end;<br /> 6 /</p><p>declare<br />pragma AUTONOMOUS_TRANSACTION;<br />begin<br />update dept set loc = loc || '-XXX' where deptno=20;<br />end;</p><p>ORA-06519: active autonomous transaction detected and rolled back<br />ORA-06512: at line 6</p><p>SQL> select * from dept;</p><p>DEPTNO DNAME LOC<br />------ -------------- -------------<br /> 10 ACCOUNTING NEW YORK01<br /> 20 RESEARCH DALLAS<br /> 30 SALES CHICAGO<br /> 40 OPERATIONS BOSTON</p><p>SQL>

 

Step 5, @session 1(commit current transaction and we get the change):

SQL> commit;</p><p>Commit complete</p><p>SQL> select * from dept;</p><p>DEPTNO DNAME LOC<br />------ -------------- -------------<br /> 10 ACCOUNTING NEW YORK01<br /> 20 RESEARCH DALLAS<br /> 30 SALES CHICAGO<br /> 40 OPERATIONS BOSTON<br /> 50

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.