KingbaseES PLSQL 支持语句级回滚

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

1、Oracle 与 KingbaseES 的差异

Oracle :在遇到 exception 时,只有触发异常的操作被回滚。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
  end;
  8  /
 
PL/SQL procedure successfully completed.
 
SQL> select * from t;
 
        ID
----------
       123

KingbaseES : 未启用语句级回滚。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 一致。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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,则还是整个事务回滚。如以下例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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)

  

posted @   KINGBASE研究院  阅读(105)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示