Using “nested” transactions in oracle

后端 未结 2 819
余生分开走
余生分开走 2020-12-20 02:50

I have troubles with transactions in Oracle. I have some procedures like this:

create or replace procedure myschema.DataSave(v_value IN NUMBER)
as
begin

SET         


        
相关标签:
2条回答
  • 2020-12-20 03:16

    Exceptions are restricted to the program group they are raised in.

    create or replace procedure myschema.DataSave(v_value IN NUMBER)
    as
    
    ex_dml_error EXCEPTION;
    begin
    
    
    begin
    insert/update/delete...
    exception 
        when OTHERS then ex_dml_error;
    end;
    
    COMMIT;
    
    EXCEPTION 
       WHEN ex_dml_error THEN
         ROLLBACK;
    end;
    /
    
    0 讨论(0)
  • 2020-12-20 03:22

    Oracle doesn't support nested transactions. If a transaction commits, it commits. That's why you generally don't want to commit (or rollback) a transaction in a stored procedure, that makes it difficult to reuse the procedure elsewhere if your transaction semantics differ.

    You can, however, declare a savepoint at the beginning of your procedure and rollback to that savepoint in the case of an error. If you then remove the commit, then the transaction is solely controlled by the application code not by the database code

    begin
      savepoint beginning_of_proc;
    
      insert/update/delete...
    
    exception 
      when OTHERS then 
        rollback to beginning_of_proc;
        raise;
    end;
    

    In this case, though, my bias would be not to have a savepoint in the code, not to have a rollback, and not to catch the exception unless you're doing something useful with it. Just do the DML, let any exceptions get thrown, and handle them in your application.

    0 讨论(0)
提交回复
热议问题