Skip to content

Commit a5a4d12

Browse files
Merge pull request #216 from Trivadis/feature/issue-212-autonomous-transactions
add G-3330: Avoid autonomous transactions.
2 parents 7bc79d9 + aa961cd commit a5a4d12

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# G-3330: Avoid autonomous transactions.
2+
3+
!!! bug "Blocker"
4+
Reliability, Testability
5+
6+
## Reason
7+
8+
>Before we take a look at how autonomous transactions work, I’d like to emphasize that this type of transaction is
9+
a powerful and therefore dangerous tool when used improperly. The true need for an autonomous transaction is very
10+
rare indeed. I would be very suspicious of any code that makes use of them—that code would get extra examination.
11+
It is far too easy to accidentally introduce logical data integrity issues into a system using them. (page 300)
12+
13+
>In my experience, that is the only truly valid use of an autonomous transaction—to log errors or informational
14+
messages in a manner that can be committed independently of the parent transaction. (page 305)
15+
16+
>-- Kyte, Thomas (2013). _Expert Oracle Database Architecture. Third Edition_. Apress.
17+
18+
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.
19+
20+
21+
## Example (bad)
22+
23+
``` sql
24+
create or replace package body dept_api is
25+
procedure ins_dept(in_dept_row in dept%rowtype) is
26+
pragma autonomous_transaction;
27+
begin
28+
insert into dept
29+
values in_dept_row;
30+
commit; -- required by autonomous transaction
31+
end ins_dept;
32+
end dept_api;
33+
/
34+
```
35+
36+
## Example (good)
37+
38+
``` sql
39+
create or replace package body dept_api is
40+
procedure ins_dept(in_dept_row in dept%rowtype) is
41+
begin
42+
insert into dept
43+
values in_dept_row;
44+
-- transaction is commited in calling module
45+
-- after the completion of the unit of work
46+
end ins_dept;
47+
end dept_api;
48+
/
49+
```

docs/9-appendix/appendix.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ n/a | 3150 | Try to use identity columns for surrogate keys. | Critical | | |
5454
n/a | 3160 | Avoid visible virtual columns. | Blocker | | | ✘ | | ✘ | | |
5555
n/a | 3170 | Always use DEFAULT ON NULL declarations to assign default values to table columns if you refuse to store NULL values. | Blocker | | | | | ✘ | | |
5656
n/a | 3180 | Always specify column names instead of positional references in ORDER BY clauses. | Major | ✘ | | | | ✘ | | |
57-
n/a | 3182 | Always specify column names instead of positional references in GROUP BY clauses. | Blocker | | | | | ✘ | | |
57+
n/a | 3182 | Always specify column names/aliases instead of positional references in GROUP BY clauses. | Blocker | | | | | ✘ | | |
5858
n/a | 3183 | Always specify column aliases instead of expressions in GROUP BY clauses. | Minor | | | ✘ | | | | |
5959
n/a | 3185 | Never use ROWNUM at the same query level as ORDER BY. | Blocker | | | | | ✘ | | | ✘
6060
n/a | 3190 | Avoid using NATURAL JOIN. | Blocker | ✘ | | | | ✘ | | |
@@ -63,6 +63,7 @@ n/a | 3195 | Always use wildcards in a LIKE clause. | Blocker | | | ✘ |
6363
n/a | 3220 | Always process saved exceptions from a FORALL statement. | Critical | | | | | ✘ | | | ✘
6464
n/a | 3310 | Never commit within a cursor loop. | Blocker | | ✘ | | | ✘ | | |
6565
n/a | 3320 | Try to move transactions within a non-cursor loop into procedures. | Major | | | ✘ | | | ✘ | | ✘
66+
n/a | 3330 | Avoid autonomous transactions. | Blocker | | | | | ✘ | | | ✘
6667
31 | 4110 | Always use %NOTFOUND instead of NOT %FOUND to check whether a cursor returned data. | Minor | | | ✘ | | | | |
6768
32 | 4120 | Avoid using %NOTFOUND directly after the FETCH when working with BULK OPERATIONS and LIMIT clause. | Blocker | | | | | ✘ | | |
6869
33 | 4130 | Always close locally opened cursors. | Blocker | | ✘ | | | ✘ | | |

0 commit comments

Comments
 (0)