oracle锁等级以及解锁

以下是两遍关于锁的介绍的文章,第一篇介绍锁等级以及常用操作,第二篇主要介绍了oracle中两个用以查询数据库任意对象的两个视图

一:

http://www.cnblogs.com/lguyss/archive/2009/12/17/1626700.html

ORACLE里锁有以下几种模式:
0:none
1:null 空
2:Row-S 行共享(RS):共享表锁,sub share 
3:Row-X 行独占(RX):用于行的修改,sub exclusive 
4:Share 共享锁(S):阻止其他DML操作,share
5:S/Row-X 共享行独占(SRX):阻止其他事务操作,share/sub exclusive 
6:exclusive 独占(X):独立访问使用,exclusive

数字越大锁级别越高, 影响的操作越多。

1级锁有:Select,有时会在v$locked_object出现。
2级锁有:Select for update,Lock For Update,Lock Row Share 
select for update当对话使用for update子串打开一个游标时,所有返回集中的数据行都将处于行级(Row-X)独占式锁定,其他对象只能查询这些数据行,不能进行update、delete或select for update操作。
3级锁有:Insert, Update, Delete, Lock Row Exclusive
没有commit之前插入同样的一条记录会没有反应, 因为后一个3的锁会一直等待上一个3的锁, 我们必须释放掉上一个才能继续工作。
4级锁有:Create Index, Lock Share
locked_mode为2,3,4不影响DML(insert,delete,update,select)操作, 但DDL(alter,drop等)操作会提示ora-00054错误。
00054, 00000, "resource busy and acquire with NOWAIT specified"
// *Cause: Resource interested is busy.
// *Action: Retry if necessary.
5级锁有:Lock Share Row Exclusive 
具体来讲有主外键约束时update / delete ... ; 可能会产生4,5的锁。
6级锁有:Alter table, Drop table, Drop Index, Truncate table, Lock Exclusive

以DBA角色, 查看当前数据库里锁的情况可以用如下SQL语句:

col owner for a12
col object_name for a16
select b.owner,b.object_name,l.session_id,l.locked_mode
from v$locked_object l, dba_objects b
where b.object_id=l.object_id
/

select t2.username,t2.sid,t2.serial#,t2.logon_time 
from v$locked_object t1,v$session t2 
where t1.session_id=t2.sid order by t2.logon_time
/

如果有长期出现的一列,可能是没有释放的锁。我们可以用下面SQL语句杀掉长期没有释放非正常的锁:

alter system kill session ‘sid,serial#‘;

如果出现了锁的问题, 某个DML操作可能等待很久没有反应。

当你采用的是直接连接数据库的方式,也不要用OS系统命令 $kill process_num 或者 $kill -9 process_num来终止用户连接,因为一个用户进程可能产生一个以上的锁, 杀OS进程并不能彻底清除锁的问题。

二:

http://blog.itpub.net/519536/viewspace-571440/

1. DBA_OBJECTS / ALL_OBJECTS / USER_OBJECTS(OBJ)视图是非常非常常用的数据视图,可以获得数据库中任意的对象
[email protected]> desc dba_objects;
 Name                                      Null?    Type
 ----------------------------------------- -------- -----------------------------
 OWNER                                              VARCHAR2(30)
 OBJECT_NAME                                        VARCHAR2(128)
 SUBOBJECT_NAME                                     VARCHAR2(30)
 OBJECT_ID                                          NUMBER
 DATA_OBJECT_ID                                     NUMBER
 OBJECT_TYPE                                        VARCHAR2(19)
 CREATED                                            DATE
 LAST_DDL_TIME                                      DATE
 TIMESTAMP                                          VARCHAR2(19)
 STATUS                                             VARCHAR2(7)
 TEMPORARY                                          VARCHAR2(1)
 GENERATED                                          VARCHAR2(1)
 SECONDARY                                          VARCHAR2(1)

[email protected]> select count(*) from dba_objects;

COUNT(*)
----------
     11441

[email protected]> select count(*) from obj;

COUNT(*)
----------
      6751

[email protected]> select count(*) from user_objects;

COUNT(*)
----------
      6751

[email protected]> select count(*) from all_objects;

COUNT(*)
----------
     11376

[email protected]> conn sec/sec
Connected.

[email protected]> select object_name,object_type from obj;

OBJECT_NAME                    OBJECT_TYPE
------------------------------ -------------------
TEST                           TABLE
STATS_TEST                     TABLE

2.通过查看catalog.sql获得oracle创建DBA_OBJECTS数据字典视图的语句
create or replace view DBA_OBJECTS
    (OWNER, OBJECT_NAME, SUBOBJECT_NAME, OBJECT_ID, DATA_OBJECT_ID,
     OBJECT_TYPE, CREATED, LAST_DDL_TIME, TIMESTAMP, STATUS,
     TEMPORARY, GENERATED, SECONDARY)
