KingbaseES默认如果在PLSQL-block 执行过程中的任何SQL 语句导致错误,都会导致该事务的所有语句都被回滚,而Oracle 则是语句级的回滚。KingbaseES 为了更好的与 Oracle兼容,新增参数ora_statement_level_rollback 。当 ora_statement_level_rollback 为 on时,表示启用了语句级的回滚。
Oracle :在遇到 exception 时,只有触发异常的操作被回滚。
SQL> create table t(id integer);
Table created.
SQL> begin 2 insert into t values(123); 3 insert into t values('a'); 4 exception 5 when others then 6 commit; 7 end; 8 /
PL/SQL procedure successfully completed.
SQL> select * from t;
ID
---------- 123
KingbaseES : 未启用语句级回滚。
test=# create table t(id integer);CREATE TABLEtest=# begintest-# insert into t values(123);test-# insert into t values('a');test-# exceptiontest-# when others thentest-# commit;test-# end;test-# /ANONYMOUS BLOCKtest=# select * from t;
id----(0 rows)
设置语句级回滚后,其行为与oracle 一致。
test=# set ora_statement_level_rollback=on;SETtest=# begintest-# insert into t values(123);test-# insert into t values('a');test-# exceptiontest-# when others thentest-# commit;test-# end;test-# /ANONYMOUS BLOCKtest=# select * from t;
id -----
123(1 row)
注意,PLSQL 的语句级回滚只发生在异常被正确捕获的场景下才有效,如果exception 没有被捕获或没有捕获exception,则还是整个事务回滚。如以下例子:
test=# set ora_statement_level_rollback=on;test=# delete from t;DELETE 1test=# begintest-# insert into t values(123);test-# insert into t values('a');test-# exceptiontest-# when no_data_found thentest-# commit;test-# end;test-# /ERROR: invalid input syntax for type integer: "a"LINE 1: insert into t values('a')QUERY: insert into t values('a')CONTEXT: PL/SQL function inline_code_block line 3 at SQL statementtest=# select * from t;
id----(0 rows)
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70004805/viewspace-2942993/ ,如需转载,请注明出处,否则将追究法律责任。