[Oracle] oracle查询被锁的sql语句并解锁


本文总阅读量

实验1:

1
2
3
4
session1:
20:01:35 SYS@ boston> select * from scott.t1 for update;
session2:
20:01:13 SCOTT@ boston> update t1 set empno=8000 where empno=7566;

session2执行update语句的时候卡主了,需要等待session1把锁释放掉。
session3查看当前被锁的会话:

1
2
3
4
5
20:01:16 SYS@ boston> select a.object_name,b.session_id,c.serial#,'alter system kill session '''||b.session_id||','||c.serial#||'''; ' as a,c.program,c.username,c.command,c.machine,c.lockwait from all_objects a,v$locked_object b,v$session c where a.object_id=b.object_id and c.sid=b.session_id;

OBJECT_NAME SESSION_ID SERIAL# A PROGRAM USERNAME COMMAND MACHINE LOCKWAIT
T1 208 2941 alter system kill session '208,2941'; sqlplus@dax-mysql-slave (TNS V1-V3) SYS 0 dax-mysql-slave
T1 301 957 alter system kill session '301,957'; sqlplus@dax-mysql-slave (TNS V1-V3) SCOTT 6 dax-mysql-slave

查看被锁的对象

1
2
3
4
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,c.sql_text FROM v$locked_object l, all_objects o, v$session s, v$sqlarea c WHERE l.object_id = o.object_id AND l.session_id = s.sid and s.sql_id = c.sql_id ORDER BY sid,s.serial#;
SID SERIAL# 锁模式 登录用户 登录机器用户名 机器名 终端用户名 被锁对象名 登录数据库时间 会话正在执行的SQL语句
301 957 3 SCOTT oracle dax-mysql-slave pts/2 T1 2019:02:28 19:18:06
update t1 set empno=8000 where empno=7566

查询结果如上所示,因为session1的查询已经结束,session2的sql还被锁住,只能查询到session2的sql语句,因此我们要杀掉sessoin1的会话,让session2继续正常运行。如果杀掉了sesison2,其他会话再进来执行dml变更操作,会话仍然被卡主。

实验2:
rollback回滚上面的操作,之后继续实验2

1
2
3
4
5
6
7
session2:
20:13:00 SCOTT@ boston> update t1 set empno=8000 where empno=7566;
4097 rows updated.
Elapsed: 00:00:00.09

session1:
20:14:13 SYS@ boston> select * from scott.t1 for update;

session1执行select操作被卡主
session3查看被锁的语句

1
2
3
4
5
6
7
20:10:44 SYS@ boston> select a.object_name,b.session_id,c.serial#,'alter system kill session '''||b.session_id||','||c.serial#||'''; ' as a,c.program,c.username,c.command,c.machine,c.lockwait from all_objects a,v$locked_object b,v$session c where a.object_id=b.object_id and c.sid=b.session_id; 

OBJECT_NAME SESSION_ID SERIAL# A PROGRAM USERNAME COMMAND MACHINE LOCKWAIT
------------------------------ ---------- ---------- --------------------------------------------------------------------------------------------------------------- ------------------------------------------------ ------------------------------ ---------- ---------------------------------------------------------------- ----------------
T1 107 6131 alter system kill session '107,6131'; sqlplus@dax-mysql-slave (TNS V1-V3) SYS 3 dax-mysql-slave 00000000BD230AE0
T1 301 957 alter system kill session '301,957'; sqlplus@dax-mysql-slave (TNS V1-V3) SCOTT 0 dax-mysql-slave
Elapsed: 00:00:00.02
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
20:16:01 SYS@ boston> 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 登录数据库时间 ,
c.sql_text 会话正在执行的sql语句
FROM v$locked_object l, all_objects o, v$session s ,v$sqlarea c
WHERE l.object_id = o.object_id
AND l.session_id = s.sid
and s.sql_id = c.sql_id
ORDER BY sid, s.serial#;
SID SERIAL# 锁模式 登录用户 登录机器用户名 机器名 终端用户名 被锁对象名 登录数据库时间 会话正在执行的SQL语句
107 6131 3 SYS oracle dax-mysql-slave pts/0 T1 2019:02:28 20:14:13 select * from scott.t1 for update
Elapsed: 00:00:00.01

查询结果为session1的select语句被堵塞,处于正在执行的状态,session2的update语句已经执行完成,需要commit才能把锁释放掉。

目录
  1. 1. 实验1:

Proudly powered by Hexo and Theme by Lap
本站访客数人次
© 2020 zeven0707's blog