Plsql 事务范围之外的查询-Oracle

Plsql 事务范围之外的查询-Oracle,plsql,oracle11g,Plsql,Oracle11g,在存储过程中,insert语句是否可能超出事务范围? 因此,有一个存储的进程,作为事务的一部分写入一些插入 这样做的原因是写入审核表并保留审核记录,即使存储的进程失败或出现异常 澄清,抱歉,如果不清楚,我正在为程序的每个操作编写多个审核,以便跟踪它做了什么和失败了什么。当出现异常时,不仅仅是一次审核 CREATE OR REPLACE PROCEDURE sampleProc IS BEGIN start a transaction INSERT to table 1

在存储过程中,insert语句是否可能超出事务范围? 因此,有一个存储的进程,作为事务的一部分写入一些插入 这样做的原因是写入审核表并保留审核记录,即使存储的进程失败或出现异常

澄清,抱歉,如果不清楚,我正在为程序的每个操作编写多个审核,以便跟踪它做了什么和失败了什么。当出现异常时,不仅仅是一次审核

CREATE OR REPLACE PROCEDURE sampleProc
IS
BEGIN

  start a transaction
      INSERT to table 1
      write to audit table about insert 1
      INSERT to table 2
      write to audit table about insert 2
      INSERT to table 3
      write to audit table about insert 3
      INSERT to table 4
      write to audit table about insert 4

     if there is an exception - rollback except audit 
     all ok? commit.

END;
/

是的,有一个选项可以在与主事务同时运行的单独事务中执行操作。详见

例如:

create table logs(creation_date date default sysdate, msg varchar2(4000));

create or replace procedure log_proc(sMessage varchar2)
is
  pragma autonomous_transaction;
begin
  insert into logs(msg)
  values(sMessage);
  commit; -- don't forget to commit in this separate transaction
end;
/

begin
  log_proc('some message');
  rollback;
end;
/

select * from logs

是的,有一个选项可以在与主事务同时运行的单独事务中执行操作。详见

例如:

create table logs(creation_date date default sysdate, msg varchar2(4000));

create or replace procedure log_proc(sMessage varchar2)
is
  pragma autonomous_transaction;
begin
  insert into logs(msg)
  values(sMessage);
  commit; -- don't forget to commit in this separate transaction
end;
/

begin
  log_proc('some message');
  rollback;
end;
/

select * from logs
您所追求的是PRAGMA autonomy_事务,它在单独的会话中启动使用它的模块

您的代码类似于:

CREATE OR REPLACE PROCEDURE sample_proc
AS
  procedure audit_insert (<params>)
  is
    pragma autonomous_transaction;
  begin
    <log details>
    commit;
  end audit_insert;
BEGIN
  <INSERT to table 1>;
  audit_insert(...);

  <INSERT to table 2>;
  audit_insert(...);

  ...

EXCEPTION
  when others then
    rollback;
    raise;
END sample_proc;
/
这样做意味着您的审计细节将被保存,而不管调用代码是否成功

注意:我已经创建了审计插入作为样本过程的子过程。您最好将代码作为包中的单个过程,而不是一个或多个过程。

您所追求的是PRAGMA autonomy\u事务,它在单独的会话中启动模块使用它

您的代码类似于:

CREATE OR REPLACE PROCEDURE sample_proc
AS
  procedure audit_insert (<params>)
  is
    pragma autonomous_transaction;
  begin
    <log details>
    commit;
  end audit_insert;
BEGIN
  <INSERT to table 1>;
  audit_insert(...);

  <INSERT to table 2>;
  audit_insert(...);

  ...

EXCEPTION
  when others then
    rollback;
    raise;
END sample_proc;
/
这样做意味着您的审计细节将被保存,而不管调用代码是否成功

注意:我已经创建了审计插入作为样本过程的子过程。您最好将代码作为包中的单个过程,而不是作为一个或多个过程