Autonomous Transactions

Before the release of PL/SQL 8.1, each Oracle session could have at most one active transaction at a given time. In other words, any and all changes made in your session had to be either saved or erased in their entirety. This restriction has long been considered a drawback in the PL/SQL world. Developers have requested the ability to execute and save or cancel certain DML statements (INSERT, UPDATE, DELETE) without affecting the overall session’s transaction.

You can now accomplish this goal with the autonomous transaction feature of PL/SQL 8.1 and above. When you define a PL/SQL block ( anonymous block, procedure, function, packaged procedure, packaged function, database trigger) as an autonomous transaction, you isolate the DML in that block from the caller’s transaction context. That block becomes an independent transaction that is started by another transaction, referred to as the main transaction.

Within the autonomous transaction block, the main transaction is suspended. You perform your SQL operations, commit or roll back those operations, and resume the main transaction. This flow of transaction control is illustrated in Figure 13-4.

Flow of transaction control between main, nested, and autonomous transactions

Figure 13-4. Flow of transaction control between main, nested, and autonomous transactions

Defining Autonomous Transactions

There isn’t much involved in defining a PL/SQLblock as an autonomous transaction. You simply ...

Get Oracle PL/SQL Programming, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.