Simple demonstration of segment-level locking caused by concurrency in an Oracle database
reprint:简单演示 Oracle 数据库并发导致段级锁(表级锁)
https://www.cnblogs.com/liuning8023/archive/2013/03/20/2971946.html
content
- environment
- demonstration
本文简单演示并发导致的行级锁。并发是两个以上的用户对同样的数据进行修改(包括插入、删除和修改)。锁的产生是因为并发。没有并发,就没有锁。并发的产生是因为系统需要,系统需要是因为用户需要。
This article briefly demonstrates row-level locking caused by concurrency. Concurrency is when two or more users make changes (including inserts, deletes, and modifications) to the same data. Locks occur because of concurrency. No concurrency, no locking. Concurrency occurs because the system needs it, and the system needs it because the user needs it.
environment
- Windows 2003 Server
- Oracle 11g Release 1 (11.1)
demonstration
open the first sql session
1 | SQL> select distinct sid from v$mystat; |
- create partition table
t
, with two partitions,p1
andp2
- insert a record into
p1
- find
TM
share lock int
andp1
, by viewV$LOCK
ID1
represents the locked object ID. You can get the object name by looking at the DBA_OBJECTS view. 128807 and 128808 correspond to thep1
partition of table t and table t, respectively.
open the second sql session
At this point, what happens if we do DDL operations on t table, p1 partition of t table, and p2 partition of t table:
1 | SQL> select distinct sid from v$mystat; |
Only the
p2
partition is not section-locked, so DDL operations can be performed onp2
.A
TM
lock is a segmental-level lock that allows locks at the same level or lower, but rejects higher-level locks, making the DDL operation significantly higher. Oracle minimizes the scope of the lock as much as possible.
1 | SQL> insert into t values(11); |
When a piece of data is inserted into a
p2
partition, a Shared lock is also added to thep2
partition, i.e. ID1=128809.A
TM lock
is a table-level Shared lock. A table is usually viewed as a segment. When a table has several segments, each segment is individually locked.
summary
- segmental-level lock allows locks at the same level or lower, but rejects higher-level locks. so
p1
reject DDL, butp2
truncated.