Lock dalam Oracle merupakan sebuah mekanisme yang mencegah interaksi destruktif diantara dua transaksi yang mengakses sumber daya yang sama.
Locking memungkinkan hanya satu pengguna yang memperbarui blok data tertentu, orang lain tidak dapat memodifikasi data yang sama.
Ide dasar dari Locking adalah bahwa ketika pengguna atau seorang user memodifikasi data melalui suatu transaksi, data tersebut dikunci (locked) sampai transaksi tersebut di commit atau di rollback. Lock diproses sampai transaksi selesai hal ini dikenal sebagai data concurrency.
1. Universitas Multimedia Nusantara, Major of Information System
Muhaimin A. R. Soltief | Bhisma Mahendra | Andy Surya Darmawan |
Hendry Setiawan | Yusuf Ramdhan
Locking
2. Agenda
• Apa itu Locks?
• Locking?
• Basic Of Locking
• Lock
• Lock Usage
• Lock Modes
• Lock Conflict
• Lock Levels
• SQL Statement - Exclusive Locks
• SQL Statement - Share Locks
• Penyebab terjadinya Lock Conflict
• Share Table Locks (S)
• Share Table – Operasi yang Diijinkan
• Share Table – Operasi yang Dilarang
• Contoh Share Table Locks (S)
• Exclusive Table Locks (X)
• Exclusive Table Locks (X) – Operasi yang Diijinkan
• Exclusive Table Locks (X) – Operasi yang Dilarang
• Contoh Exclusive Table Locks (X)
Locking
3. Apa itu Lock?
• Lock dalam Oracle merupakan sebuah mekanisme yang mencegah interaksi
destruktif diantara dua transaksi yang mengakses sumber daya yang sama.
• General Object Type Affected By Locks:
- User Objects, seperti tabel dan baris (struktur dan data).
- System Objects not visible to users, seperti shared data structures pada
memory dan data dictionary rows.
4. Locking ?
• Dalam sistem multi-user, banyak pengguna dapat memperbarui informasi
yang sama pada waktu yang sama.
• Locking memungkinkan hanya satu pengguna yang memperbarui blok data
tertentu, orang lain tidak dapat memodifikasi data yang sama.
5. Basic of Locking
• Ide dasar dari Locking adalah bahwa ketika pengguna atau seorang user
memodifikasi data melalui suatu transaksi, data tersebut dikunci (locked)
sampai transaksi tersebut di commit atau di rollback. Lock diproses sampai
transaksi selesai hal ini dikenal sebagai data concurrency.
Transaction 1
SQL> UPDATE employees
2 SET salary=salary*1.1
3 WHERE employee_id=101;
SQL> UPDATE employees
2 SET salary=salary+100
3 WHERE employee_id=100;
Transaction 2
Example!
Assume that the rows for employee_id 100 and 101 reside in the same block:
6. Basic of Locking
Time:
09:00:00
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;
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;
7. Lock
• Allow sessions to join a queue for a resource that is not immediately
available. Memungkinkan sessions untuk join queue jika resource tidak tersedia.
• To achieve consistency and integrity. Untuk mencapai konsistensi dan integritas.
• Performed automatically by Oracle and manually by users. Dilakukan secara
otomatis oleh Oracle dan secara manual oleh pengguna.
8. Lock Usage
• Transaction & Row-level locks :
1. Transactions imposing implicit locks on rows.
2. In effect for the duration of the transaction.
• Buffer locks :
Short term block-level locks in force while modifying blocks in cache.
• Data dictionary locks :
Locks that protect data dictionary objects.
9. Lock Modes
• Oracle menggunakan dua mode penguncian (locking) dalam
multi-user database : Exclusive Lock Mode & Share Lock Mode .
1. Exclusive Lock Mode (X)
• Prevents the associates resource from being shared.
• Mode lock ini diperoleh untuk memodifikasi data.
• The first transaction to lock a resource exclusively is the only transaction that
can alter the resource until the exclusive lock is released.
10. Exclusive Locks
SQL Statement Mode of Lock
SELECT ... FROM table... No Lock
INSERT INTO table ... RX
UPDATE table ... RX
DELETE FROM table ... RX
LOCK TABLE table IN ROW EXCLUSIVE MODE RX
LOCK TABLE table IN SHARE ROW EXCLUSIVE MODE SRX
LOCK TABLE table IN EXCLUSIVE MODE X
11. Lock Modes
2. Share Lock Mode (S)
• Memungkinkan associated resource untuk dibagikan, tergantung pada
operasi yang terlibat.
• Multiple users reading data can share the data, holding share locks to
prevent concurrent access by a writer (who needs an exclusive lock).
• Beberapa transaksi dapat memperoleh share locks pada resource yang sama.
12. Share Locks
SQL Statement Mode of Lock
SELECT ... FROM table FOR UPDATE OF ... RS
LOCK TABLE table IN ROW SHARE MODE RS
LOCK TABLE table IN SHARE MODE S
LOCK TABLE table IN SHARE ROW EXCLUSIVE MODE SRX
Keterangan :
RS : Row Share
RX : Row Exclusive
S : Share
SRX : Share Row Exclusive
X : Exclusive
13. Lock Conflict
UPDATE employees SET
salary=salary+100 WHERE
employee_id=100;
1 row updated.
9:00:00 UPDATE employees SET salary=salary+100
WHERE employee_id=101;
1 row updated.
UPDATE employees SET
COMMISION_PCT=2 WHERE
employee_id=101;
Session waits enqueued due to lock conflict.
9:00:05 SELECT sum(salary) FROM employees;
SUM(SALARY)
-----------
692634
Session still waiting!
16:30:00
Many selects, inserts, updates, and deletes during
the last 7.5 hours, but no commits or rollbacks!
1 row updated.
Session continues.
16:30:01 commit;
Transaction 1 Transaction 2Time
14. Peyebab terjadinya Lock Conflict
1. Transaksi berjalan lama ; Banyak aplikasi menggunakan proses batch
untuk melakukan update secara keseluruhan.
Ex : Kasus lock conflict yang umum; ketika transaksi dan batch processing
sedang dilakukan secara bersamaan.
2. Unnecessarily high locking levels ; Tidak semua database mendukung row-
level locking. Beberapa database masih melakukan proses locking pada page
atau table.
15. Lock Levels
• Oracle menyediakan dua level lock yang berbeda :
Tabel Level LockRow Level Lock
16. Lock Levels
1. Row Level Lock.
Dengan strategi Row Level Lock, setiap baris dalam sebuah tabel dapat dikunci
secara individual.
Baris yang ter-locked hanya dapat di update dari locking process.
Semua baris lain dalam tabel masih dapat diperbarui oleh proses lainnya. Tentu
saja, proses lain tetap dapat membaca setiap baris di dalam tabel, termasuk salah
satu yang sedang diperbarui.
Bila proses lain melakukan read terhadap baris yang diperbarui, proses tersebut
hanya dapat melihat versi lama dari baris sebelum di update (melalui segmen
rollback) sampai perubahan tesebut benar-benar di commit. Hal ini dikenal sebagai
read consistency.
17. Lock Levels
2. Table Level Lock.
o Dengan Table Level Lock, seluruh tabel terlock sebagai satu entitas.
o Setelah proses lock terhadap tabel, hanya proses tersebut yang dapat
memperbarui setiap baris dalam tabel.
o Tak satu pun dari baris dalam tabel yang dapat memperbarui proses lainnya.
Tentu saja, proses lain tetap dapat membaca setiap baris di dalam tabel,
termasuk salah satu yang sedang diperbarui.
18. Share Table Locks (S)
• A share table lock is acquired automatically for the table specified in the
following statement:
LOCK TABLE table IN SHARE MODE;
19. Share Table – Operasi yang Diijinkan
• Sebuah share table lock yang dimiliki oleh transaksi, memungkinkan transaksi
lain hanya untuk :
untuk SELECT (query dari table)
untuk lock specific rows with SELECT ... FOR UPDATE
atau untuk execute LOCK TABLE ... IN SHARE MODE
• Tidak ada update yang diizinkan oleh transaksi lain.
• Transaksi yang memiliki share table lock dapat memperbarui tabel, hanya
jika ada transaksi lain yang juga memiliki share table lock di tabel yang
sama.
20. Share Table – Operasi yang Dilarang
• Sebuah share table yang dimiliki oleh suatu transaksi mencegah transaksi
lainnya untuk memodifikasi tabel yang sama. Berikut ini pernyataan yang
dilarang dalam mengeksekusi share table:
LOCK TABLE table IN SHARE ROW EXCLUSIVE MODE;
LOCK TABLE table IN EXCLUSIVE MODE;
LOCK TABLE table IN ROW EXCLUSIVE MODE;
21. Contoh 1 : Share Table Locks (S)
Session 1 Session 2
lock table emp
in share mode;
OK
update emp
set ename = ‘BHISMA’
where empno = 7900;
commit;
OK
lock table emp
in share mode;
OK
This and other Transactions h ave to wait until Session 2 commits the
Transaction.
update emp
set ename = 'MUHAIMIN'
where empno = 7900;
Waiting ....
select * from emp;
OK
This and other Transactions have to wait until Session 1 commits the
Transaction.
This and other Transactions can
get a Share Lock (Lock Switch).
lock table emp
in share mode;
OK
22. Contoh 2 : Share Table Locks (S)
LOCK TABLE dept IN SHARE MODE; /* Transaksi lainnya harus menunggu*/
UPDATE emp
SET sal = sal * 1.1
WHERE deptno IN
(SELECT deptno FROM dept WHERE loc = ‘ANDY');
UPDATE budget
SET Totsal = Totsal * 1.1
WHERE deptno IN
(SELECT deptno FROM dept WHERE Loc = ‘ANDY');
COMMIT; /* release the lock, open the door xD */
23. Exclusive Table Locks (X)
• An exclusive table lock is acquired for a table as follows:
LOCK TABLE table IN EXCLUSIVE MODE;
24. Exclusive Table Locks (X) – Operasi
yang Diijinkan
• Only one transaction can obtain an exclusive table lock for a table.
Hanya satu transaksi yang dapat memperoleh Exclusive Table Locks untuk sebuah
tabel.
• An exclusive table lock permits other transactions only to query the table.
Sebuah Exclusive Table Locks memungkinkan transaksi lainnya hanya untuk
melakukan query pada tabel.
25. Exclusive Table Locks (X) – Operasi
yang Dilarang
• An exclusive table lock held by a transaction prohibits other transactions
from performing any type of DML statement or placing any type of lock on
the table.
Sebuah exclusive table lock yang dimiliki oleh transaksi melarang adanya transaksi
lainnya dari melakukan setiap jenis pernyataan DML atau menempatkan jenis
pernyataan lock pada tabel.
26. Be careful to use an EXCLUSIVE lock!
• Your transaction requires immediate update access to the locked table. When your
transaction holds an exclusive table lock, other transactions cannot lock specific rows
in the locked table.
• Your transaction also ensures transaction-level read consistency for the locked table
until the transaction is committed or rolled back.
• You are not concerned about low levels of data concurrency, making transactions that
request exclusive table locks wait in line to update the table sequentially.
27. Contoh Exclusive Table Locks (X)
Session 1 Session 2
lock table emp
in exclusive mode;
OK
update emp
set ename = ‘HENDRY'
where empno = 7900;
commit;
OK
lock table emp
in exclusive mode;
OK
select * from emp;
OK
This and other Transactions have to wait until Session 1 commits the Transaction.
This and other Transactions cannot
get any other Lock (No Lock Switch).
lock table emp
in share mode;
Waiting ....
lock table emp
in exclusive mode;
Waiting ....
update emp
set ename = ‘HENDRY'
where empno = 7900;
Waiting ....