暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

金仓数据库KingbaseES PLSQL 支持语句级回滚

数据猿 2023-03-31
462

KingbaseES默认如果在PLSQL-block 执行过程中的任何SQL 语句导致错误,都会导致该事务的所有语句都被回滚,而Oracle 则是语句级的回滚。KingbaseES 为了更好的与 Oracle兼容,新增参数ora_statement_level_rollback 。当 ora_statement_level_rollback 为 on时,表示启用了语句级的回滚。

1、Oracle 与 KingbaseES 的差异

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 TABLE
test=# begin
test-#   insert into t values(123);
test-#   insert into t values('a');
test-# exception
test-#   when others then
test-#     commit;
test-# end;
test-# /
ANONYMOUS BLOCK
test=# select * from t;                                                                                                     
 id
----
(0 rows)


2、启用语句级回滚

设置语句级回滚后,其行为与oracle 一致。

test=# set ora_statement_level_rollback=on;
SET
test=# begin
test-#   insert into t values(123);
test-#   insert into t values('a');
test-# exception
test-#   when others then
test-#     commit;
test-# end;
test-# /
ANONYMOUS BLOCK
test=# select * from t;
 id 
-----
 123
(1 row)

注意,PLSQL 的语句级回滚只发生在异常被正确捕获的场景下才有效,如果exception 没有被捕获或没有捕获exception,则还是整个事务回滚。如以下例子:

test=# set ora_statement_level_rollback=on;
test=# delete from t;
DELETE 1
test=# begin
test-#   insert into t values(123);
test-#   insert into t values('a');
test-# exception
test-#   when no_data_found then
test-#     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 statement
test=# select * from t;
 id
----
(0 rows)


「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论