Oracle的自治事務

來源:互聯網
上載者:User

標籤:oracle 自治 事務

自治事務(autonomous transaction)允許你建立一個"事務中的事務",它能獨立於其父事務提交或復原。利用自治事務,可以掛起當前執行的事務,開始一個新事務,完成一些工作,然後提交或復原,所有這些都不影響當前所執行事務的狀態。自治事務提供了一種用PL/SQL控制事務的新方法,可用於:

  • 頂層匿名塊;

  • 本地(過程中的過程)、獨立或打包的函數和過程;

  • 物件類型的方法;

  • 資料庫觸發器。

使用例子示範自治事務如何工作

--建立測試表用於儲存資訊[email protected]>create table t ( msg varchar2(25) );Table created.--建立自治事務的預存程序[email protected]>create or replace procedure Autonomous_Insert  2  as        pragma autonomous_transaction;---指示自治事務語句  4  begin  5          insert into t values ( ‘Autonomous Insert‘ );  6          commit;  7  end;  8  /Procedure created.--建立普通預存程序[email protected]>create or replace procedure NonAutonomous_Insert  2  as  3  begin  4          insert into t values ( ‘NonAutonomous Insert‘ );  5          commit;  6  end;  7  /Procedure created.

觀察使用PL/SQL代碼中非自治事務的行為

[email protected]>begin  2          insert into t values ( ‘Anonymous Block‘ );  3          NonAutonomous_Insert;  4          rollback;  5  end;  6  /PL/SQL procedure successfully completed.[email protected]>select * from t;MSG---------------------------------------------------------------------------Anonymous BlockNonAutonomous Insert

可以觀察到非自治事務的過程中的commit也把調用它的父事務也提交了,而父事務中的rollback沒有起到作用。

再觀察使用PL/SQL代碼中非自治事務的行為

[email protected]>delete from t;2 rows deleted.[email protected]>commit;Commit complete.[email protected]>begin        insert into t values ( ‘Anonymous Block‘ );        Autonomous_Insert;        rollback;end;  6  /PL/SQL procedure successfully completed.[email protected]>select * from t;MSG---------------------------------------------------------------------------Autonomous Insert

可以看到,自治事務過程中的commit只把它本身的事務提交了,而對於父事務的語句沒有起到作用,而父事務中的rollback對自治事務中的語句也沒有作用。


本文出自 “DBA Fighting!” 部落格,請務必保留此出處http://hbxztc.blog.51cto.com/1587495/1888528

Oracle的自治事務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.