在生产环境中过多的DML操作或者程序的一些错误时会导致数据表锁表现象,出现锁表时可通过以下语句进行处理解锁,但该处理方式只是临时处理,造成数据表死锁的真正原因需要在根源上排查根治。
1 --查看锁表进程SQL语句1: 2 --被锁对象表、数据库对象表、数据session表关联来获取被锁对象对应的是那个session; 3 select sess.sid, 4 sess.serial#, 5 lo.oracle_username, 6 lo.os_user_name, 7 ao.object_name, 8 lo.locked_mode 9 from v$locked_object lo, 10 dba_objects ao, 11 v$session sess 12 where ao.object_id = lo.object_id and lo.session_id = sess.sid; 13 --通过上面的查询获取SID和serial#,替换下面的x,y,就可以解除被锁的状态 14 alter system kill session 'X,XX'; 15 --查看锁表进程SQL语句2: 16 select * from v$session t1, v$locked_object t2 where t1.sid = t2.SESSION_ID; 17 --查看那个用户那个进程照成死锁 18 select b.username,b.sid,b.serial#,logon_time from v$locked_object a,v$session b where a.session_id = b.sid order by b.logon_time; 19 --查看导致锁表的sql语句是那一条 20 select l.session_id sid, 21 s.serial#, 22 l.locked_mode, 23 l.oracle_username, 24 s.user#, 25 l.os_user_name, 26 s.machine, 27 s.terminal, 28 a.sql_text, 29 a.action 30 from v$sqlarea a, v$session s, v$locked_object l 31 where l.session_id = s.sid 32 and s.prev_sql_addr = a.address 33 order by sid, s.serial#; 34 --批量解锁: 35 declare cursor mycur is 36 select b.sid,b.serial# 37 from v$locked_object a,v$session b 38 where a.session_id = b.sid group by b.sid,b.serial#; 39 begin 40 for cur in mycur 41 loop 42 execute immediate ( 'alter system kill session '''||cur.sid || ','|| cur.SERIAL# ||''' '); 43 end loop; 44 end; 45 --批量解锁语句生成 46 SELECT A.OBJECT_NAME, 47 B.SESSION_ID, 48 C.SERIAL#, 49 ( 'alter system kill session '''||b.session_id || ','|| c.SERIAL# ||''' ') 50 AS TEXT, C.PROGRAM, C.USERNAME, C.COMMAND, C.MACHINE, C.LOCKWAIT 51 FROM ALL_OBJECTS A, V$LOCKED_OBJECT B, V$SESSION C WHERE A.OBJECT_ID = B.OBJECT_ID AND C.SID = B.SESSION_ID;