数据库锁
锁的概念
Oracle中的锁的主要作用就是:防止并发事务对相同的资源(所谓资源是指 表、行、共享的数据结构、 数据字典行等)进行更改的时候,相互破坏。 锁有既有隐式的,也有显式的; 但某用户对某一批数据进行更改,而未提交之前,Oracle会隐式的进行加锁; 当然用户也可以显式的加锁,比如:select … from tableA where … for update NoWait
create table testtab3
( Pk1 number ,
field1 varchar2(200)
);
ALTER TABLE testtab3 ADD CONSTRAINT testtab3_PK PRIMARY KEY(Pk1) ;
insert into testtab3 values (1, 'AAA');
查锁
①方式一
select a.*, C.type, C.LMODE
from v$locked_object a, all_objects b, v$lock c
where a.OBJECT_ID = b.OBJECT_ID
and a.SESSION_ID = c.SID
and b.OBJECT_NAME = 'TESTTAB3'
②方式二
select c.sid, c.serial#, c.username, c.osuser, b.owner, b.object_name, a.locked_mode, p.spid
from v$locked_object a, dba_objects b, v$session c, v$process p
where a.object_id = b.object_id
and a.session_id = c.sid
and c.paddr = p.addr ;
解锁
①解单锁
alter system kill session 'sid, serial#' ;--填写对应的sid, serial#
②批量解锁
declare cursor mycur is
select b.sid,b.serial# from v$locked_object a,v$session b
where a.session_id = b.sid group by b.sid,b.serial#;
begin
for cur in mycur
loop
execute immediate ( 'alter system kill session '''||cur.sid || ','|| cur.SERIAL# ||''' ');
end loop;
end;