一聚教程网:一个值得你收藏的教程网站

热门教程

Oracle只读事务测试学习笔记

时间:2022-06-29 09:59:38 编辑:袖梨 来源:一聚教程网

我们知道直接执行select语句并不会开启事务的,那么怎么开启只读事务呢。

 

 代码如下 复制代码

--演示只读事务

--创建一张表
create table t_read_only(id number primary key)

--插入一条数据
insert into t_read_only(id) values(1);

--开启只读事务(只读事务中只有查询,不能出现DML语句)
set transaction read only;
declare
  type to_table_type is table of number index by binary_integer;
 
  v_numbers to_table_type;
begin
  select id bulk collect into v_numbers from t_read_only;--通过批量数据加载到数组中
  --打印出查询出来的结果
  for i in 1..v_numbers.count loop
      dbms_output.put_line(v_numbers(i));
  end loop;
   --不提交事务执行下面的select语句,然后重新打开sql窗口再次执行select语句。
end;

--将数据插入后执行查询
select * from t_read_only;


--重新开启一个sql窗口,执行insert一条数据
begin
  insert into t_read_only(id) values(2);
  commit;--提交事务
end;


我们可以发现事务2提交的数据库并没有在事务一中查询出来,那么只读事务的中只能看到在开启事务时候的数据,对于其它事务的操作是不可见的。

热门栏目