OraQA

Oracle Question and Answer

  • Do you have a solution to a problem? Do you have an unanswered question? Login and share it with the Oracle community. More...

Oracle News


Entries RSS feed

Comments RSS feed

Can I commit a transaction inside a database trigger?

February 16th, 2006 By Eddie Awad

Short answer: No. However, using autonomous transactions you can start a “sub-transaction” and commit without affecting the main transaction. An example of when to use autonomous transactions is when you want to audit secured infromation such as payroll or financial data. For more information about autonomous transactions please read the documentation.

One Response to “Can I commit a transaction inside a database trigger?”

  1. Karl Reitschuster Says:

    Hi,
    is use autonomous transactions to log errors/warnings/infos independent if the main transaction fails or not (see for WriteEventlog):

    
      PROCEDURE Logapperror(p_Appcode                 IN Eventlog.Appcode%TYPE,
                            p_Eventmessage            IN Eventlog.Eventmessage%TYPE,
                            p_Modul                   IN Eventlog.Modul%TYPE,
                            p_Method                  IN Eventlog.Method%TYPE,
                            p_Section                 IN Eventlog.Section%TYPE,
                            p_Raise_Application_Error IN BOOLEAN,
                            p_Objectid                IN t_Objectid,
                            p_Objectentity            IN t_Objectentity) IS
    
      BEGIN
    
        Initentry(p_Eventmessage,
                  p_Modul,
                  p_Method,
                  p_Severity => c_Severity_Error,
                  p_Component => c_Component_App,
                  p_Objectid => p_Objectid,
                  p_Objectentity => p_Objectentity);
    
        Eventlog_Entry.Appcode := p_Appcode;
        Eventlog_Entry.Section := Substr(p_Section,
                                         1,
                                         64);
    
        Writeeventlog;
    
        IF (p_Raise_Application_Error) THEN
          Raise_Application_Error(-20001,
                                  p_Modul || '.' || p_Method || ' : ' ||
                                  p_Eventmessage);
    
        END IF;
      END Logapperror;
    
      PROCEDURE Writeeventlog IS
        PRAGMA AUTONOMOUS_TRANSACTION;
      BEGIN
        INSERT INTO Eventlog
        VALUES Eventlog_Entry;
        COMMIT;
      EXCEPTION
        WHEN OTHERS THEN
          -- Housten we have a problem
          ROLLBACK;
          RAISE;
      END Writeeventlog;
    

    i would not use this feature to modify application data via trigger and commit them. There must be a better way/design to avoid this.
    Karl

Leave a Reply

You must be logged in to post a comment.

RSS feed for comments on this question