We can't COMMIT/ROLLBACK in DML triggers because transaction is handled manually after DML statement. However, database triggers seems to be an exception. For example, suppose there's a database trigger:
SQL> CREATE TABLE log (timestamp DATE, operation VARCHAR2(2000));
Table created.
SQL> CREATE TABLE honnikery (col1 NUMBER);
Table created.
SQL> create or replace trigger honni_trig
2 after insert on honnikery
3 begin
4 insert into log values(sysdate,'insert on tab1');
5 commit;
6 end;
7 /
Trigger created.
SQL> INSERT INTO honnikery VALUES (1);
INSERT INTO honnikery VALUES (1)
*
ERROR at line 1:
ORA-04092: cannot COMMIT in a trigger
ORA-06512: at "SCOTT.HONNI_TRIG", line 3
ORA-04088: error during execution of trigger 'SCOTT.HONNI_TRIG'
As workaround, one can use autonomous transactions. Autonomous transactions execute separate from the current transaction.
create or replace trigger honnikery_trig
after insert on honnikery
declare
PRAGMA AUTONOMOUS_TRANSACTION;
begin
insert into log values(sysdate,'insert on honnikery');
commit;
end;
Trigger created.
SQL> INSERT INTO honnikery VALUES (1);
1 row created.
No comments:
Post a Comment