oracle如何防止锁表,Oracle

oracle如何防止锁表,Oracle

只有插入有主键约束的列,或者有唯一约束的列时才可能会阻塞。

示例:create table t(x int primary key);

session 1:

insert into t values(1);

session 2:

insert into t values(1);

这时session 2就会发生阻塞。

解决这种情况最好的方法就是在列上绑定一个序列,如果没有这么做,你也可以创建一个before触发器在插入前捕获resource_busy异常来防止阻塞:

create or replace trigger t_trigger

before insert on t for each row

declare

p_lockid pls_integer;

p_resource_busy exception;

pragma exception_init(p_resource_busy,-54);

begin

p_lockid:=dbms_utility.get_hash_value(to_char(:new.x),1,22554);

if(dbms_lock.request(id=>p_lockid,timeout=>0,lockmode=>dbms_lock.x_mode,release_on_commit=>true)<>0)

then

raise p_resource_busy;

end if;

end;

异常处理开销是很大的,所以最好的解决方法还是用序列来处理。

发布者:admin,转转请注明出处:http://www.yc00.com/news/1707150806a1480296.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信