You are on page 1of 3

How To Experiment a Data Lock?

If you want to have some experience with data locks, you can create two windows
runing two SQL*Plus sessions. In session 1, you can run a UPDATE statements to c
reate a data lock. Before committing session 2, switch to session 2, and run a U
PDATE statements on the same row as session 1. The UPDATE statement will be put
into wait status because of the data lock. Follow the tutorial exercise below to
experience yourself:
(session 1)
SQL> connect HR/fyicenter
SQL> SET TRANSACTION
ISOLATION LEVEL
READ COMMITTED;
Transaction set.
SQL> SELECT * FROM fyi_links;
ID URL NOTES
--- ---------------- --------
101 FYICENTER.COM
110 CENTERFYI.COM
SQL> UPDATE fyi_links
SET url='fyicenter.com'
WHERE id=101;
1 row updated.
(lock created on row id=101)
(session 2)
SQL> connect HR/fyicenter

SQL> SET TRANSACTION
ISOLATION LEVEL
READ COMMITTED;
Transaction set.
SQL> UPDATE fyi_links SET
notes='FAQ Resource'
WHERE id=101;
(wait on lock at id=101)
SQL> COMMIT;
(lock on row id=101 released)
(ready to run UPDATE)
1 row updated.
SQL> SELECT * FROM fyi_links;
ID URL NOTES
--- ---------------- --------
101 fyicenter.com
110 CENTERFYI.COM
SQL> COMMIT;
SQL> SELECT * FROM fyi_links;
ID URL NOTES
--- ---------------- ------------
101 fyicenter.com FAQ Resource
110 CENTERFYI.COM
How To View Existing Locks on the Database?
As can see from the pervious tutorial exercise, performance of the second sessio
n is greatly affected by the data lock created on the database. To maintain a go
od performance level for all sessions, you need to monitor the number of data lo
cks on the database, and how long do they last.
Oracle maintains current existing data locks in a Dynamic Performance View calle
d V$LOCK with columns like:
SID - Session ID to identify the session that owns this lock.
TYPE - The type of the lock, like TM (DML enqueue) and TX (Transaction enqueue).

LMODE - The lock mode in which the session holds the lock.
REQUEST - The lock mode in which the session requests the lock.
CTIME - The time since current lock mode was granted
BLOCK - A value of either 0 or 1, depending on whether or not the lock in questi
on is the blocker.
The following tutorial exercise shows you how to view existing locks on the data
base:
(session 1)
SQL> connect HR/fyicenter
SQL> UPDATE fyi_links
SET url='centerfyi.com'
WHERE id=110;
1 row updated.
(session 2)
SQL> connect HR/fyicenter
SQL> INSERT INTO fyi_links
(url, id) VALUES
('oracle.com', 112);
1 row created.
SQL> UPDATE fyi_links
SET notes='FYI Resource'
WHERE id=110;
(wait on lock at id=110)
Now keep those two sessions as is. You need to open a third window to connect to
the database as SYSTEM to view all current locks:
(session 3)
SQL> connect SYSTEM/password
SQL> select sid, username from v$session
2 where username='HR';
SID USERNAME
---------- ------------------------------
23 HR
39 HR
SQL> SELECT sid, type, lmode, request, ctime, block
FROM V$LOCK WHERE sid in (23, 39) ORDER BY ctime DESC;
SID TY LMODE REQUEST CTIME BLOCK
---- -- ---------- ---------- ---------- ----------
1 39 TX 6 0 84 1
2 39 TM 3 0 84 0
3 23 TM 3 0 27 0
4 23 TX 6 0 27 0
5 23 TX 0 6 18 0
You should read the output as:
Line #1 and #2 represent the lock resulted from the UPDATE statement in session
#1 on row id=110.
Line #3 and #4 represent the lock resulted from the INSERT statement in session
#2 on row id=112.
Line #5 represents a request of lock resulted from the UPDATE statement in sessi
on #2 on row id=110, which is blocked by the lock from line #1 and #2.
What Is a Dead Lock?
A dead lock is phenomenon happens between two transactions with each of them hol
ding a lock that blocks the other transaction as shown in the following diagram:
(transaction 1) (transaction 2)
update row X to create lock 1
update row Y to create lock 2
update row X
(blocked by lock 1)
update row Y
(blocked by lock 2)
(dead lock created)
How Oracle Handles Dead Locks?
Oracle server automatically detects dead locks. When a dead lock is detected, Or
acle server will select a victim transaction, and fail its statement that is blo
cked in the dead lock to break the dead lock. The tutorial exercise below shows
you an example of statements failed by Oracle server because of dead locks:
(session 1)
SQL> connect HR/fyicenter
SQL> UPDATE fyi_links
SET notes='Session 1'
WHERE id=101;
1 row updated.
(session 2)
SQL> connect HR/fyicenter
SQL> UPDATE fyi_links
SET notes='Session 2'
WHERE id=110;
1 row updated.

SQL> UPDATE fyi_links
SET notes='Session 2'
WHERE id=101;
(blocked by lock id=101)
SQL> UPDATE fyi_links
SET notes='Session 1'
WHERE id=110;
(blocked by lock on row id=110)
ORA-00060: deadlock
detected while waiting
for resource
(statement failed)

You might also like