Oracle Locking (3)

39
Oracle Locking

Transcript of Oracle Locking (3)

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 1/39

Oracle Locking

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 2/39

Oracle Locking Agenda

• Oracle Locking a Brief Description

• Oracle Isolation Levels

• Setting Isolation Level

Oracle Lock Duration• Oracle Lock Modes

• Oracle Lock Types

• Oracle DML Lock Types/Modes

• Oracle DDL Locks Modes

• Oracle Lock Escalation

• Deadlocks

• Snapshot too old brief description

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 3/39

Oracle Locking a Brief Description

• Locks are mechanisms that prevent destructive interaction

between transactions accessing the same resource.

General Object Type Affected By Locks:

• User objects, such as tables and rows (structures and data)

• System objects not visible to users, such as shared data

structures in the memory and data dictionary rows

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 4/39

Oracle Isolation Levels

Isolation Levels are how Oracle executes SQLstatements in regards to read consistency andis directly related to what lock may be

ignored.

• Read Committed (Default)

•Serializable Transactions

• Read-only

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 5/39

Oracle Isolation Levels

Read Committed (Oracle Default)

Each query executed by a transaction sees only data that wascommitted before the query (not the transaction) began. AnOracle query will never read dirty (uncommitted) data.

Because Oracle does not prevent other transactions frommodifying the data read by a query, that data may be changed

by other transactions between two executions of the query

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 6/39

Oracle Isolation Levels

Serializable Transactions

See only those changes that were committed at the

time the transaction began, plus those changesmade by the transaction itself through INSERT,

UPDATE, and DELETE statements.

* Note: Not Usable in Distributed Transactions

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 7/39

Oracle Isolation Levels

Read-Only

See only those changes that were committed atthe time the transaction began and do not

allow INSERT, UPDATE, and DELETE

statements.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 8/39

Setting Oracle Isolation Level

Setting at Transaction Level:

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SET TRANSACTION ISOLATION LEVEL READ ONLY;

Setting at Session Level:

 ALTER SESSION SET ISOLATION_LEVEL READ COMMITTED; ALTER SESSION SET ISOLATION_LEVEL SERIALIZABLE;

 ALTET SESSION SET ISOLATION_LEVEL READ ONLY;

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 9/39

Oracle Lock Duration

• All locks acquired by statements within a transaction are heldfor the duration of the transaction.

• Oracle releases all locks acquired by the statements within a

transaction when an explict or implied commit or roll back isexecuted. Oracle also releases locks acquired after a savepointwhen rolling back to the savepoint.

* Note: Only transactions not waiting for the previously locked resources canacquire locks on now available resources. Waiting transactions continue towait until after the original transaction commits or completely rolls back.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 10/39

Oracle Lock Modes

• Exclusive Lock Mode

• Share Lock Mode

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 11/39

Oracle Exclusive Lock Mode

Exclusive Lock Mode

Prevents the associates resource from being

shared. This lock mode is obtained to modifydata. The first transaction to lock a resourceexclusively is the only transaction that can

alter the resource until the exclusive lock isreleased.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 12/39

Oracle Share Lock Mode

Share Lock Mode

Allows the associated resource to be shared, depending

on the operations involved. Multiple users readingdata can share the data, holding share locks to

prevent concurrent access by a writer (who needs an

exclusive lock). Several transactions can acquire

share locks on the same resource.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 13/39

Oracle Lock Types

• DML locks (data locks)

• DDL locks (dictionary locks)

Oracle Internal Locks/Latches• Oracle Distributed Locks

• Oracle Parallell Cache Management Locks

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 14/39

Oracle DML Locks

DML locks (data locks)

DML locks protect data. For example, table locks lock

entire tables, row locks lock selected rows.

DML operations can acquire data locks at two different

levels: for specific rows and for entire tables.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 15/39

Oracle DML Lock Types

• Row Level Locks

• Table Level Locks

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 16/39

Oracle Row Locks [TX]

• All DML locks Oracle acquires automatically are row-level locks.

• No limit to the number of row locks held by atransaction.

• Oracle does not escalate locks from the row level.

