ORACLE lock mechanism (reproduced)

ORACLE LOCK MECHANISM

A database is a shared resource used by multiple users. When multiple users access data concurrently, there will be multiple transactions accessing the same data at the same time in the database. Without control over concurrent operations, incorrect data may be read and stored, breaking database consistency.

Locking is a very important technology for implementing database concurrency control. Before a transaction operates on a data object, it first sends a request to the system to lock it. After locking, the transaction has certain control over the data object. Before the transaction releases the lock, other transactions cannot update the data object.

There are two basic types of locks in the database: exclusive locks (Exclusive Locks, namely X locks) and shared locks (Share Locks, namely S locks). When an exclusive lock is added to a data object, other transactions cannot read or modify it. Data objects with shared locks can be read by other transactions, but cannot be modified. The database uses these two basic lock types to control the concurrency of database transactions.

Lock Types of Oracle Database

According to different protected objects, Oracle database locks can be divided into the following categories: DML locks (data locks, data locks), used to protect the integrity of data; DDL locks (dictionary locks, dictionary locks), used to protect database objects The structure of the database, such as the structure definition of tables, indexes, etc.; internal locks and latches, which protect the internal structure of the database.

The purpose of DML locks is to ensure data integrity in concurrent situations. In the Oracle database, DML locks mainly include TM locks and TX locks, where TM locks are called table-level locks, and TX locks are called transaction locks or row-level locks.

When Oracle executes a DML statement, the system automatically applies for a TM-type lock on the table to be operated. After the TM lock is obtained, the system automatically applies for a TX type lock, and sets the lock flag of the actually locked data row. In this way, it is not necessary to check the lock flag row by row when checking the compatibility of the TX lock before the transaction is locked, but only the compatibility of the TM lock mode can be checked, which greatly improves the efficiency of the system. TM locks include SS, SX, S, X and other modes, which are represented by 0-6 in the database. Different SQL operations generate different types of TM locks.

There are only X locks (exclusive locks) on data rows. In the Oracle database, a TX lock is acquired when a transaction initiates a DML statement for the first time, and the lock is held until the transaction is committed or rolled back. When two or more sessions execute DML statements on the same record in the table, the first session locks the record, and the other sessions are in a waiting state. When the first session is committed, the TX lock is released, and other sessions can be locked.

When the Oracle database waits for a TX lock, if it is not handled in time, it will often cause the Oracle database to hang, or cause a deadlock to occur, resulting in an ORA-60 error. These phenomena will cause great harm to practical applications, such as unresponsiveness for a long time, and a large number of transaction failures.

Pessimistic blocking and optimistic blocking 
1. Pessimistic blocking 
locks come into play before the user modifies: 
Select ..for update (nowait) 
Select * from tab1 for update 
After the user issues this command, Oracle will create row-level data in the returned set Blocked to prevent modification by other users. 
If other users perform dml or ddl operations on the data in the result set returned above, an error message will be returned or blockage will occur. 
1: The update or delete operation on the returned result set will block. 
2: DDL operation on this table will report: Ora-00054: resource busy and acquire with nowait specified.

Cause Analysis 
At this time, Oracle has added an exclusive row-level lock to the returned result set. All other modification or deletion operations on these data must wait for the release of this lock. The external phenomenon is that other operations will Blocking occurs, this operation commit or rollback. 
The same query transaction will add a table-level lock to the table, and any ddl operations on the table will not be allowed, otherwise an ora-00054 error will be reported: resource busy and acquire with nowait specified.

2. Optimistic Blockade 
Optimistically believe that the data will not be changed during the period from when the data is selected to when the update is aggressive and submitted. There is a potential danger here that because the selected result set is not locked, there is a possibility that it may be changed by other users. Therefore, Oracle still recommends using pessimistic blocking, because it will be more secure.


block

Definition: 
Blocking occurs when one session holds a lock on a resource another session is requesting. A blocked session will remain suspended until the session holding the lock relinquishes the locked resource. 4 common dml statements that generate blocking 
INSERT 
UPDATE 
DELETE 
SELECT...FOR UPDATE


INSERT

The only case where Insert will block is when the user has a table with a primary key constraint. When 2 sessions try to insert the same data into the table at the same time, one session will be blocked until the other session commits or will roll. When one session commits, the other session will receive a duplicate primary key error. When rolling back, the blocked session will continue to execute.

UPDATE and DELETE When the data row for the Update and delete operation has been locked by another session, it will block until another session commits or rolls.

Select …for update

