Autonomous transactions - autonomous transaction
In the Oracle database, sometimes we want to record a running log of a procedure or function, regardless of the normal operation of the end or the end trigger abnormal, should be recorded.
Ends normally there is no problem, but the case triggered an exception, the general procedure or function obviously can not be directly inserted Commit after running log, because the relevant business logic required to trigger abnormal RollBack.
The autonomous transaction can be good to avoid such a problem, that is independent autonomous transaction is a transaction in an open session in which the processing operation does not affect the contents of the same conversation uncommitted transactions.
The following example began to explain:
. 1 - Run_Logs; // operation log table contains fields a dates, logs 2 . 3 - autonomous transactions stored procedures . 4 the CREATE OR the REPLACE PROCEDURE Pro_Run_Logs (error_info the In Varchar2 ) . 5 Is PRAGMA AUTONOMOUS_TRANSACTION; . 6 the BEGIN . 7 the Insert Into Run_Logs (Dates, logs) Values (Sysdate, error_info); . 8 a COMMIT ; . 9 the END ; 10 . 11 - general procedure stored business logic 12 is the CREATE OR the REPLACE pROCEDUREPro_Test (v_oldcustname in VARCHAR2 , v_newcustname in VARCHAR2 ) IS 13 is I Number ; 14 errorException Exception; - affirms abnormal 15 str_err VARCHAR2 ( 100 ); 16 user_err Exception; . 17 the begin 18 is - business logic . 19 20 is the Commit ; 21 is Exception 22 is the When errorException the Then 23 is Pro_Run_Logs (str_err); 24 the WHEN user_errTHEN 25 raise_application_error(-20007, str_err); 26 RAISE; 27 end;