-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add G-3330: Avoid autonomous transactions
- Loading branch information
1 parent
7bc79d9
commit 0aa4374
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
docs/4-language-usage/3-dml-and-sql/3-transaction-control/g-3330.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# G-3330: Avoid autonomous transactions. | ||
|
||
!!! bug "Blocker" | ||
Reliability, Testability | ||
|
||
## Reason | ||
|
||
>Before we take a look at how autonomous transactions work, I’d like to emphasize that this type of transaction is | ||
a powerful and therefore dangerous tool when used improperly. The true need for an autonomous transaction is very | ||
rare indeed. I would be very suspicious of any code that makes use of them—that code would get extra examination. | ||
It is far too easy to accidentally introduce logical data integrity issues into a system using them. (page 300) | ||
|
||
>In my experience, that is the only truly valid use of an autonomous transaction—to log errors or informational | ||
messages in a manner that can be committed independently of the parent transaction. (page 305) | ||
|
||
>-- Kyte, Thomas (2013). _Expert Oracle Database Architecture. Third Edition_. Apress. | ||
It is most likely not possible to distinguish legitimate uses of autonomous transactions from illegitimate ones via static code analysis. However, since we expect exactly one autonomous transaction per application, the number of false positives is manageable. | ||
|
||
|
||
## Example (bad) | ||
|
||
``` sql | ||
create or replace package body dept_api is | ||
procedure ins_dept(in_dept_row in dept%rowtype) is | ||
pragma autonomous_transaction; | ||
begin | ||
insert into dept | ||
values in_dept_row; | ||
commit; -- required by autonomous transaction | ||
end ins_dept; | ||
end dept_api; | ||
/ | ||
``` | ||
|
||
## Example (good) | ||
|
||
``` sql | ||
create or replace package body dept_api is | ||
procedure ins_dept(in_dept_row in dept%rowtype) is | ||
begin | ||
insert into dept | ||
values in_dept_row; | ||
-- transaction is commited in calling module | ||
-- after the completion of the unit of work | ||
end ins_dept; | ||
end dept_api; | ||
/ | ||
``` |