• Row locking provides the lowest level of lockingpossible provides the best possible transaction

concurrency.• Readers of data do not wait for writers of the same

data rows.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 17/39

Oracle Row Level Locks [TX]

Continued

• A modified row is always locked exclusively so that otherusers cannot modify the row until the transaction holding thelock is committed or rolled back.

• If a transaction obtains a row lock for a row, the transaction

also acquires a table lock for the corresponding table. Thetable lock prevents conflicting DDL operations that wouldoverride data changes in a current transaction.

* Note: A transaction gets an exclusive DML lock for each rowmodified by any of the following statements: INSERT, UPDATE,DELETE, and SELECT with the FOR UPDATE clause.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 18/39

Oracle Table Level Lock [TM]

• A transaction acquires a table lock for DML statements suchas INSERT/UPDATE/DELETE, SELECT with the FOR UPDATE, andLOCK TABLE. Reasons are to reserve DML access to the tableon behalf of a transaction and prevent DDL operations

•Table locks prevent the an exclusive DDL lock on the sametable which prevents DDL operations. Example, a tablecannot be altered or dropped if any uncommitted transactionholds a table lock for it.

• A table lock can be held in several modes: row share (RS), row

exclusive (RX), share (S), share row exclusive (SRX), andexclusive (X).

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 19/39

Oracle Table Level Lock [TM]

Continued

• The restrictiveness of a table lock's mode determines

the modes in which other table locks on the same

table can be obtained and held.**

** Table Next Slide Explains restrictiveness of lock

modes.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 20/39

Oracle Table Lock Mode TableLock Lock Modes Permitted?

SQL Mode RS RX S SRX X

SELECT Y Y Y Y Y

INSERT RX Y Y N N N

UPDATE RX Y* Y* N N N

DELETE RX Y* Y* N N N

SELECT.. FOR UPDATE OF.. RS Y* Y* Y* Y* NLOCK TABLE <table_name>

IN ROW SHARE MODE RS Y Y Y Y N

IN ROW EXCLUSIVE MODE RX Y Y N N N

IN SHARE MODE S Y N Y N N

IN SHARE ROW EXCLUSIVE MODE SRX Y N N N N

IN EXCLUSIVE MODE X N N N N N

RS: row share RX: row exclusive S: share

SRX: share row exclusive X: exclusive

* Waits if another transaction has a lock

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 21/39

Oracle Table Lock Mode (RS)

Row Share Table Lock (RS)

• Indicates a transaction holding the lock on the table has locked rows in the

table and intends to update them.

• Permitted Operations: Allows other transactions to query, insert, update,

delete, or lock rows concurrently in the same table. Therefore, other

transactions can obtain simultaneous row share, row exclusive, share, and

share row exclusive table locks for the same table.

• Prohibited Operations: Lock Table in Exclusive Mode. 

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 22/39

Oracle Table Lock Mode (RX)Row Exclusive Table Lock (RX)

• Indicates that a transaction holding the lock has made one or moreupdates to rows in the table. A row exclusive table lock is acquiredautomatically by: INSERT, UPDATE, DELETE, LOCK TABLE.. INROW EXCLUSIVE MODE; A row exclusive table lock is slightly

more restrictive than a row share table lock.

• Permitted Operations: Allows other transactions to query, insert,update, delete, or lock rows in the same table. The row exclusivetable locks allow multiple transactions to obtain simultaneous rowexclusive and row share table locks in the same table.

• Prohibited Operations: Prevents locking the table for exclusive readingor writing. Therefore, other transactions cannot concurrently lock thetable: IN SHARE MODE, IN SHARE EXCLUSIVE MODE, or INEXCLUSIVE MODE.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 23/39

Oracle Table Lock Mode (S)

Share Table Lock (S)

• Acquired automatically for the table specified in the followingstatement: LOCK TABLE <table> IN SHARE MODE;

Permitted Operations: Allows other transactions only to query thetable, to lock specific rows with SELECT . . . FOR UPDATE, or to executeLOCK TABLE . . . IN SHARE MODE; no updates are allowed by othertransactions. Multiple transactions can hold share table locks for thesame table concurrently. No transaction can update the table (withSELECT.. FOR UPDATE). Therefore, a transaction that has a share tablelock can update the table only if no other transaction has a share