as
select u.name, o.name, o.subname, o.obj#, o.dataobj#,
       decode(o.type#, 0, ‘NEXT OBJECT‘, 1, ‘INDEX‘, 2, ‘TABLE‘, 3, ‘CLUSTER‘,
                      4, ‘VIEW‘, 5, ‘SYNONYM‘, 6, ‘SEQUENCE‘,
                      7, ‘PROCEDURE‘, 8, ‘FUNCTION‘, 9, ‘PACKAGE‘,
                      11, ‘PACKAGE BODY‘, 12, ‘TRIGGER‘,
                      13, ‘TYPE‘, 14, ‘TYPE BODY‘,
                      19, ‘TABLE PARTITION‘, 20, ‘INDEX PARTITION‘, 21, ‘LOB‘,
                      22, ‘LIBRARY‘, 23, ‘DIRECTORY‘, 24, ‘QUEUE‘,
                      28, ‘JAVA SOURCE‘, 29, ‘JAVA CLASS‘, 30, ‘JAVA RESOURCE‘,
                      32, ‘INDEXTYPE‘, 33, ‘OPERATOR‘,
                      34, ‘TABLE SUBPARTITION‘, 35, ‘INDEX SUBPARTITION‘,
                      40, ‘LOB PARTITION‘, 41, ‘LOB SUBPARTITION‘,
                      42, NVL((SELECT distinct ‘REWRITE EQUIVALENCE‘
                               FROM sum$ s
                               WHERE s.obj#=o.obj#
                                     and bitand(s.xpflags, 8388608) = 8388608),
                              ‘MATERIALIZED VIEW‘),
                      43, ‘DIMENSION‘,
                      44, ‘CONTEXT‘, 46, ‘RULE SET‘, 47, ‘RESOURCE PLAN‘,
                      48, ‘CONSUMER GROUP‘,
                      51, ‘SUBSCRIPTION‘, 52, ‘LOCATION‘,
                      55, ‘XML SCHEMA‘, 56, ‘JAVA DATA‘,
                      57, ‘SECURITY PROFILE‘, 59, ‘RULE‘,
                      60, ‘CAPTURE‘, 61, ‘APPLY‘,
                      62, ‘EVALUATION CONTEXT‘,
                      66, ‘JOB‘, 67, ‘PROGRAM‘, 68, ‘JOB CLASS‘, 69, ‘WINDOW‘,
                      72, ‘WINDOW GROUP‘, 74, ‘SCHEDULE‘, 79, ‘CHAIN‘,
                      81, ‘FILE GROUP‘,
                     ‘UNDEFINED‘),
       o.ctime, o.mtime,
       to_char(o.stime, ‘YYYY-MM-DD:HH24:MI:SS‘),
       decode(o.status, 0, ‘N/A‘, 1, ‘VALID‘, ‘INVALID‘),
       decode(bitand(o.flags, 2), 0, ‘N‘, 2, ‘Y‘, ‘N‘),
       decode(bitand(o.flags, 4), 0, ‘N‘, 4, ‘Y‘, ‘N‘),
       decode(bitand(o.flags, 16), 0, ‘N‘, 16, ‘Y‘, ‘N‘)
