如何在Oracle PLSQL中提交单个事务

时间:2017-02-08 08:01:11

标签: 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;

我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:7)

看看Autonomous transation。另见演示

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;
 /

执行:

 -- empty the test table
    TRUNCATE TABLE t;

   -- run the parent procedure
     exec parent_block;

   -- check the results
    SELECT * FROM t; 

答案 1 :(得分:2)

您可以使用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;

答案 2 :(得分:0)

创建一个(提交单个交易过程),您可以创建过程并按照注释中的说明使用它。

public function check(){
    $this->load->library('form_validation');
    $data= array('success'=>false, 'message'=>array());
    $this->form_validation->set_rules('username','Username','trim|required');
    $this->form_validation->set_rules('password','Password','required');

    if($this->form_validation->run() == false){
      foreach ($_POST as $key => $value) {
        $data['message'][$key] = form_error($key);
      }
    }
    else{
      $data['success'] = true;
    }
    echo json_encode($data);
}

有关更多信息: https://brainsdazed.blogspot.com/2018/09/oracle-procedure-to-commit-per-dml.html