MySQL
第一种解锁表的方式
查看当前数据库锁表的情况
SELECT * FROM information_schema.INNODB_TRX;
杀掉查询结果中锁表的trx_mysql_thread_id
kill trx_mysql_thread_id
第二种解锁表的方式
查询是否锁表
show OPEN TABLES where In_use > 0;
查询进程
show processlist
杀掉查询到相对应的进程
kill id
补充
查看正在锁的事务
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS;
查看等待锁的事务
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS;
Oracle
前提
操作的前提 用 sys 用户 以 sysdba角色登录
第一种解锁表的方式
查询被锁的表
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;
查询那个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;
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;
解锁表
ALTER system kill session 'sid, serial#';
第二种解锁表的方式
查看被锁的表
SELECT p.spid, a.serial#, c.object_name, b.session_id, b.oracle_username,b.os_user_name
FROM v$process p, v$session a, v$locked_object b, all_objects c
WHERE p.addr = a.paddr
AND a.process = b.process
AND c.object_id = b.object_id;
批量解锁
declare cursor mycur is
select b.sid,b.serial#
from v$locked_object a,v$session b
where a.session_id = b.sid group by b.sid,b.serial#;
begin
for cur in mycur
loop
execute immediate ( 'alter system kill session '''||cur.sid || ','|| cur.SERIAL# ||''' ');
end loop;
end;