Can we use commit or rollback in trigger? If yes, then how. Please explain with a suitable example?

Answers were Sorted based on User's Feedback



Can we use commit or rollback in trigger? If yes, then how. Please explain with a suitable example?..

Answer / lopa

We can use commit and roll back in a trigger using autonomous transaction.
CREATE TABLE at_test (
id NUMBER NOT NULL,
description VARCHAR2(50) NOT NULL
);

INSERT INTO at_test (id, description) VALUES (1, 'Description for 1');
INSERT INTO at_test (id, description) VALUES (2, 'Description for 2');

SELECT * FROM at_test;

ID DESCRIPTION
---------- --------------------------------------------------
1 Description for 1
2 Description for 2

2 rows selected.

Next, we insert another 8 rows using an anonymous block declared as an autonomous transaction, which contains a commit statement.

DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
FOR i IN 3 .. 10 LOOP
INSERT INTO at_test (id, description)
VALUES (i, 'Description for ' || i);
END LOOP;
COMMIT;
END;
/

PL/SQL procedure successfully completed.

SELECT * FROM at_test;

ID DESCRIPTION
---------- --------------------------------------------------
1 Description for 1
2 Description for 2
3 Description for 3
4 Description for 4
5 Description for 5
6 Description for 6
7 Description for 7
8 Description for 8
9 Description for 9
10 Description for 10

10 rows selected.

SQL>
As expected, we now have 10 rows in the table. If we now issue a rollback statement we get the following result.

ROLLBACK;
SELECT * FROM at_test;

ID DESCRIPTION
---------- --------------------------------------------------
3 Description for 3
4 Description for 4
5 Description for 5
6 Description for 6
7 Description for 7
8 Description for 8
9 Description for 9
10 Description for 10

8 rows selected.

SQL>
The 2 rows inserted by our current session (transaction) have been rolled back, while the rows inserted by the autonomous transactions remain. The presence of the PRAGMA AUTONOMOUS_TRANSACTION compiler directive made the anonymous block run in its own transaction, so the internal commit statement did not affect the calling session. As a result rollback was still able to affect the DML issued by the current statement.

Is This Answer Correct ?    9 Yes 1 No

Can we use commit or rollback in trigger? If yes, then how. Please explain with a suitable example?..

Answer / nishi.swain@gmail.com

using pragma autonomous transaction

Is This Answer Correct ?    4 Yes 0 No

Can we use commit or rollback in trigger? If yes, then how. Please explain with a suitable example?..

Answer / mrityunjay singh

Normally, the usage of a commit or a rollback is not recommended in triggers. But there are certain situations in which we have to use commit or rollback in triggers. One of the prevalent usages of commit in trigger is to avoid the mutating table error encountered during trigger execution prior to 11g. Other way to avoid this error is use of compound trigger introduced in Oracle 11g. To use database control statements in triggers, we have to use compiler directive ‘pragma autonomous_transaction’. When we define trigger as autonomous, it becomes independent and does not belong to current transaction. Since trigger is not part of current transaction, use of commit is allowed in the trigger when declared with pragma. This holds true for any package, procedure and/or function defined with pragma directive. Let us start with an example. We will create two tables and trigger to explain the scenario. Example is based on the one mentioned in oracle documentation.
CREATE TABLE TEST
(
COL1 NUMBER(9) PRIMARY KEY,
COL2 VARCHAR2(30),
COL3 NUMBER(5)
);

CREATE TABLE AUDIT_TEST
(
COL1 NUMBER(9),
COL2 VARCHAR2(30),
OLD_COL3 NUMBER(5),
NEW_COL3 NUMBER(5),
COL4 DATE
);

INSERT INTO TEST
SELECT
rownum,’HELLO..’||to_char(rownum), rownum*1000
FROM user_Tables;

Now create following trigger with commit statement in the trigger body.

CREATE OR REPLACE TRIGGER TUA_TEST
AFTER UPDATE OF COL3
ON TEST
FOR EACH ROW
BEGIN
INSERT INTO audit_test(col1,col2,new_col3,old_col3,col4)
VALUES(: old.col1, : old.col2, : new.col3, : old.col3,sysdate);
COMMIT;
END;
/

Trigger will get created without any error. But whenever we update test table, we run into following error.


SQL> UPDATE TEST
2 SET COL3 = 2000
3 WHERE col1 = 1;
UPDATE TEST
*
ERROR at line 1:
ORA-04092: cannot COMMIT in a trigger
ORA-06512: at “SCOTT.TUA_TEST”, line 5
ORA-04088: error during execution of trigger ‘SCOTT.TUA_TEST’
Now let us create same trigger with pragma autonomous_transaction and execute the update statement again.


CREATE OR REPLACE TRIGGER TUA_TEST
AFTER UPDATE OF COL3
ON TEST
FOR EACH ROW
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
INSERT INTO audit_test(col1,col2,new_col3,old_col3,col4)
VALUES(: old.col1, : old.col2, :new.col3, : old.col3,sysdate);
COMMIT;
END;
/


/* update statement*/
SQL> update test
2 set col3 = 2000
3 where col1 = 1;

1 row updated.
This time update went through fine since trigger is declared autonomous As mentioned earlier, it is not a routine requirement to use control statements in triggers but under certain scenarios where we have to use control statements, this is the way to use it.

Is This Answer Correct ?    2 Yes 0 No

Can we use commit or rollback in trigger? If yes, then how. Please explain with a suitable example?..

Answer / nitinsharma06

Yes we can, the trigger will get compile also if you use commit inside it and that is without using PRAGMA AUTONOUMS TRANSACTION, but when you execute the DML operation it fails to execute.

So in compilation you won't get any error and while execute trigger will throw error.

Is This Answer Correct ?    0 Yes 0 No

Can we use commit or rollback in trigger? If yes, then how. Please explain with a suitable example?..

Answer / santosh kumar

Yes ,You can Commit inside the trigger.

But for this you have to make this trigger transaction to be a Independent transaction from its parent transaction, You can do this by using Pragma. Pragma AUTONOMOUS_TRANSACTION allow you to build the Indepadent(child) Transaction,started by another. Shold be declare in DECLARE section of any subprogram.

Used to make Modular and Resuable Blocks. if you need the example then ask to me.

CREATE OR REPLACE TRIGGER TRIG_ARG
AFTER INSERT ON TAB1
DECLARE
PRAGMA AUTONOMOUNS_TRNASACTION
BEGIN
INSERT INTO LOG VALUES(SYSDATE,'INSERT ON TAB1');
COMMIT;
END;
/

OR

CREATE OR REPLACE TRIGGER t_trigger
AFTER INSERT ON t1 FOR EACH ROW

DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
i PLS_INTEGER;
BEGIN
SELECT COUNT(*)
INTO i
FROM t1;

INSERT INTO t2
VALUES
(i);
COMMIT;
END;
/

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More SQL PLSQL Interview Questions

What is the difference between python and sql?

0 Answers  


what is query cache in mysql? : Sql dba

0 Answers  


What is the purpose of cursors in pl/sql?

0 Answers  


What does closing a cursor do?

0 Answers  


What are different types of indexes?

0 Answers  






What is meant by user defined function?

0 Answers  


What is rule base and cost base optimizer?

2 Answers   Thermotech,


what is the difference between join and union? : Sql dba

0 Answers  


what is the syntax used for partition in tsql? : Transact sql

0 Answers  


how are rank and dense rank being alloted for column with same values over a particular column

1 Answers  


What version is sql?

0 Answers  


In pl/sql, what is bulk binding, and when/how would it help performance?

0 Answers  


Categories