当一个用户发出select..for update的错作准备对返回的结果集进行修改时,如果结果集已经被另一个会话锁定,就是发生阻塞。需要等另一个会话结束之后才可继续执行。可以通过发出 select… for update nowait的语句来避免发生阻塞,如果资源已经被另一个会话锁定,则会返回以下错误:Ora-00054:resource busy and acquire with nowait specified.

死锁-deadlock

定义:当两个用户希望持有对方的资源时就会发生死锁. 
即两个用户互相等待对方释放资源时,oracle认定为产生了死锁,在这种情况下,将以牺牲一个用户作为代价,另一个用户继续执行,牺牲的用户的事务将回滚. 
例子: 
1:用户1对A表进行Update,没有提交。 
2:用户2对B表进行Update,没有提交。 
此时双反不存在资源共享的问题。 
3:如果用户2此时对A表作update,则会发生阻塞,需要等到用户一的事物结束。 
4:如果此时用户1又对B表作update,则产生死锁。此时Oracle会选择其中一个用户进行会滚,使另一个用户继续执行操作。 
起因: 
Oracle的死锁问题实际上很少见,如果发生,基本上都是不正确的程序设计造成的,经过调整后,基本上都会避免死锁的发生。

DML锁分类表


表1Oracle的TM锁类型 
锁模式 锁描述 解释 SQL操作 
0 none 
1 NULL 空 Select 
2 SS(Row-S) 行级共享锁,其他对象 
只能查询这些数据行 Select for update、Lock for

update、Lock row share

3 SX(Row-X) 行级排它锁, 
在提交前不允许做DML操作 Insert、Update、 
Delete、Lock row share

4 S(Share) 共享锁 Create index、Lock share 
5 SSX(S/Row-X) 共享行级排它锁 Lock share row exclusive 
6 X(Exclusive) 排它锁 Alter table、Drop able、Drop index、Truncate table 、Lock exclusive

oracle 锁问题的解决


可以用Spotlight软件对数据库的运行状态进行监控。

当出现session锁时,我们要及时进行处理.

1. 查看哪些session锁: 
SQL语句:select 'alter system kill session '''||sid||','||serial#||''';' from v$session where sid in (select sid from v$lock where block = 1);

SQL> select 'alter system kill session '''||sid||','||serial#||''';' from v$session where sid in (select sid from v$lock where block = 1); 
'ALTERSYSTEMKILLSESSION'''||SID||','||SERIAL#||''';' 
-------------------------------------------------------------------------------- 
alter system kill session '132,731'; 
alter system kill session '275,15205'; 
alter system kill session '308,206'; 
alter system kill session '407,3510';

2. 查看session锁. 
sql语句:select s.sid, q.sql_text from v$sqltext q, v$session s 
where q.address = s.sql_address 
and s.sid = &sid 
order by piece;

SQL> select s.sid,q.sql_text from v$sqltext q, v$session s where q.address = s.sql_address and s.sid in (select sid from v$lock where block = 1) order by piece; 
        SID SQL_TEXT 
---------- ---------------------------------------------------------------- 
       77 UPDATE PROFILE_USER SET ID=1,COMPANY_ID=2,CUSTOMER_ID=3,NAMED   
       77 _INSURED_ID=4,LOGIN=5,ROLE_ID=6,PASSWORD=7,EMAIL=8,TIME_ZON 
       77 E=9 WHERE PROFILE_USER.ID=:34 
3 rows selected.


3. kill锁的进程. 
SQL语句:alter system kill session '77,22198';

SQL> alter system kill session '391,48398'; 
System altered.

4. 查看谁锁了谁。
select s1.username || [email='@']'@'[/email] || s1.machine
  || ' ( SID=' || s1.sid || ' )  is blocking '
  || s2.username || [email='@']'@'[/email] || s2.machine || ' ( SID=' || s2.sid || ' ) ' AS blocking_status
  from v$lock l1, v$session s1, v$lock l2, v$session s2
  where s1.sid=l1.sid and s2.sid=l2.sid
  and l1.BLOCK=1 and l2.request > 0
  and l1.id1 = l2.id1
  and l2.id2 = l2.id2 ;

注: 
> : 重定向输出,将文件的标准输出重新定向输出到文件,或将数据文件作为另一程序的标准输入内容。 
| :UNIX管道:将一文件的输出作为另一文件的输入.

在执行SQL语句试:alter system kill session '391,48398'(sid为391); 应当注意对于sid在100以下的应当谨慎,可能该进程对应某个application,如对应某个事务,可以kill.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326574304&siteId=291194637