扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
出现锁的根本解决办法不是去手动解锁啊,
创新互联主营白云网站建设的网络公司,主营网站建设方案,成都App定制开发,白云h5小程序设计搭建,白云网站营销推广欢迎白云等地区企业咨询
而是应该去看,是哪个SESSION,哪个sql语句锁的,锁了哪些资源,是不是应该锁定这些资源!
如果有些锁是不必要的,要么commit事务来释放锁,要不就不锁定这些资源!
修改应用才是应该做的!
Session1创建测试表:
SQL create table test (id number (10) not null , name varchar(20), primary key(id));
Table created.
SQL desc test;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER(10)
NAME VARCHAR2(20)
SQL insert into test values(001,'tom');
1 row created.
SQL insert into test values(002,'lisa');
1 row created.
SQL insert into test values(003,'joy');
1 row created.
SQL insert into test values(004,'jia');
1 row created.
查看test表信息
SQL update test set name='xue' where name='joy';
1 row updated.
SQL commit;
Commit complete.
SQL select * from test updata;
ID NAME
---------- --------------------
1 tom
2 lisa
3 xue
4 jia
重新打开session 2:
SQL select * from test;
ID NAME
---------- --------------------
1 tom
2 lisa
3 xue
4 jia
update模拟锁表
SQL update test set name='da' where name='tom';
1 row updated.
注:不提交
Session2查询:
SQL select * from test;
ID NAME
---------- --------------------
1 tom
2 lisa
3 xue
4 jia
查看哪个表被锁
SQL 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;
OWNER
------------------------------
OBJECT_NAME
--------------------------------------------------------------------------------
SESSION_ID LOCKED_MODE
---------- -----------
SYS
TEST
23 3
查看是哪个session引起的
SQL 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;
USERNAME SID SERIAL# LOGON_TIM
------------------------------ ---------- ---------- ---------
SYS 23 23 02-JAN-20
杀掉对应进程
SQL alter system kill session'23,23';
System altered.
其中23为sid,23为serial#.
SQL 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;
no rows selected
SQL 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;
no rows selected
session 1查询:
SQL select * from test;
select * from test
*
ERROR at line 1:
ORA-00028: your session has been killed
SQL select * from test;
select * from test
*
ERROR at line 1:
ORA-01012: not logged on
Process ID: 5366
Session ID: 23 Serial number: 23
重新连接SQL
[oracle@localhost ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Thu Jan 2 11:39:53 2020
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL select * from test updata;
ID NAME
---------- --------------------
1 tom
2 lisa
3 xue
4 jia
1、在做Oracle监听程序测试时,发现帐户已经被锁定。
2、在数据库安装电脑上,点击开始打开运行窗口。
3、在运行窗口输入CMD,调出命令提示符界面。
3、在命令提示符下面,用管理员身份登入到数据库sqlplus / as sysdba。
4、输入解锁命令alter user Scott account unlock后回车。
5、看见用户已更改的字样,表示命令已成功执行。
6、再切换到监听程序验证,原来的ora-28000帐户被锁定的提示已经不存在了。用户解锁成功。
1. 先通过top命令查看产用资源较多的spid号
2.查询当前耗时的会话ID,用户名,sqlID等:
select sid,serial#,machine,username,program,sql_hash_value,sql_id,
to_char(logon_time,'yyyy/mm/dd hh24:mi:ss') as login_time from v$session
where paddr in (select addr from v$process where spid in ('5648612','256523'));
3. 如果上一步sql_id或者 hash_value不为空,则可用v$sqlarea查出当前正在使用的sql
select sql_text
from v$sqltext_with_newlines
where hash_value = hash_value
order by piece;
也可直接使用:
select a.*,b.SQL_TEXT from (
select sid,serial#,machine,username,program,sql_hash_value,sql_id,
to_char(logon_time,'yyyy/mm/dd hh24:mi:ss') as login_time from v$session
where paddr in (select addr from v$process where spid in ('23226'))
) a,v$sql b
where a.sql_id = b.SQL_ID(+)
4.kill占用大资源的session
Alter system kill session 'SID,SERIAL#'
解锁:
1.查询哪些对象被锁:
select object_name,machine,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;
2.下面的语句用来杀死一个进程:
alter system kill session '524,1095'; (其中24,111分别是上面查询出的sid,serial#)
3.再一次查询目前锁定的对象,若发现以上方法不能解除锁定的表,则用以下方法:
3.1 执行下面的语句获得进程(线程)号:
select spid, osuser, s.program
from v$session s,v$process p
where s.paddr=p.addr and s.sid=524 (524是上面的sid)
首先你要知道表锁住了是不是正常锁?因为任何DML语句都会对表加锁。
你要先查一下是那个会话那个sql锁住了表,有可能这是正常业务需求,不建议随便KILL session,如果这个锁表是正常业务你把session kill掉了会影响业务的。
建议先查原因再做决定。
(1)锁表查询的代码有以下的形式:
select count(*) from v$locked_object;
select * from v$locked_object;
(2)查看哪个表被锁
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;
(3)查看是哪个session引起的
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;
(4)查看是哪个sql引起的
select b.username,b.sid,b.serial#,c.* from v$locked_object a,v$session b,v$sql c where a.session_id = b.sid
and b.SQL_ID = c.sql_id and c.sql_id = ''
order by b.logon_time;
(5)杀掉对应进程
执行命令:alter system kill session'1025,41';
其中1025为sid,41为serial#.
你好:这个死锁没办法完全避免,尽量的话在做事物提交的时候,提交完成后在进行其余的同一个表的操作,再就是insert、update等操作尽量能减少就减少。其实正常情况下是很少出现死锁的。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流