Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to commit individual transaction in Oracle PLSQL

I need to write a PL/SQL procedure, within this procedure I need to call another procedure within its own transaction bounds, and commit it regardless of failure or commit of main transaction. In other words I need something like REQUIRES NEW transaction propagation.

Something like:

procedure mainProcedure(arugements) is 
begin
    // some statements
    nestedProcedure(someArguments);
    // some other statements
end;

procedure nestedProcedure(arguments) is
begin
  // start a new transaction
  // some statements, lock some objects!
  // commit the new transaction and release locked objects
end;

How can I achieve this?

like image 477
Amir Pashazadeh Avatar asked Dec 07 '22 19:12

Amir Pashazadeh


1 Answers

Have a look at Autonomous transation. Also see demo

CREATE TABLE t (
 test_value VARCHAR2(25));

CREATE OR REPLACE PROCEDURE child_block IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
   INSERT INTO t
   (test_value)
   VALUES
   ('Child block insert');
  COMMIT; 
END child_block;
 /

CREATE OR REPLACE PROCEDURE parent_block IS

BEGIN
   INSERT INTO t
   (test_value)
   VALUES
   ('Parent block insert');

    child_block;

    ROLLBACK; 
END parent_block;
 /

Execution:

 -- empty the test table
    TRUNCATE TABLE t;

   -- run the parent procedure
     exec parent_block;

   -- check the results
    SELECT * FROM t; 
like image 77
XING Avatar answered Jan 09 '23 13:01

XING