oracle_locking.ppt
TRANSCRIPT
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 1/50
Oracle Locking
Michael Messina
Principal Database AnalystIndiana University
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 2/50
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/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 3/50
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 datastructures in the memory and data dictionary rows
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 4/50
Oracle Isolation Levels
Isolation Levels are how Oracle executes SQLstatements in regards to read consistency
and is directly related to what lock may beignored.
•
Read Committed (Default)• Serializable Transactions
• Read-only
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 5/50
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 from
modifying the data read by a query, that data may bechanged by other transactions between two executions ofthe query
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 6/50
Oracle Isolation Levels
Serializable Transactions
See only those changes that were committed at thetime the transaction began, plus those changes
made by the transaction itself through INSERT,
UPDATE, and DELETE statements.
* Note: Not Usable in Distributed Transactions
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 7/50
Oracle Isolation Levels
Read-Only
See only those changes that were committed
at the time the transaction began and do not
allow INSERT, UPDATE, and DELETE
statements.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 8/50
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/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 9/50
Oracle Lock Duration
• All locks acquired by statements within a transaction areheld for the duration of the transaction.
• Oracle releases all locks acquired by the statements withina transaction when an explict or implied commit or roll
back is executed. Oracle also releases locks acquired aftera savepoint when rolling back to the savepoint.
* Note: Only transactions not waiting for the previously locked resourcescan acquire locks on now available resources. Waiting transactionscontinue to wait until after the original transaction commits orcompletely rolls back.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 10/50
Oracle Lock Modes
• Exclusive Lock Mode
• Share Lock Mode
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 11/50
Oracle Exclusive Lock Mode
Exclusive Lock Mode
Prevents the associates resource from beingshared. This lock mode is obtained tomodify data. The first transaction to lock a
resource exclusively is the only transactionthat can alter the resource until theexclusive lock is released.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 12/50
Oracle Share Lock Mode
Share Lock Mode
Allows the associated resource to be shared,depending on the operations involved. Multiple
users reading data 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/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 13/50
Oracle Lock Types
• DML locks (data locks)
• DDL locks (dictionary locks)
• Oracle Internal Locks/Latches
• Oracle Distributed Locks
•
Oracle Parallell Cache Management Locks
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 14/50
Oracle DML Locks
DML locks (data locks)
DML locks protect data. For example, table lockslock entire tables, row locks lock selected rows.
DML operations can acquire data locks at twodifferent levels: for specific rows and for entire
tables.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 15/50
Oracle DML Lock Types
• Row Level Locks
• Table Level Locks
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 16/50
Oracle Row Locks [TX]
• All DML locks Oracle acquires automatically arerow-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 locking
possible provides the best possible transactionconcurrency.
• Readers of data do not wait for writers of the samedata rows.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 17/50
Oracle Row Level Locks [TX]
Continued• A modified row is always locked exclusively so that other
users cannot modify the row until the transaction holdingthe lock is committed or rolled back.
• If a transaction obtains a row lock for a row, thetransaction also acquires a table lock for the correspondingtable. The table lock prevents conflicting DDL operationsthat would override data changes in a current transaction.
* Note: A transaction gets an exclusive DML lock for eachrow modified by any of the following statements:INSERT, UPDATE, DELETE, and SELECT with the FORUPDATE clause.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 18/50
Oracle Table Level Lock [TM]
• A transaction acquires a table lock for DML statementssuch as INSERT/UPDATE/DELETE, SELECT with theFOR UPDATE, and LOCK TABLE. Reasons are to
reserve DML access to the table on behalf of a transactionand prevent DDL operations
• Table locks prevent the an exclusive DDL lock on thesame table which prevents DDL operations. Example, atable cannot be altered or dropped if any uncommitted
transaction holds 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),and exclusive (X).
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 19/50
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/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 20/50
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/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 21/50
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/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 22/50
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 exclusive tablelocks allow multiple transactions to obtain simultaneous row exclusiveand row share table locks in the same table.
• Prohibited Operations: Prevents locking the table for exclusivereading or writing. Therefore, other transactions cannot concurrentlylock the table: IN SHARE MODE, IN SHARE EXCLUSIVEMODE, or IN EXCLUSIVE MODE.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 23/50
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 toexecute LOCK TABLE . . . IN SHARE MODE; no updates areallowed by other transactions. Multiple transactions can hold sharetable locks for the same table concurrently. No transaction can update
the table (with SELECT.. FOR UPDATE). Therefore, a transactionthat has a share table lock can update the table only if no othertransaction has a share table lock on the same table.
• Prohibited Operations: Prevents other transactions from modifying thesame table or lock table: IN SHARE ROW EXCLUSIVE MODE, IN
EXCLUSIVE MODE, or IN ROW EXCLUSIVE MODE.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 24/50
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, butnot to update the table.
• Prohibited Operations: Prevents other transactions from obtaining rowexclusive 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/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 25/50
Oracle Table Lock Mode (X)
Exclusive Table Lock (X)
• Most restrictive mode of table lock, allowing thetransaction that holds the lock exclusive write access to the
table. An exclusive table lock is acquired by: LOCKTABLE <table> IN EXCLUSIVE MODE;
• Permitted Operations: Only one transaction can obtain anexclusive table lock for a table. An exclusive table lock
permits other transactions only to query the table.
• Prohibited Operations: Prohibits other transactions from performing any type of DML statement or placing any type
of lock on the table.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 26/50
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 the procedurefrom being altered/dropped before the compile is complete.
• Cannot explicitly request DDL locks. Individual schemaobjects that are modified or referenced are locked during
DDL operations; the whole data dictionary is never locked.
• Three categories: exclusive DDL locks, share DDL locks,and breakable parse locks.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 27/50
Oracle DDL Lock Modes
• Exclusive DDL Locks
• Shared DDL Locks
• Breakable Parse Locks
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 28/50
Oracle Exclusive DDL Lock
• Most DDL operations require exclusive DDLlocks for a resource to prevent destructiveinterference with other DDL operations on the
same object.
• In the acquisition of an exclusive DDL lock, ifanother DDL lock is already held on the object byanother operation, the lock get waits until theother DDL lock is released before proceeding.
• DDL operations also acquire DML locks (datalocks) on the schema object to be modified.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 29/50
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
a CREATE PROCEDURE executes, the transactionacquires share DDL locks for all referenced tables. Othertransactions can concurrently create procedures thatreference the same tables and therefore acquire concurrentshare DDL locks on the same tables, but no transaction can
acquire an exclusive DDL lock on any referenced table. Notransaction can alter or drop a referenced table. As a result,a transaction that holds a share DDL lock is guaranteedthat the definition of the referenced schema object willremain constant for the duration of the transaction.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 30/50
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, CREATE SYNONYM, andCREATE TABLE (when the CLUSTER
parameter is not included).
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 31/50
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/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 32/50
Oracle Internal Locks/Latches
Internal Locks/Latches
Internal locks and latches protect Oracleinternal database structures such likedatafiles. Internal locks and latches areentirely handled by Oracle internalfunctions and are automatic. Some InternalLatches can be turned by an Oracle DBA.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 33/50
Oracle Latches
• Latches are low-level serialization mechanisms to protect shared data structures in the system globalarea (SGA). Latches protect the oracle lists likelist of users currently accessing the database and
protect the data structures describing the blocks inthe buffer cache. A server or background processacquires a latch for a very short time whilemanipulating or looking at one of these structures.
The implementation of latches is operating systemdependent, particularly in regard to whether andhow long a process will wait for a latch.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 34/50
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 one process 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/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 35/50
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 thantransactions.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 36/50
Oracle Parallel Cache
Management LocksParallel 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 anyrows on behalf of transactions.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 37/50
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 ofrestrictiveness, no lock conversion is required or
performed.
• Oracle automatically converts a table lock of lowerrestrictiveness to one of higher restrictiveness asappropriate. For example, assume that a transaction uses aSELECT 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/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 38/50
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 lock
for the transaction. The physical number of locks isreduced, but the restrictiveness is increased. **
• Lock escalation increases the possiblity of deadlocks.Imagine the situation where the system is trying to escalatelocks on behalf of transaction T1 but cannot because of the
locks held by transaction T2. A deadlock is created iftransaction T2 also requires escalation of the same data
before it can proceed.
** Note: Oracle dose not do Lock Escalation to ExclusiveTable Level for numerous row locks.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 39/50
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 Oracledefault 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 accessing
the same tables lock those tables in the same order, eitherthrough implicit or explicit locks and when a sequence oflocks for one transaction are required, you should consideracquiring the most exclusive (least compatible) lock first
• Always close explicit cursors when finished to free locks.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 40/50
Snapshot Too Old
• Snapshot too old occurs when:
– (a) transaction reads a table (a)
–
Another transaction (b) updates a row in table (a) – The update transaction (b) commits, the commit
releases the rollback segment for another transaction to
use.
–
Another transaction (c) uses that same rollback segmentand from update transaction (b) and overwrites the
rollback information from the update transaction (b)
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 41/50
Snapshot Too Old
– The transaction (a) tries to read the row
transaction (b) updated and to maintain read
consistency Oracle has to goto rollback for the previous view of the record. Since transaction
(c) has over wrote the rollback from transaction
(b) transaction (a) can not get a read consistent
view of the row which causes the snapshot tooold error to be returned.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 42/50
Snapshot Too Old
• Things that can affect occurance of snapshot too
old.
–
Rollback Segment Max Size – Rollback Segment Optimal Size
– Number of Rollback Segments
–
Proper application transaction activity
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 43/50
Rollback Segment Max Size
• The maximum size a rollback segment can
grow too can have an impact on how long a
particular change can be held in a rollbacksegment before being over wrote causing
the snapshot too old error.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 44/50
Rollback Segment Optimal Size
• The rollback segment can have what is called andoptimal size. The optimal size is what a rollbacksegment will shrink back to after the space beyond
optimal has not been used for a period of time.This affects when a large transaction does a DMLoperation and the rollback segment must grow
beyond optimal and then commits. The rollback
information contained in the space beyond optimalcan not be accessed if the rollback segmentshrinks back to optimal.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 45/50
Number of Rollback Segments
• The total number of rollback segments in
the database can affect the snapshot too old
by making it less likely that a rollbacksegment would be used by another
transaction.
Note: Additional rollback segments dose notensure that more rollback segments are used
in lower transaction activity environments
P A li i T i
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 46/50
Proper Application Transaction
Activity
• 98% of all snapshot too old errors are due to
conflicting transactional activity, mostly in
batch processing jobs.• Keep conflicting update and select jobs
from running at the same time. This has to
do with the design of application jobs and process flow of jobs in batch. Which jobs
run concurrently and in what order jobs run.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 47/50
Proper Application Transaction
Activity• Keep Updates under the Rollback segment optimal
if you can not consider speaking with a DBA
about rollbacks with high optimal size.
• Use special rollback segments for large DML jobs.
• Commit in DML jobs as little as possible. This let
rollbacks larger, but will keep undo information in
rollback segments until commit and can have alarger optimal size.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 48/50
Proper Application Transaction
Activity• Keep transactions as fast as possible. The
faster activity completes will reduce the
chances of updates that may cause snapshottoo old.
8/14/2019 oracle_locking.ppt
http://slidepdf.com/reader/full/oraclelockingppt 49/50
CREDITS
• Oracle Metalink White Paper 70120
• Oracle 8.1.7 Documentation
• My own knowledge and experience using
Oracle