0% found this document useful (0 votes)
41 views20 pages

Concurrency and Locks

Uploaded by

Aya Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views20 pages

Concurrency and Locks

Uploaded by

Aya Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Data Concurrency and Locks

Ahmed Naguib
ORACLE administration by Ahmed Naguib
Objectives

After completing this lesson, you should be able to:


• Describe the locking mechanism and how Oracle manages
data concurrency
• Monitor and resolve locking conflicts

Ahmed Naguib
2 ORACLE administration by Ahmed Naguib
Concurrency and consistency

• Data concurrency means that many users can access data


at the same time.
• Data consistency means that each user sees a consistent
view of the data, including visible changes made by the
user's own transactions and transactions of other users.

User

Ahmed Naguib
3 ORACLE administration by Ahmed Naguib
Locks

• Prevent multiple sessions from changing the same data at


the same time
• Are automatically obtained at the lowest possible level for
a given statement
• Do not escalate

Transaction 1 Transaction 2
SQL> UPDATE employees SQL> UPDATE employees
2 SET salary=salary+100 2 SET salary=salary*1.1
3 WHERE employee_id=100; 3 WHERE employee_id=100;

Ahmed Naguib
4 ORACLE administration by Ahmed Naguib
Locking Mechanism

• High level of data concurrency:


– Row-level locks for inserts, updates, and deletes
– No locks required for queries
• Automatic queue management
• Locks held until the transaction ends (with a commit or
rollback operation)
Example
Assume that the rows for EMPLOYEE_ID 100 and 101 reside in the same
block:

Transaction 1 Transaction 2
SQL> UPDATE employees SQL> UPDATE employees
2 SET salary=salary+100 2 SET salary=salary*1.1
3 WHERE employee_id=100; 3 WHERE employee_id=101;

Ahmed Naguib
5 ORACLE administration by Ahmed Naguib
Ahmed Naguib
6 ORACLE administration by Ahmed Naguib
Data Concurrency

Time: Transaction 1 UPDATE hr.employees


SET salary=salary+100
WHERE employee_id=100;
Transaction 2 UPDATE hr.employees
SET salary=salary+100
WHERE employee_id=101;
09:00:00 Transaction 3 UPDATE hr.employees
SET salary=salary+100
WHERE employee_id=102;
... ...
Transaction x UPDATE hr.employees
SET salary=salary+100
WHERE employee_id=xxx;

Ahmed Naguib
7 ORACLE administration by Ahmed Naguib
DML Locks

Transaction 1 Transaction 2
SQL> UPDATE employees SQL> UPDATE employees
2 SET salary=salary*1.1 2 SET salary=salary*1.1
3 WHERE employee_id= 107; 3 WHERE employee_id= 106;
1 row updated. 1 row updated.

Each DML transaction must acquire two locks:


• EXCLUSIVE row lock on the row or rows being updated
• Table lock (TM) in ROW EXCLUSIVE (RX) mode on the
table containing the rows

Ahmed Naguib
9 ORACLE administration by Ahmed Naguib
Enqueue Mechanism

The enqueue mechanism keeps track of:


• Sessions waiting for locks
• Requested lock mode
• Order in which sessions requested the lock

Ahmed Naguib
10 ORACLE administration by Ahmed Naguib
Lock Conflicts

Transaction 1 Time Transaction 2


UPDATE employees SET 9:00:00 UPDATE employees SET
salary=salary+100 WHERE salary=salary+100 WHERE
employee_id=100; employee_id=101;
1 row updated. 1 row updated.
UPDATE employees SET 9:00:05 SELECT sum(salary) FROM
COMMISION_PCT=2 WHERE employees;
employee_id=101; SUM(SALARY)
Session waits enqueued due to -----------
lock conflict. 692634
Session still waiting! Many selects, inserts, updates,
16:30:00 and deletes during the last 7.5
hours, but no commits or
rollbacks!
1 row updated. 16:30:01 commit;
Session continues.

Ahmed Naguib
11 ORACLE administration by Ahmed Naguib
Possible Causes of Lock Conflicts

• Uncommitted changes
• Long-running transactions
• Unnecessarily high locking levels

Ahmed Naguib
12 ORACLE administration by Ahmed Naguib
Detecting Lock Conflicts

• Select Blocking Sessions from the Performance menu.

• Click the Session ID link to view information about the


locking session.
Ahmed Naguib
13 ORACLE administration by Ahmed Naguib
Resolving Lock Conflicts

To resolve a lock conflict:


• Have the session holding the lock commit or roll back
• Terminate the session holding the lock (in an emergency)

Ahmed Naguib
14 ORACLE administration by Ahmed Naguib
Resolving Lock Conflicts by Using SQL

SQL statements can be used to determine the blocking session


and kill it.

SQL> SELECT sid, serial#, username


1 2 FROM v$session WHERE sid IN
3 (SELECT blocking_session FROM v$session);

Result:

2 SQL> ALTER SYSTEM KILL SESSION '144,8982' immediate;

Ahmed Naguib
15 ORACLE administration by Ahmed Naguib
Deadlocks

Transaction 1 Transaction 2
UPDATE employees UPDATE employees
SET salary = salary x 1.1 9:00 SET manager = 1342
WHERE employee_id = 1000; WHERE employee_id = 2000;
UPDATE employees UPDATE employees
SET salary = salary x 1.1 9:15 SET manager = 1342
WHERE employee_id = 2000; WHERE employee_id = 1000;
ORA-00060:
Deadlock detected while 9:16
waiting for resource

Ahmed Naguib
16 ORACLE administration by Ahmed Naguib
Ahmed Naguib
17 ORACLE administration by Ahmed Naguib
Quiz

The lock mechanism defaults to a fine-grained,


row-level locking mode.
a. True
b. False

Ahmed Naguib
18 ORACLE administration by Ahmed Naguib
Quiz

When a deadlock occurs, Oracle database


automatically:
a. Waits 300 seconds before terminating both sessions
b. Terminates one statement with an error in one session
c. Terminates the statements with an error in both sessions
d. Takes no action by default and leaves it to the DBA

Ahmed Naguib
19 ORACLE administration by Ahmed Naguib
Summary

In this lesson, you should have learned how to:


• Describe the locking mechanism and how Oracle manages
data concurrency
• Monitor and resolve locking conflicts

Ahmed Naguib
20 ORACLE administration by Ahmed Naguib
Practice: Overview

This practice covers the following topics:


• Identifying locking conflicts
• Resolving locking conflicts

Ahmed Naguib
21 ORACLE administration by Ahmed Naguib

You might also like