pl/sql savepoint的使用

保存点(savepoint)是事务处理过程中的一个标志,和回滚命令(rollback)结合使用,主要的用途是允许用户将某一段处理回滚而不必回滚整个事务,这在pl/sql研发中还是非常有用处的。

下面的例子中,把savepoint标记在insert语句之前,如果这条insert语句试图将重复的数据保存到emp表中的话,将触发执行预先定义的dup_val_on_index例外处理,在这里面的rollback to do_insert命令将回滚上面的那条insert操作,而不会影响前面的所有操作。

declare

emp_id emp.empno%type;

begin

update emp set ... where empno = emp_id;

delete from emp where ...

...

savepoint do_insert;

insert into emp values (emp_id, ...);

exception

when dup_val_on_index then

rollback to do_insert;

end;

如果你定义了多个savepoint,当你指定回滚到某个savepoint时,那么回滚操作将回滚这个savepoint后面的所有操作(即使后面可能标记了n个savepoint)。例如,在一段处理中

你定义了五个savepoint,从第三个savepoint回滚,后面的第四、第五个标记的操作都将被回滚,如果不使用rollback to savepoint_name而使用rollback,将会滚整个事务处理。

如果你在递归子程式里面定义了一个savepoint, 如果每一个递归层都设置了savepoint. 此时, 你只能回滚到最近的一个savepoint.

savepoint的声明能在同一个事务处理里面重复定义. 他的作用就是把savepoint从上一个位置转移到目前的位置. 因而,执行回滚也只回滚到最近的savepoint.

下面是个例子:

begin

...

savepoint my_point;

update emp set ... where empno = emp_id;

...

savepoint my_point; -- move my_point to current point

insert into emp values (emp_id, ...);

exception

when others then

rollback to my_point;

end;

另外,oracle没有对每个session里面能使用的savepoint个数做限制.

相关推荐
treacle田13 分钟前
达梦数据库-配置本地守护进程dmwatcher服务-记录总结
数据库·达梦数据库·达梦数据库local数据守护
wyt53142927 分钟前
Redis的安装教程(Windows+Linux)【超详细】
linux·数据库·redis
CeshirenTester41 分钟前
从数据库到结构化用例:一套可落地的测试智能体架构
数据库·架构
2301_793804691 小时前
Python数据库操作:SQLAlchemy ORM指南
jvm·数据库·python
不想看见4042 小时前
Qt 项目中实现良好封装(模块化设计)的详细流程指南
数据库·系统架构
mygljx2 小时前
MySQL 数据库连接池爆满问题排查与解决
android·数据库·mysql
Jeremy爱编码2 小时前
软考数据库
数据库
Bdygsl3 小时前
MySQL(1)—— 基本概念和操作
数据库·mysql
zongzizz3 小时前
Oracle 11g 两节点rac在机房断电重启后PL/SQL和客户端连接数据库报错ORA-12541
数据库·oracle
qq_417695053 小时前
实战:用OpenCV和Python进行人脸识别
jvm·数据库·python