table lock on the same table.

• Prohibited Operations: Prevents other transactions from modifyingthe same table or lock table: IN SHARE ROW EXCLUSIVEMODE, IN EXCLUSIVE MODE, or IN ROW EXCLUSIVEMODE.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 24/39

Oracle Table Lock Mode (SRX)

Share Row Exclusive Table Lock (SRX)

• More restrictive than a share table lock. A share row exclusive tablelock is acquired for a table as follows: LOCK TABLE <table> INSHARE ROW EXCLUSIVE MODE;

• Permitted Operations: Only one transaction at a time can acquire ashare row exclusive table lock on a given table. A share row exclusivetable lock held by a transaction allows other transactions to query orlock specific rows using SELECT with the FOR UPDATE clause, but notto update the table.

• Prohibited Operations: Prevents other transactions from obtainingrow exclusive table locks and modifying the same table. A share rowexclusive table lock also prohibits other transactions from obtainingshare, share row exclusive, and exclusive table locks. 

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 25/39

Oracle Table Lock Mode (X)

Exclusive Table Lock (X)

• Most restrictive mode of table lock, allowing thetransaction that holds the lock exclusive write access tothe table. An exclusive table lock is acquired by: LOCK

TABLE <table> IN EXCLUSIVE MODE;

• Permitted Operations: Only one transaction can obtain anexclusive table lock for a table. An exclusive table lockpermits other transactions only to query the table.

• Prohibited Operations: Prohibits other transactions fromperforming any type of DML statement or placing any typeof lock on the table.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 26/39

Oracle DDL LocksDDL locks (dictionary locks)

• Protects the definition of an object while being used by aDDL operation. Recall that a DDL statement implicitlycommits.

• Create Procedure will automatically acquire DDL locks forall schema objects referenced in the procedure definition.The DDL locks prevent objects referenced in theprocedure from being altered/dropped before the compileis complete.

• Cannot explicitly request DDL locks. Individual schema

objects that are modified or referenced are locked duringDDL operations; the whole data dictionary is never locked. 

• Three categories: exclusive DDL locks, share DDL locks,and breakable parse locks.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 27/39

Oracle DDL Lock Modes

• Exclusive DDL Locks

• Shared DDL Locks

Breakable Parse Locks

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 28/39

Oracle Exclusive DDL Lock

• Most DDL operations require exclusive DDL locks fora resource to prevent destructive interference withother DDL operations on the same object.

• In the acquisition of an exclusive DDL lock, if anotherDDL lock is already held on the object by anotheroperation, the lock get waits until the other DDL lockis released before proceeding.

• DDL operations also acquire DML locks (data locks)on the schema object to be modified.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 29/39

Oracle Shared DDL Lock

• Some DDL need a share DDL lock for an object to preventdestructive interference other conflict DDL operations, butallow data concurrency for other DDL. For example, when aCREATE PROCEDURE executes, the transaction acquires share

DDL locks for all referenced tables. Other transactions canconcurrently create procedures that reference the sametables and therefore acquire concurrent share DDL locks onthe same tables, but no transaction can acquire an exclusiveDDL lock on any referenced table. No transaction can alter or

drop a referenced table. As a result, a transaction that holds ashare DDL lock is guaranteed that the definition of thereferenced schema object will remain constant for theduration of the transaction.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 30/39

Oracle Shared DDL Lock Continued

• Gotten on an object for DDL statements that

have: AUDIT, NOAUDIT, COMMENT, CREATE

[OR REPLACE] VIEW/PROCEDURE/PACKAGE/

PACKAGE BODY/FUNCTION/ TRIGGER, CREATESYNONYM, and CREATE TABLE (when the

CLUSTER parameter is not included).

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 31/39

Oracle Breakable Parse Locks

• A SQL statement (or PL/SQLprogram unit) in the

shared pool holds a parse lock for each object

referenced. Parse locks are gotten so that the

associated shared SQL area can be invalidated if areferenced object is altered or dropped. A parse

