Package Supporting Autonomous Transaction
An autonomous transaction can be defined in a stored procedure or function in a package. The identifier of an autonomous transaction is PRAGMA AUTONOMOUS_TRANSACTION. The syntax of an autonomous transaction is the same as that of creating a stored procedure or function in a package. The following is an example.
-- Create a table. gaussdb=# drop table if exists t2; gaussdb=# create table t2(a int, b int); CREATE TABLE gaussdb=# insert into t2 values(1,2); INSERT 0 1 gaussdb=# select * from t2; a | b ---+--- 1 | 2 (1 row) -- Create a stored procedure or function in a package that contains autonomous transactions. gaussdb=# CREATE OR REPLACE PACKAGE autonomous_pkg AS PROCEDURE autonomous_4(a int, b int); FUNCTION autonomous_32(a int ,b int) RETURN int; END autonomous_pkg; / CREATE PACKAGE gaussdb=# CREATE OR REPLACE PACKAGE body autonomous_pkg AS PROCEDURE autonomous_4(a int, b int) AS DECLARE num3 int := a; num4 int := b; PRAGMA AUTONOMOUS_TRANSACTION; BEGIN insert into t2 values(num3, num4); END; FUNCTION autonomous_32(a int ,b int) RETURN int AS DECLARE PRAGMA AUTONOMOUS_TRANSACTION; BEGIN insert into t2 values(a, b); return 1; END; END autonomous_pkg; / CREATE PACKAGE BODY -- Create a common stored procedure that calls a stored procedure or function from a package that contains autonomous transactions. gaussdb=# CREATE OR REPLACE PROCEDURE autonomous_5(a int, b int) AS DECLARE va int; BEGIN insert into t2 values(666, 666); autonomous_pkg.autonomous_4(a,b); va := autonomous_pkg.autonomous_32(a + 1, b + 1); rollback; END; / CREATE PROCEDURE -- Call a common stored procedure. gaussdb=# select autonomous_5(11,22); autonomous_5 -------------- (1 row) -- View the table result. gaussdb=# select * from t2 order by a; a | b ----+---- 1 | 2 11 | 22 12 | 23 (3 rows)
In the preceding example, a stored procedure or function in a package containing autonomous transactions is finally executed in a transaction block that is rolled back, which directly illustrates a characteristic of the autonomous transaction, that is, rollback of the primary transaction does not affect content that has been committed by an autonomous transaction.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot