1、事务用于确保数据的一致性,由一组相关的DML语句组成,该组DML语句所执行的操作要么全部确认,要么全部取消。
2、当执行事务操作(DML)时,Oracle会在被作用的表上加锁,以防止其他用户改变表结构,同时也会在被作用的行上加行锁,以防止其他事务在相应行上执行DML操作。
3、当执行事务提交或事务回滚时,Oracle会确认事务变化或回滚事务、结束事务、删除保存点、释放锁。
4、提交事务(COMMIT)确认事务变化,结束当前事务、删除保存点,释放锁,使得当前事务中所有未决的数据永久改变。
5、保存点(SAVEPOINT)在当前事务中,标记事务的保存点。
6、回滚事务(ROLLBACK)回滚整个事务,删除该事务所定义的所有保存点,释放锁,丢弃所有未决的数据改变。
7、回滚事务到指定的保存点(ROLLBACK TO SAVEPOINT)回滚当前事务到指定的保存点,丢弃该保存点创建后的任何改变,释放锁。
8、当执行DDL、DCL语句,或退出SQL*PLUS时,会自动提交事务;
9、事务期间应避免与使用者互动;
10、查询数据期间,尽量不要启动事务;
11、尽可能让事务持续地越短越好;
12、在事务中尽可能存取最少的数据量
案例:
createorreplaceprocedure stu_proc
(
v_id innumber,
v_name invarchar2,
v_age innumber,
v_msg out varchar2
) as
begin
insertinto student(id, sname, age) values (v_id, v_name, v_age);
commit;
v_msg:='添加成功';
exception
when others then
rollback;
v_msg:='失败成功';
RAISE_APPLICATION_ERROR(-20010, 'ERROR:违反唯一索引约束!');
end;
(
v_id innumber,
v_name invarchar2,
v_age innumber,
v_msg out varchar2
) as
begin
insertinto student(id, sname, age) values (v_id, v_name, v_age);
commit;
v_msg:='添加成功';
exception
when others then
rollback;
v_msg:='失败成功';
RAISE_APPLICATION_ERROR(-20010, 'ERROR:违反唯一索引约束!');
end;
create or replace procedure delete_exceed_bound(playtype varchar2, end07 varchar2 , end08 varchar2)
is
begin
delete lotterydate where lotterydate.playtype=playtype and lotterydate.lotterydate_name>end07 and lotterydate.lotterydate_name like '07%';
delete lotterydate where lotterydate.playtype=playtype and lotterydate.lotterydate_name>end08 and lotterydate.lotterydate_name like '08%';
savepoint p1;
delete province_sell_amounts where province_sell_amounts.play_no=playtype and province_sell_amounts.term>end07 and province_sell_amounts.term like '07%';
delete province_sell_amounts where province_sell_amounts.play_no=playtype and province_sell_amounts.term>end08 and province_sell_amounts.term like '08%';
delete province_winning_prize where province_winning_prize.play_no=playtype and province_winning_prize.term>end07 and province_winning_prize.term like '07%';
delete province_winning_prize where province_winning_prize.play_no=playtype and province_winning_prize.term>end08 and province_winning_prize.term like '08%';
savepoint p2;
delete condition_winning_prize where condition_winning_prize.play_no=playtype and condition_winning_prize.term>end07 and condition_winning_prize.term like '07%';
delete condition_winning_prize where condition_winning_prize.play_no=playtype and condition_winning_prize.term>end08 and condition_winning_prize.term like '08%';
savepoint p3;
delete open_result where open_result.play_no=playtype and open_result.term>end07 and open_result.term like '07%';
delete open_result where open_result.play_no=playtype and open_result.term>end08 and open_result.term like '08%';
exception
when others then
dbms_output.put_line(sqlerrm);
rollback to savepoint p1;
end delete_exceed_bound;
保存点(SAVEPOINT)是事务处理过程中的一个标志,与回滚命令(ROLLBACK)结合使用,主要的用途是允许用户将某一段处理回滚而不必回滚整个事务。
is
begin
delete lotterydate where lotterydate.playtype=playtype and lotterydate.lotterydate_name>end07 and lotterydate.lotterydate_name like '07%';
delete lotterydate where lotterydate.playtype=playtype and lotterydate.lotterydate_name>end08 and lotterydate.lotterydate_name like '08%';
savepoint p1;
delete province_sell_amounts where province_sell_amounts.play_no=playtype and province_sell_amounts.term>end07 and province_sell_amounts.term like '07%';
delete province_sell_amounts where province_sell_amounts.play_no=playtype and province_sell_amounts.term>end08 and province_sell_amounts.term like '08%';
delete province_winning_prize where province_winning_prize.play_no=playtype and province_winning_prize.term>end07 and province_winning_prize.term like '07%';
delete province_winning_prize where province_winning_prize.play_no=playtype and province_winning_prize.term>end08 and province_winning_prize.term like '08%';
savepoint p2;
delete condition_winning_prize where condition_winning_prize.play_no=playtype and condition_winning_prize.term>end07 and condition_winning_prize.term like '07%';
delete condition_winning_prize where condition_winning_prize.play_no=playtype and condition_winning_prize.term>end08 and condition_winning_prize.term like '08%';
savepoint p3;
delete open_result where open_result.play_no=playtype and open_result.term>end07 and open_result.term like '07%';
delete open_result where open_result.play_no=playtype and open_result.term>end08 and open_result.term like '08%';
exception
when others then
dbms_output.put_line(sqlerrm);
rollback to savepoint p1;
end delete_exceed_bound;
如果定义了多个savepoint,当指定回滚到某个savepoint时,那么回滚操作将回滚这个savepoint后面的所有操作(即使后面可能标记了N个savepoint)。
在一段处理中定义了3个savepoint,从第2个savepoint回滚,后面的第3个标记的操作都将被回滚,如果不使用ROLLBACK TO savepoint_name而使用ROLLBACK,将会滚整个事务处理。
相关推荐
oracle 事务 回滚 存储过程 自用
oracle回滚段使用率过高之解决方法 推荐针对不同的情况有多种解决.
oracle 在java中的事务处理和异常回滚。 里面的类直接运行,需要把oracle的数据库连接改改就ok、
花了一晚上时间终于搞出来了Spring整合Mybatis事务回滚(Mysql数据库),控制Service层中的多次插入操作,多次操作整体是一个事务。 里面有缘嘛和jar包,资源为war包,导入即可。运行Test类中的测试代码即可。 建表...
回滚段用于存放数据修改之前的值(包括数据修改之前的位置和值)。回滚段的头部包含正在使用的该回滚段事务的信息。一个事务只能使用一个回滚段来存放它的回滚信息,而一个回滚段可以存放多个事务的回滚信息。
回滚段管理一直是ORACLE数据库管理的一个难题,本文通过实例介绍ORACLE回滚段的概念
自己做的一个简单的spring运用实例,主要是关于事务回滚的一个小例子,希望对还在纠结事务回滚的童靴有点帮助。。。
Spring/SpringMVC/MyBatis整合,可事务回滚
SQL 事务回滚解决回滚无效的情况 BEGIN TRY END TRY CAHTH
基于SpringBoot的轻量级redis事务回滚机制,使用栈和ThreadLocal记录业务链的redis操作,发生异常进行回滚,参考了阿里巴巴Seata AT模式的db回滚策略:补偿回滚,记录前镜像与当前操作语句,反向解析生成补偿动作。...
对于回滚段出现异常,无法启动数据库的处理脚本
执行多条SQL语句对数据库进行操作,如果某个SQL语句执行中出错,那么要对之前的操作进行回滚,代码比较简单。
回滚段探究 在eygle转 在网上看到的,感觉写的不错.... 大家一起学习,如果侵犯了作者版权,请联系本人将删除
对Oracle数据库回滚日志的清理,删除不必要的日志文件。
java 应用程序操作数据库实例,若出现sql异常则事务回滚。
通过登记正在执行的事务性工作类型的资源...它还提供了将多个不稳定资源转换为提交和回滚事务模型所需的架构。在下面的内容中将介绍.NET 2.0中与事务有关的轻量级事务管理、显式事务、TransactionScope类和自动化事务。
主要介绍了使用SpringBoot注解方式处理事务回滚实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
1. 基于Aspectj实现动态数据源...6. 实现事务内切换数据源(支持原生Spring声明式事务哟,仅此一家),并支持多数据源事务回滚(有了它除了跨服务的事务你需要考虑分布式事务,其他都不需要,极大的减少了系统的复杂程度)
正在看的ORACLE教程是:Oracle回滚段的概念,用法和规划及问题的解决。回滚段管理一直是ORACLE数据库管理的一个难题,本文通过实例介绍ORACLE回滚段的概念,用法和规划及问题的解决。 回滚段概述 回滚段用于存放...
批量导入Excel数据到数据库,.NET 2008开发,带数据校验、数据库事务回滚、Excel预览、日志功能的完整开发代码