+ All Categories
Home > Technology > Oracle Administrator - Locking presentation

Oracle Administrator - Locking presentation

Date post: 12-May-2015
Category:
Upload: muhaimin-soltief
View: 417 times
Download: 0 times
Share this document with a friend
Description:
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.
Popular Tags:
28
Universitas Multimedia Nusantara, Major of Information System Muhaimin A. R. Soltief | Bhisma Mahendra | Andy Surya Darmawan | Hendry Setiawan | Yusuf Ramdhan Locking
Transcript
Page 1: Oracle Administrator - Locking presentation

Universitas Multimedia Nusantara, Major of Information System

Muhaimin A. R. Soltief | Bhisma Mahendra | Andy Surya Darmawan

| Hendry Setiawan | Yusuf Ramdhan

Locking

Page 2: Oracle Administrator - Locking presentation

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

Page 3: Oracle Administrator - Locking presentation

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.

Page 4: Oracle Administrator - Locking presentation

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.

Page 5: Oracle Administrator - Locking presentation

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 1SQL> 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:

Page 6: Oracle Administrator - Locking presentation

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;

Page 7: Oracle Administrator - Locking presentation

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.

Page 8: Oracle Administrator - Locking presentation

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.

Page 9: Oracle Administrator - Locking presentation

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.

Page 10: Oracle Administrator - Locking presentation

Exclusive LocksSQL 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

Page 11: Oracle Administrator - Locking presentation

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.

Page 12: Oracle Administrator - Locking presentation

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 ShareRX :   Row Exclusive

S :    ShareSRX :  Share Row ExclusiveX :    Exclusive

Page 13: Oracle Administrator - Locking presentation

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 SETCOMMISION_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

Page 14: Oracle Administrator - Locking presentation

Peyebab terjadinya Lock Conflict1. 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.

Page 15: Oracle Administrator - Locking presentation

Lock Levels

• Oracle menyediakan dua level lock yang berbeda :

Tabel Level LockRow Level Lock

Page 16: Oracle Administrator - Locking presentation

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.

Page 17: Oracle Administrator - Locking presentation

Lock Levels2. 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.

Page 18: Oracle Administrator - Locking presentation

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;

Page 19: Oracle Administrator - Locking presentation

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 UPDATEatau 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.

Page 20: Oracle Administrator - Locking presentation

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;

Page 21: Oracle Administrator - Locking presentation

Contoh 1 : Share Table Locks (S)Session 1 Session 2

lock table empin share mode;OK

update empset ename = ‘BHISMA’where empno = 7900;commit;OK

lock table empin share mode;OK

This and other Transactions h ave to wait until Session 2 commits the Transaction.

update empset 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 canget a Share Lock (Lock Switch).

lock table empin share mode;OK

Page 22: Oracle Administrator - Locking presentation

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 */

Page 23: Oracle Administrator - Locking presentation

Exclusive Table Locks (X)

• An exclusive table lock is acquired for a table as follows:

LOCK TABLE table IN EXCLUSIVE MODE;

Page 24: Oracle Administrator - Locking presentation

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.

Page 25: Oracle Administrator - Locking presentation

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.

Page 26: Oracle Administrator - Locking presentation

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.

Page 27: Oracle Administrator - Locking presentation

Contoh Exclusive Table Locks (X) Session 1 Session 2

lock table empin exclusive mode;OK

update empset ename = ‘HENDRY'where empno = 7900;commit;OK

lock table empin exclusive mode;OK

select * from emp;OK

This and other Transactions have to wait until Session 1 commits the Transaction.

This and other Transactions cannotget any other Lock (No Lock Switch).

lock table empin share mode;Waiting ....

lock table empin exclusive mode;Waiting ....

update empset ename = ‘HENDRY'where empno = 7900;Waiting ....

Page 28: Oracle Administrator - Locking presentation

Thank You!


Recommended