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鎖有:行級(事務級)鎖和表級鎖
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.
如果一個事務獲得了一個行級鎖,那么它會同時獲得包含該行的表級鎖,避免DDL操作導致當前事務中的數據修改而引起沖突。
下圖是表級鎖TM和行級鎖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 .
一些數據庫管理系統,在內存中通過鎖管理來維護一系列的鎖,而Oracle數據庫通過包含了鎖住行的數據塊來存儲鎖的相關信息。
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" ).
使用隊列的機制來獲取行級鎖。
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:
表級鎖包含了以下幾種模式:
-
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.
行共享:表級鎖中最低限度的鎖表模式,能夠提供最高的并發度。此時不希望其他用戶或繪畫對改行進行更新操作。
-
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.行級排他鎖,表示獲得該鎖的事務將對表進行DML操作,SX鎖允許其他事務對相同的表進行查詢、插入、更新、修改或者鎖其他行。因此SX鎖允許多個事務獲得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.共享鎖,不允許任何用戶更新表,但是允許其他用戶發出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.同時只有一個事務可以獲得該共享行級排他鎖,此時只允許其他事務查詢表,但不能進行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.
排他鎖,其他事務不能對該表進行任何DML和DDL操作,也不能添加任何類型的鎖。
各個模式的鎖之間的兼容性如下:
See Also:
-
Oracle Database Advanced Application Developer's Guide ?to learn more about table locks
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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