After completing this lesson, you should be able to do the following: • Define levels of locking • Identify causes of contention • Prevent locking problems • Use Oracle utilities to dete
Trang 1Copyright © Oracle Corporation, 2002 All rights reserved.
Monitoring and Detecting
Lock Contention
Trang 2After completing this lesson, you should be able to do the following:
• Define levels of locking
• Identify causes of contention
• Prevent locking problems
• Use Oracle utilities to detect lock contention
• Resolve contention in an emergency
• Resolve deadlock conditions
Trang 317-3 Copyright © Oracle Corporation, 2002 All rights reserved.
Locking Mechanism
• Automatic management
• High level of data concurrency
– Row-level locks for DML transactions – No locks required for queries
• Multi-version consistency
• Exclusive and Share lock modes
• Locks held until commit or rollback operations
are performed
Trang 4Data Concurrency
Transaction 1
SQL> SELECT salary
2 FROM employees
3 WHERE id = 10;
SALARY 1000
SQL> UPDATE employees
2 SET salary=salary*1.1
3 WHERE id= 24878;
1 row updated.
SQL> UPDATE employees
2 SET salary=salary+1200;
13120 rows updated.
SQL> UPDATE employees
2 SET salary=salary*1.1
3 WHERE id= 24877;
1 row updated.
Transaction 2
Trang 517-6 Copyright © Oracle Corporation, 2002 All rights reserved.
Two Types of Locks
• DML or data locks:
– Table-level locks – Row-level locks
• DDL or dictionary locks
(TM)
(TX)
Trang 6DML Locks
A DML transaction gets at least two locks:
• A shared table lock
• An exclusive row lock
Trang 717-9 Copyright © Oracle Corporation, 2002 All rights reserved.
Enqueue Mechanism
The enqueue mechanism keeps track of:
• Users waiting for locks
• The requested lock mode
• The order in which users requested the lock
Trang 8Table Lock Modes
These table lock modes are automatically assigned
by the Oracle server:
• Row Exclusive (RX): INSERT, UPDATE, DELETE
• Row Share (RS): SELECT FOR UPDATE
Trang 917-12 Copyright © Oracle Corporation, 2002 All rights reserved.
Manually Locking a Table
Manually acquired in LOCK TABLE statement:
• Share (S)
– No DML operations allowed – Implicitly used for referential integrity
SQL> LOCK TABLE hr.employees IN share MODE;
Trang 10Manually Locking a Table
• Share Row Exclusive (SRX)
– No DML operations or Share mode allowed – Implicitly used for referential integrity
– No index is required on the foreign key column in
the child table
• Exclusive (X)
– No DML or DDL operations allowed by other sessions
– No manual locks allowed by other sessions – Queries are allowed
Trang 1117-14 Copyright © Oracle Corporation, 2002 All rights reserved.
DML Locks in Blocks
Row 6 Block Header
Lock bytes
Row 1
1
2
TX slot 1 TX slot 2
Trang 12DDL Locks
• Exclusive DDL locks are required for:
– DROP TABLE statements
– ALTER TABLE statements
– (The lock is released when the DDL statement
completes.)
• Shared DDL locks are required for:
– CREATE PROCEDURE statements
– AUDIT statements
– (The lock is released when the DDL parse
Trang 1317-17 Copyright © Oracle Corporation, 2002 All rights reserved.
Possible Causes of Lock Contention
• Unnecessarily high locking levels
• Long-running transactions
• Uncommitted changes
• Other products imposing higher-level locks
Trang 14Diagnostic Tools for Monitoring
Locking Activity
Transaction 1
UPDATE employees
SET salary =
salary x 1.1;
Transaction 2 Transaction 3
v$lock
v$locked_object
UPDATE employees SET salary =
salary x 1.1 WHERE empno = 1000; UPDATE employees
SET salary = salary x 1.1
Trang 1517-20 Copyright © Oracle Corporation, 2002 All rights reserved.
Transaction 1
Guidelines for Resolving Contention
UPDATE employees SET salary = salary x 1.1 WHERE empno = 1000;
> COMMIT/ROLLBACK ;
UPDATE employees SET salary = salary x 1.1 WHERE empno = 1000;
1 row updated;
Transaction 2
> ALTER SYSTEM KILL SESSION ‘10,23’;
9:00
9:05 10:30 11:30
Trang 16Performance Manager: Locks
Trang 1717-23 Copyright © Oracle Corporation, 2002 All rights reserved.
Transaction
2
Transaction
1
Deadlocks
UPDATE employees
SET salary = salary x 1.1
WHERE empno = = 1000;
UPDATE employees
SET salary = salary x 1.1
WHERE empno = 2000;
ORA-00060:
Deadlock detected while
waiting for resource
UPDATE employees SET manager = 1342 WHERE empno = 2000; UPDATE employees SET manager = 1342 WHERE empno = 1000;
9:00
9:15
9:16
Trang 18ORA-00060:
Deadlock detected while waiting for resource
Trace file
Server process
SID_ora_PID.trc
UNIX
Trang 1917-26 Copyright © Oracle Corporation, 2002 All rights reserved.
Summary
In this lesson, you should have learned to do the following:
• Define levels of locking
• Identify causes of contention
• Prevent locking problems
• Use Oracle utilities to detect lock contention
• Resolve contention in an emergency
• Resolve deadlock conditions