Oracle中锁
1.表的锁:
--以下几个为相关表
SELECT * FROM v$lock;
SELECT * FROM v$sqlarea;
SELECT * FROM v$session;
SELECT * FROM v$process ;
SELECT * FROM v$locked_object;
SELECT * FROM all_objects;
SELECT * FROM v$session_wait;
--查看被锁的表
select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects b where b.object_id = a.object_id;
--查看那个用户那个进程照成死锁
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;
--查看连接的进程
SELECT sid, serial#, username, osuser FROM v$session;
--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语句其实产生了两个锁,一个是表锁,一个是行锁。
--杀掉进程 sid,serial#
alter system kill session'***,***';
2.存储过程的锁:
解决方法如下:
1:查V$DB_OBJECT_CACHE
SELECT * FROM V$DB_OBJECT_CACHE WHERE name='CRM_LASTCHGINFO_DAY' AND LOCKS!='0';
注意:CRM_LASTCHGINFO_DAY为存储过程的名称。
2:按对象查出sid的值
select /*+ rule*/ SID from V$ACCESS WHERE object='CRM_LASTCHGINFO_DAY';
注意:CRM_LASTCHGINFO_DAY为存储过程的名称。
3:查sid,serial#
SELECT SID,SERIAL#,PADDR FROM V$SESSION WHERE SID='刚才查到的SID';
4:alter system kill session 'sid值,serial#值' immediate;
3.死锁的查找以及处理:
通过检查数据库表,能够检查出是哪一条语句被死锁,产生死锁的机器是哪一台。
1.用dba用户执行以下语句:
select username,lockwait,status,machine,program from v$session where sid in
(select session_id from v$locked_object);
2.用dba用户执行以下语句,可以查看到被死锁的语句。
select sql_text from v$sql where hash_value in
(select sql_hash_value from v$session where sid in
(select session_id from v$locked_object))
3.查询死锁的信息:
select * from v$session t1, v$locked_object t2 where t1.sid = t2.SESSION_ID;
4.PL/SQL中新建命令窗口执行如下的命令(也可在Xshell中执行该命令)
Alter system kill session 'sid,serial#;