What is Pragma Autonomous transaction. It's Real time
Example

Answer Posted / mohan reddy

Autonomous Transactions
Autonomous transactions allow you to leave the context of
the calling transaction, perform an independant transaction,
and return to the calling transaction without affecting it's
state. The autonomous transaction has no link to the calling
transaction, so only commited data can be shared by both
transactions.
The following types of PL/SQL blocks can be defined as
autonomous transactions:
• Stored procedures and functions.
• Local procedures and functions defined in a PL/SQL
declaration block.
• Packaged procedures and functions.
• Type methods.
• Top-level anonymous blocks.
The easiest way to understand autonomous transactions is to
see them in action. To do this, we create a test table and
populate it with two rows. Notice that the data is not commited.
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.

SQL>
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 ?    20 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is oracle soa suite and what are its different components?

660


What is a value set? List its various types.

658


tell me the scenarios of the independent in reports

2126


Which oracle applications pl/sql standard apis you are familiar, have you used most of them?

644


What is the concept of soa governance?

783






what are the balancing segments in AR?

1662


How do you register report in oracle apps?

638


Explain oracle apps architecture.

784


Are you familiar with internet architecture of oracle apps 11i?

642


What do you understand by soa?

701


WHAT IS CUSTOM VIEW AND CUSTOM COMPONENT?

2601


tell me the scenarios of the independent in reports

2061


q)what are the validations in sql*loader

1755


tell my any difficult situtation you solved in your experience in your company?

1877


What is a flexfield? List out its types.

675