lock does not disallow any DDL operation and can

be broken to allow conflicting DDL operations.

• Gotten during the parse phase of SQL statementexecution and held as long as the shared SQL area

for that statement remains in the shared pool.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 32/39

Oracle Internal Locks/Latches

Internal Locks/Latches

Internal locks and latches protect Oracle internal

database structures such like datafiles.Internal locks and latches are entirely handledby Oracle internal functions and are

automatic. Some Internal Latches can beturned by an Oracle DBA.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 33/39

Oracle Latches

• Latches are low-level serialization mechanisms toprotect shared data structures in the systemglobal area (SGA). Latches protect the oracle listslike list of users currently accessing the databaseand protect the data structures describing theblocks in the buffer cache. A server or backgroundprocess acquires a latch for a very short timewhile manipulating or looking at one of these

structures. The implementation of latches isoperating system dependent, particularly inregard to whether and how long a process willwait for a latch.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 34/39

Oracle Internal Locks• Data Dictionary Locks

 – Held on entries in dictionary caches while the entries are being

modified or used. They guarantee that statements being parsed do

not see inconsistent object definitions.

• File and Log Management Locks

 –

Protect various files like control files, redo log files so that only oneprocess at a time can change it. Datafiles are locked to ensure that

multiple instances mount a database in shared mode or that one

instance mounts it in exclusive mode.

• Tablespace and Rollback Segment Locks

 – Protect tablespaces and rollback segments. Example, all instancesaccessing a database must agree on if s tablespace is online or

offline. Rollback segments are locked so that only one instance can

write to a segment.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 35/39

Oracle Distributed Locks

Distributed Locks

Distributed locks ensure that the data and other

resources distributed among the various

instances consistent. Distributed locks are

held by instances rather than transactions.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 36/39

Oracle Parallel Cache Management

Locks

Parallel Cache Management (PCM) Locks

Parallel cache management locks are distributed

locks that cover one or more data blocks

(table or index blocks) in the buffer cache.

PCM locks do not lock any rows on behalf of 

transactions.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 37/39

Data Lock Escalation• A transaction holds exclusive row locks for all rows

inserted, updated, or deleted within the transaction.Because row locks are acquired at the highest degree of restrictiveness, no lock conversion is required orperformed.

• Oracle automatically converts a table lock of lowerrestrictiveness to one of higher restrictiveness asappropriate. For example, assume that a transaction usesa SELECT statement with the FOR UPDATE clause to lock

rows of a table. As a result, it acquires the exclusive rowlocks and a row share table lock for the table. If thetransaction later updates one or more of the locked rows,the row share table lock is automatically converted to arow exclusive table lock.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 38/39

Oracle Lock Escalation Continued

• Occurs when many locks are held at one level like rows,and the database raises the locks to a higher level like atable lock. If many row locks on a table, the database willautomatically escalate the row locks to a single table lockfor the transaction. The physical number of locks is

reduced, but the restrictiveness is increased. **• Lock escalation increases the possiblity of deadlocks.

Imagine the situation where the system is trying toescalate locks on behalf of transaction T1 but cannotbecause of the locks held by transaction T2. A deadlock is

created if transaction T2 also requires escalation of thesame data before it can proceed.

** Note: Oracle dose not do Lock Escalation to ExclusiveTable Level for numerous row locks.

8/2/2019 Oracle Locking (3)

http://slidepdf.com/reader/full/oracle-locking-3 39/39

Deadlocks

• Oracle automatically detects deadlock situations and

resolves them by rolling back one of the statementsinvolved in the deadlock. This releases one set of theconflicting row locks. A corresponding message also isreturned to the transaction that undergoes the rollback.

• Deadlocks often occur when transactions override Oracle

default locking. Oracle itself does no lock escalation anddoes not use read locks for queries and does not usepage-level locking, deadlocks rarely occur in Oracle.

• Deadlocks can usually be avoided if transactions accessingthe same tables lock those tables in the same order, either

through implicit or explicit locks and when a sequence of locks for one transaction are required, you shouldconsider acquiring the most exclusive (least compatible)lock first

• Always close explicit cursors when finished to free locks.