Note:
The database never locks rows when performing simple reads.Lock | Description |
---|---|
DML Locks | Protect data. For example, table locks lock entire tables, while row locks lock selected rows. See? "DML Locks" . |
DDL Locks | Protect the structure of schema objects—for example, the dictionary definitions of tables and views. See? "DDL Locks" . |
System Locks | Protect internal database structures such as data files. Latches, mutexes, and internal locks are entirely automatic. See? "System Locks" . |
DML Locks
A DML lock, also called a? data lock , guarantees the integrity of data accessed concurrently by multiple users. For example, a DML lock prevents two customers from buying the last copy of a book available from an online bookseller. DML locks prevent destructive interference of simultaneous conflicting DML or DDL operations.
DML statements automatically acquire the following types of locks:
DML鎖有:行級(jí)(事務(wù)級(jí))鎖和表級(jí)鎖
In the following sections, the acronym in parentheses after each type of lock or lock mode is the abbreviation used in the Locks Monitor of Oracle Enterprise Manager (Enterprise Manager). Enterprise Manager might display TM for any table lock, rather than indicate the mode of table lock (such as RS or SRX).
If a transaction obtains a lock for a row, then the transaction also acquires a lock for the table containing the row. The table lock prevents conflicting DDL operations that would override data changes in a current transaction.? Figure 9-2 ?illustrates an update of the third row in a table. Oracle Database automatically places an exclusive lock on the updated row and a subexclusive lock on the table.
如果一個(gè)事務(wù)獲得了一個(gè)行級(jí)鎖,那么它會(huì)同時(shí)獲得包含該行的表級(jí)鎖,避免DDL操作導(dǎo)致當(dāng)前事務(wù)中的數(shù)據(jù)修改而引起沖突。
下圖是表級(jí)鎖TM和行級(jí)鎖TX的示意圖
?
Storage of Row Locks
Unlike some databases, which use a lock manager to maintain a list of locks in memory, Oracle Database stores lock information in the? data block ?that contains the locked row .
一些數(shù)據(jù)庫(kù)管理系統(tǒng),在內(nèi)存中通過(guò)鎖管理來(lái)維護(hù)一系列的鎖,而Oracle數(shù)據(jù)庫(kù)通過(guò)包含了鎖住行的數(shù)據(jù)塊來(lái)存儲(chǔ)鎖的相關(guān)信息。
The database uses a queuing mechanism for acquisition of row locks. If a transaction requires a lock for an unlocked row, then the transaction places a lock in the data block. Each row modified by this transaction points to a copy of the transaction ID stored in the? block header ?(see? "Overview of Data Blocks" ).
使用隊(duì)列的機(jī)制來(lái)獲取行級(jí)鎖。
When a transaction ends, the transaction ID remains in the block header. If a different transaction wants to modify a row, then it uses the transaction ID to determine whether the lock is active. If the lock is active, then the session asks to be notified when the lock is released. Otherwise, the transaction acquires the lock.
Table Locks (TM)
A?
table lock
, also called a?
TM lock
, is acquired by a transaction when a table is modified by an?
INSERT
,?
UPDATE
,?
DELETE
,?
MERGE
,?
SELECT
?with the?
FOR
UPDATE
?clause, or?
LOCK
?
TABLE
?statement. DML operations require table locks to reserve DML access to the table on behalf of a transaction and to prevent DDL operations that would conflict with the transaction.
A table lock can be held in any of the following modes:
表級(jí)鎖包含了以下幾種模式:
-
This lock, also called a? subshare table lock (SS) , indicates that the transaction holding the lock on the table has locked rows in the table and intends to update them. A row share lock is the least restrictive mode of table lock, offering the highest degree of concurrency for a table.
行共享:表級(jí)鎖中最低限度的鎖表模式,能夠提供最高的并發(fā)度。此時(shí)不希望其他用戶(hù)或繪畫(huà)對(duì)改行進(jìn)行更新操作。
-
This lock, also called a? subexclusive table lock (SX) , generally indicates that the transaction holding the lock has updated table rows or issued
SELECT ... FOR UPDATE
. An SX lock allows other transactions to query, insert, update, delete, or lock rows concurrently in the same table. Therefore, SX locks allow multiple transactions to obtain simultaneous SX and subshare table locks for the same table.行級(jí)排他鎖,表示獲得該鎖的事務(wù)將對(duì)表進(jìn)行DML操作,SX鎖允許其他事務(wù)對(duì)相同的表進(jìn)行查詢(xún)、插入、更新、修改或者鎖其他行。因此SX鎖允許多個(gè)事務(wù)獲得SX鎖和S鎖,但是不能獲得X鎖
-
A share table lock held by a transaction allows other transactions to query the table (without using?
SELECT ... FOR UPDATE
), but updates are allowed only if a single transaction holds the share table lock. Because multiple transactions may hold a share table lock concurrently, holding this lock is not sufficient to ensure that a transaction can modify the table.共享鎖,不允許任何用戶(hù)更新表,但是允許其他用戶(hù)發(fā)出select ... for update添加RS鎖
-
Share Row Exclusive Table Lock (SRX)
This lock, also called a? share-subexclusive table lock (SSX) , is more restrictive than a share table lock. Only one transaction at a time can acquire an SSX lock on a given table. An SSX lock held by a transaction allows other transactions to query the table (except for?
SELECT ...
?FOR UPDATE
) but not to update the table.同時(shí)只有一個(gè)事務(wù)可以獲得該共享行級(jí)排他鎖,此時(shí)只允許其他事務(wù)查詢(xún)表,但不能進(jìn)行DML操作,也不能使用select ... for update。
-
This lock is the most restrictive, prohibiting other transactions from performing any type of DML statement or placing any type of lock on the table.
排他鎖,其他事務(wù)不能對(duì)該表進(jìn)行任何DML和DDL操作,也不能添加任何類(lèi)型的鎖。
各個(gè)模式的鎖之間的兼容性如下:
See Also:
-
Oracle Database Advanced Application Developer's Guide ?to learn more about table locks
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
