当前位置:首页 » 编程语言 » oracle锁表解锁sql
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

oracle锁表解锁sql

发布时间: 2023-05-10 06:27:51

‘壹’ oracle查询锁表与解锁情况提供解决方案

如果发生了锁等待,我们可能更想知道是谁锁了表而引起谁的等待
以下的语句可以查询到谁锁了表:
复制代码
代码如下:
SELECT
/*+
rule
*/
s.username,
decode(l.type,'TM','TABLE
LOCK',
'TX','ROW
LOCK',
NULL)
LOCK_LEVEL,
o.owner,o.object_name,o.object_type,
s.sid,s.serial#,s.terminal,s.machine,s.program,s.osuser
FROM
v$session
s,v$lock
l,dba_objects
o
WHERE
l.sid
=
s.sid
AND
l.id1
=
o.object_id(+)
AND
s.username
is
NOT
NULL
以下的语句可以查询到谁在等待:
复制代码
代码如下:
SELECT
/*+
rule
*/
lpad('
',decode(l.xisn
,0,3,0))||l.oracle_username
User_name,
o.owner,o.object_name,o.object_type,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
ORDER
BY
o.object_id,xisn
DESC
解锁命令:
复制代码
代码如下:
alter
system
kill
session
'sid,serial#'
1).
复制代码
代码如下:
select
LOCK_INFO.OWNER
||
'.'
||
LOCK_INFO.OBJ_NAME
as
"已锁物件名称",
--物件名称(已经被锁住)
LOCK_INFO.SUBOBJ_NAME
as
"已锁子物件名称",
--
子物件名称(已经被锁住)
SESS_INFO.MACHINE
as
"机器名称",
--
机器名称
LOCK_INFO.SESSION_ID
as
"会话ID",
--
会话SESSION_ID
SESS_INFO.SERIAL#
as
"会话SERIAL#",
--
会话SERIAL#
SESS_INFO.SPID
as
"OS系统的SPID",
--
OS系统的SPID
(SELECT
INSTANCE_NAME
FROM
V$INSTANCE)
"实例名SID",
--实例名SID
LOCK_INFO.ORA_USERNAME
as
"ORACLE用户",
--
ORACLE系统用户名称
LOCK_INFO.OS_USERNAME
as
"OS用户",
--
作业系统用户名称
LOCK_INFO.PROCESS
as
"进程编号",
--
进程编号
LOCK_INFO.OBJ_ID
as
"对象ID",
--
对象ID
LOCK_INFO.OBJ_TYPE
as
"对象类型",
--
对象类型
SESS_INFO.LOGON_TIME
as
"登录时间",
--
登录时间
SESS_INFO.PROGRAM
as
"程式名称",
--
程式名称
SESS_INFO.STATUS
as
"会话状态",
--
会话状态
SESS_INFO.LOCKWAIT
as
"等待锁",
--
等待锁
SESS_INFO.ACTION
as
"动作",
--
动作
SESS_INFO.CLIENT_INFO
as
"客户资讯"
--
客户资讯
from
(select
obj.OWNER
as
OWNER,
obj.OBJECT_NAME
as
OBJ_NAME,
obj.SUBOBJECT_NAME
as
SUBOBJ_NAME,
obj.OBJECT_ID
as
OBJ_ID,
obj.OBJECT_TYPE
as
OBJ_TYPE,
lock_obj.SESSION_ID
as
SESSION_ID,
lock_obj.ORACLE_USERNAME
as
ORA_USERNAME,
lock_obj.OS_USER_NAME
as
OS_USERNAME,
lock_obj.PROCESS
as
PROCESS
from
(select
*
from
all_objects
where
object_id
in
(select
object_id
from
v$locked_object))
obj,
v$locked_object
lock_obj
where
obj.object_id
=
lock_obj.object_id)
LOCK_INFO,
(select
SID,
SERIAL#,
LOCKWAIT,
STATUS,
(select
spid
from
v$process
where
addr
=
a.paddr)
spid,
PROGRAM,
ACTION,
CLIENT_INFO,
LOGON_TIME,
MACHINE
from
v$session
a)
SESS_INFO
where
LOCK_INFO.SESSION_ID
=
SESS_INFO.SID
order
by
LOCK_INFO.SESSION_ID;
2).
复制代码
代码如下:
select
sql_text
from
v$sqltext
where
address
in
(select
sql_address
from
v$session
where
sid
=
&sid)
order
by
piece;
3).
复制代码
代码如下:
ALTER
SYSTEM
KILL
SESSION
'会话ID,会话SERIAL#';
4).
kill
-9
OS系统的SPID