from sys.obj$ o, sys.user$ u
where o.owner# = u.user#
  and o.linkname is null
  and (o.type# not in (1  /* INDEX - handled below */,
                      10 /* NON-EXISTENT */)
       or
       (o.type# = 1 and 1 = (select 1
                              from sys.ind$ i
                             where i.obj# = o.obj#
                               and i.type# in (1, 2, 3, 4, 6, 7, 9))))
  and o.name != ‘_NEXT_OBJECT‘
  and o.name != ‘_default_auditing_options_‘
  and bitand(o.flags, 128) = 0
union all
select u.name, l.name, NULL, to_number(null), to_number(null),
       ‘DATABASE LINK‘,
       l.ctime, to_date(null), NULL, ‘VALID‘,‘N‘,‘N‘, ‘N‘
from sys.link$ l, sys.user$ u
where l.owner# = u.user#
/

3.oracle官方文档中关于ALL_OBJECTS的描述

ALL_OBJECTS

ALL_OBJECTS describes all objects accessible to the current user.

Related Views

  • DBA_OBJECTS describes all objects in the database.
  • USER_OBJECTS describes all objects owned by the current user. This view does not display the OWNER column.
Column Datatype NULL Description
OWNER VARCHAR2(30) NOT NULL Owner of the object
OBJECT_NAME VARCHAR2(30) NOT NULL Name of the object
SUBOBJECT_NAME VARCHAR2(30)   Name of the subobject (for example, partition)
OBJECT_ID NUMBER NOT NULL Dictionary object number of the object
DATA_OBJECT_ID NUMBER   Dictionary object number of the segment that contains the object
      Note:
OBJECT_ID and DATA_OBJECT_ID display data dictionary metadata. Do not
confuse these numbers with the unique 16-byte object identifier (object ID) that the Oracle Database assigns to row objects in object tables in the system.
OBJECT_TYPE VARCHAR2(19)   Type of the object (such as TABLE, INDEX)
CREATED DATE NOT NULL Timestamp for the creation of the object
LAST_DDL_TIME DATE NOT NULL Timestamp for the last modification of the object resulting from a DDL statement (including grants and revokes)
TIMESTAMP VARCHAR2(20)   Timestamp for the specification of the object (character data)
STATUS VARCHAR2(7)   Status of the object (VALID, INVALID, or N/A)
TEMPORARY VARCHAR2(1)   Whether the object is temporary (the current session can see only data that it placed in this object itself)
GENERATED VARCHAR2(1)   Indicates whether the name of this object was system generated (Y) or not (N)
SECONDARY VARCHAR2(1)   Whether this is a secondary object created by the ODCIIndexCreate method of the Oracle Data Cartridge (Y | N)

4.小结
数据库中包含数以万计的对象, DBA_OBJECTS / ALL_OBJECTS / USER_OBJECTS(OBJ)这些视图就像是一个小爬犁,通过这些视图可以很快的了解某个SCHEMA包含的内容。

时间: 2024-10-17 19:06:34

oracle锁等级以及解锁的相关文章

oracle 锁表 及 解锁

很久没有写博客了.不知道写什么 今天,让我清一张表的数据,结果清了之后,系统直接不动了,就好像服务器挂死了一样. 后来找了半天才发现,原来是我删除数据之后没有提交事物造成了数据库锁表,然后又不明白了, 什么是锁表呢,于是就上网去查 查了半天,大多数都是说怎么解决锁表和查询锁表的,但是只了解方法还不够,又想知道原理,于是 就继续往深里查查到了oracle的锁机制 由于数据库是多用户同时使用的共享资源,当多个用户并发的存取事物的时候,数据库中就会发生多个进程 同时操作同一个数据的情况,若对并发操作不

Oracle锁表、解锁

我遇到的情况: 当使用select语句查询表时,后面跟着for update , select * from table for update 当修改表中数据,但是没有commit就关掉PL/SQL,下次再打开执行上面sql语句,就会卡死,不能再for update 一.锁表 查看锁表进程SQL语句: select * from v$session t1, v$locked_object t2 where t1.sid = t2.SESSION_ID; 如果有记录,可查到如下锁表记录 二.解锁

for update造成的Oracle锁表与解锁

我遇到的情况: 当使用select语句查询表时,后面跟着for update , select * from table for update 当修改表中数据,但是没有commit就关掉PL/SQL,下次再打开,执行带for update的sql语句,就会卡死 一.锁表 查看锁表进程SQL语句: select * from v$session t1, v$locked_object t2 where t1.sid = t2.SESSION_ID; 如果有记录,可查到如下锁表记录 二.解锁 杀掉锁

Oracle锁表及解锁

为了防止用户在同一时间并发地访问和修改资源,ORACLE使用不同类型的锁控制对数据的并发访问,以防止用户之间出现破坏性的交互操作 1,锁表 锁定表的通用语法: lock table 表名 in <share or share update or exclusive mode> 共有三种锁表模式: 共享模式(in share mode) 共享更新模式(in share update mode) 排他锁模式(in exclusive mode) 锁表实例: //以排他锁模式锁定tRun表 lock

Oracle锁表与解锁

查看锁表进程SQL语句 select * from v$session t1, v$locked_object t2 where t1.sid = t2.SESSION_ID; 解锁 杀掉锁表进程: 记录下SID和serial# ,分别替换掉下面的1155,39095,即可解除锁表 alter system kill session '1155,39095'; 原文地址:https://www.cnblogs.com/yx007/p/9077939.html

oracle 锁表 如何解锁

---查询 被锁的表 SELECT 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; --alter system kill session 'sid, serial#'; ALTER system kill session '85, 289';

oracle 锁表、解锁的语句

 --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 '24,111'; (其中24,111分别是上面查询出的sid,

oracle锁表,解锁

//查询那些对象被锁 select object_name,machine,s.sid,s.serial#from v$locked_object l,dba_objects o ,v$session swhere l.object_id = o.object_id and l.session_id=s.sid; //杀死进程 alter system kill session 'sid,serial#'; 原文地址:https://www.cnblogs.com/lijianan/p/1017

Oracle的锁表与解锁

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