Oracle Database autonomy transaction details

Source: Internet
Author: User

Oracle DatabaseOfAutonomous transactionsThis is what we will introduce in this article. Next let's take a look at its mysteries.

A database transaction is a unit operation. Either all operations are successful or all operations fail. In Oracle, a transaction starts from the execution of the first data management language DML) statement until a COMMIT statement is executed, the transaction is committed and saved, or a ROLLBACK statement is executed, stop this operation.

It is difficult to record the error information to the database table because the transaction fails to be re-run, the INSERT statement used to write log entries has not been completed yet.

To address this dilemma, Oracle provides a convenient method, that is, autonomous transactions. An autonomous transaction starts from the current transaction and runs in its own context. They can be submitted or re-run independently without affecting running transactions. As a result, they form an ideal form of writing error log tables. When an error is detected in a transaction, you can insert a row in the error log table and submit it, and then roll back the primary transaction without losing this insert.

Because the autonomous transaction is separated from the primary transaction, it cannot detect the current status of the modified row. It seems that they are always in separate sessions before the primary transaction is committed, and they are unavailable for autonomous transactions. However, in turn, the situation is different: the main transaction can detect the results of self-governing transactions that have been executed.

To create an autonomous transaction, you must use the PRAGMA AUTONOMOUS_TRANSACTION statement in PL/SQL at the top of the anonymous block or in the stored procedure, function, data packet, or trigger definition section. The SQL statements executed in such a module or process are autonomous.

The trigger cannot contain the COMMIT statement, unless the PRAGMA AUTONOMOUS_TRANSACTION flag exists. However, only the statements in the trigger can be committed, but not the primary transaction.

Autonomous transactions:

 
 
  1. create or replace procedure AutoNomouse_Insert is PRAGMA AUTONOMOUS_TRANSACTION;   
  2. begin insert into Msg values('AutoNomouse Insert');   
  3. commit;   
  4. end; 

Non-autonomous transactions:

 
 
  1. CREATE OR REPLACE Procedure NonAutoNomouse_Insert as   
  2. begin insert into Msg Values('NonAutonomouse Insert');   
  3. commit;  
  4. end;  
 
 
  1. SQL> begin  
  2. insert into Msg Values('This Main Info');  
  3. NonAutoNomouse_Insert;  
  4. rollback;  
  5. end  
  6. ;  
  7. / PL/SQL procedure successfully completed SQL> select * from msg; MSG This Main Info  
  8. NonAutonomouse Insert 

Because there is a COMMIT in the process, RULLBACK in the anonymous block does not work. Therefore, the ROLLBACK will affect the entire transaction in a non-autonomous transaction.

Let's look at another situation:

 
 
  1. SQL> delete msg; 2 rows deleted SQL> there is no COMMIT here;
  2. SQL> begin
  3. Insert into Msg Values ('this Main info ');
  4. Rollback; -- ROLLBACK is added here;
  5. NonAutoNomouse_Insert;
  6. Rollback;
  7. End
  8. ;
  9. /PL/SQL procedure successfully completed SQL> select * from msg; MSG This Main Info
  10. NonAutonomouse Insert
  11. NonAutonomouse Insert

Why is there no ROLLBACK (DELETE * from msg? Because the process is a new SESSION, the previous SESSION is normally exited and automatically submitted;

 
 
  1. SQL> commit; Commit complete   
  2. SQL> select * from msg; MSG This Main Info  
  3. NonAutonomouse Insert  
  4. NonAutonomouse Insert SQL> commit; Commit complete SQL> select * from msg; MSG This Main Info  
  5. NonAutonomouse Insert  
  6. NonAutonomouse Insert  

A new SESSION is a meaningless transaction control statement.

 
 
  1. SQL> delete msg;   
  2. SQL>3 rows deleted   
  3. SQL> commit;  
  4. SQL>Commit complete   
  5. SQL> select * from msg;   
  6. MSG 

Let's take a look at autonomous transactions:

 
 
  1. SQL> begin  
  2. insert into Msg Values('This Main Info');  
  3. AutoNomouse_Insert;  
  4. rollback;  
  5. end  
  6. ;  
  7. / PL/SQL procedure successfully completed  
  8.  
  9. SQL> select * from msg; MSG AutoNomouse Insert 

We can see that it is a row of data. Obviously, the first SQL INSERT statement is ROLLBACK, which proves that an autonomous transaction is a transaction independent of the main program and will not affect the control of the main transaction. In addition, in the distributed environment, we often encounter ORA-02064 ERROR, that is, because the main transaction has its own transaction control statement, but the called remote process also has its own transaction control statement, of course, an error will be reported. If we declare the called process as an autonomous transaction, then OK.

Here is an introduction to Oracle Database autonomous transactions. I hope this introduction will be helpful to you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.