‘贰’ oracle数据库表被锁了不能操作怎么办

首先你要知道表锁住了是不是正常锁?因为任何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#.

‘叁’ oracle 锁表、解锁的语句

1.正确方法用 select xxx from table for update; <- 锁表
直接用update table set xxx的话,只能锁row
不要commit的意思是不提交该transaction。若commit或rollback,则释放所有lock资源
2.commit = confirm 确认并提交该次会话中的修改
rollback = cancel 取消该次会话

‘肆’ sql数据库如何解锁呢

你先查一下你的数据表示不是锁表了。

select sess.sid,
sess.serial#,
lo.oracle_username,
lo.os_user_name,
ao.object_name,
lo.locked_mode
from v$locked_object lo,
dba_objects ao,
v$session sess
where ao.object_id = lo.object_id and lo.session_id = sess.sid

通过以上sql就可以知道哪个进程、序列,oracle用户名、操作系统用户名、表名、锁表模式几个字段

下面一步就是将改锁表的进程和序列杀掉了,执行下面的语句即可。
alter system kill session '1020,38953' --(1020,就是执行第一步语句得到的sid字段值,38953就是serial#字段值)

详细的请参照:网页链接

‘伍’ oracle锁表多长时间自动解锁

oracle锁表不会自动解锁。根据查询相关资料显示,oracle锁表想要解锁需要余带逗,找系统管理员,行败终止掉你竖卖之前的session,就可以了。会rollback回来。有管理者权限的话,通过客户端,一样可以做到的。

‘陆’ orcal数据库表被锁了怎么解锁

1、在做Oracle监听程序测试时,发现帐户已经被锁定。

‘柒’ ORACLE 如何查询被锁定表及如何解锁释放session

ORACLE
EBS操作某一个FORM界面,或者后台数据库操作某一个表时发现一直出于"假死"状态,可能是该表被某一用户锁定,导致其他用户无法继续操作
复制代码
代码如下:--锁表查询SQLSELECT
object_name,
machine,
s.sid,
s.serial#
FROM
gv$locked_object
l,
dba_objects
o,
gv$session
s
WHERE
l.object_id
=
o.object_id
AND
l.session_id
=
s.sid;
找到被锁定的表,解锁
复制代码
代码如下:--释放SESSION
SQL:
--alter
system
kill
session
'sid,
serial#';
您可能感兴趣的文章:mysql
事务处理及表锁定深入简析

‘捌’ oracle查询资源占用,锁表解锁

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)

‘玖’ oracle 表被锁住 怎么解锁

一般先查询并找到被锁定的表,解锁代码如下:

--释放SESSION余缓SQL:
--altersystemkillsession'sid,serial#';
ALTERsystem信慎kill竖坦模session'23,1647';

‘拾’ 查询Oracle锁表语句以及解锁语句

查询锁表: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 FROM  v$locked_object l,

      all_objects      o,

      v$session       码腊 s WHERE l.object_id = o.object_id ANd l.session_id = s.sid ORDER BY sid,

      s.serial#;

解锁:ALTER system KILL session 'sid,serial#';

查询锁住原因:SELECT b.sid oracleID,b.username 登录Oracle用户名,b.serial#,spid 操作系统ID,paddr, 

sql_text 正在执行的SQL,b.machine 计算机名 FROM v$process a, v$session b, v$sqlarea c 

WHERE a.addr = b.paddr AND b.sql_hash_value = c.hash_value  and b.USERNAME='FKPHIS24';