oracle数据库锁定与解锁
oracle数据库锁定与解锁
查看锁定项:表锁定、行锁定等,如下代码
SELECT /*+ rule */ s.username, decode(l.type,'TM','TABLE LOCK', 'TX','ROW LOCK', NULL) LOCK_LEVEL, o.owner,o.object_name,o.object_type, s.sid,s.serial#,s.terminal,s.machine,s.program,s.osuser FROM v$session s,v$lock l,dba_objects o WHERE l.sid = s.sid AND l.id1 = o.object_id(+) AND s.username is NOT Null
解锁语句如下
alter system kill session'sid,serial';
1.查出锁定object的session的信息以及被锁定的object名 SELECT l.session_id sid, s.serial#, l.locked_mode,l.oracle_username, l.os_user_name,s.machine, s.terminal, o.object_name, s.logon_time FROM v$locked_object l, all_objects o, v$session s WHERE l.object_id = o.object_id AND l.session_id = s.sid ORDER BY sid, s.serial# ; --2.查出锁定表的session的sid, serial#,os_user_name, machine name, terminal和执行的语句 --比上面那段多出sql_text和action SELECT l.session_id sid, s.serial#, l.locked_mode, l.oracle_username, s.user#, l.os_user_name,s.machine, s.terminal,a.sql_text, a.action FROM v$sqlarea a,v$session s, v$locked_object l WHERE l.session_id = s.sid AND s.prev_sql_addr = a.address ORDER BY sid, s.serial#; --3.查出锁定表的sid, serial#,os_user_name, machine_name, terminal,锁的type,mode SELECT s.sid, s.serial#, s.username, s.schemaname, s.osuser, s.process, s.machine, s.terminal, s.logon_time, l.type FROM v$session s, v$lock l WHERE s.sid = l.sid AND s.username IS NOT NULL ORDER BY sid;
这个语句将查找到数据库中所有的DML语句产生的锁,还可以发现,
任何DML语句其实产生了两个锁,一个是表锁,一个是行锁。
杀锁命令
alter system kill session 'sid,serial#' SELECT /*+ rule */ s.username, decode(l.type,'TM','TABLE LOCK', 'TX','ROW LOCK', NULL) LOCK_LEVEL, o.owner,o.object_name,o.object_type, s.sid,s.serial#,s.terminal,s.machine,s.program,s.osuser FROM v$session s,v$lock l,dba_objects o WHERE l.sid = s.sid AND l.id1 = o.object_id(+) AND s.username is NOT NULL
合并锁定项及批量导出语句方法如下:
SET ECHO ON -------是否显示执行的命令内容 SET FEEDBACK OFF --------是否显示* rows selected SET HEADING OFF --------是否显示字段名称 SET VERIFY OFF ---------是否显示替代变量被替代前后的语句。 SET TRIMSPOOL ON --------去字段空格 SET PAGESIZE 1000 --------页面大小 SET LINESIZE 50 --------尽量根据需要来定,生成文件大小 DEFINE FIL='D:\EXP.TXT' PROMPT * * * SPOOLING TO &FIL SPOOL ON SPOOL &FIL select 'alter system kill session ',''''||trim(t2.sid)||','||trim(t2.serial#)||''';' from v$locked_object t1,v$session t2 where t1.session_id=t2.sid order by t2.logon_time;//该部分只查询出SID和serial#值; SPOOL OFF
如果想知道锁用了哪个回滚段,还可以关联到V$rollname,其中xidusn就是回滚段的USN
col user_name format a10 col owner format a10 col object_name format a10 col object_type format a10 SELECT /*+ rule */ lpad(' ',decode(l.xidusn ,0,3,0))||l.oracle_username User_name, o.owner,o.object_name,o.object_type,s.sid,s.serial# FROM v$locked_object l,dba_objects o,v$session s WHERE l.object_id=o.object_id AND l.session_id=s.sid ORDER BY o.object_id,xidusn DESC