Ami*_*deh 2 oracle plsql transactions
我需要编写一个PL/SQL过程,在这个过程中我需要在自己的事务边界内调用另一个过程,并且无论主事务的失败或提交如何都要提交它.换句话说,我需要像REQUIRES NEW
事务传播这样的东西.
就像是:
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;
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
看看自治转换.另见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;
/
Run Code Online (Sandbox Code Playgroud)
执行:
-- empty the test table
TRUNCATE TABLE t;
-- run the parent procedure
exec parent_block;
-- check the results
SELECT * FROM t;
Run Code Online (Sandbox Code Playgroud)
小智 7
您可以使用pragma autonomous_transaction
. 它的作用与您所需要的相同。但不要忘记,在子事务中您将看不到上述事务的任何更新。
procedure mainProcedure(arugements) is
begin
// some statements
nestedProcedure(someArguments);
// some other statements
end;
procedure nestedProcedure(arguments) is
pragma autonomous_transaction;
begin
// start a new transaction
// some statements, lock some objects!
// commit the new transaction and release locked objects
commit;
end;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1707 次 |
最近记录: |