Oracle locking


Published on

Published in: Education, Technology, Business
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Oracle locking

  1. 1. Oracle Locking Michael Messina Principal Database Analyst Indiana University
  2. 2. Oracle Locking Agenda <ul><li>Oracle Locking a Brief Description </li></ul><ul><li>Oracle Isolation Levels </li></ul><ul><li>Setting Isolation Level </li></ul><ul><li>Oracle Lock Duration </li></ul><ul><li>Oracle Lock Modes </li></ul><ul><li>Oracle Lock Types </li></ul><ul><li>Oracle DML Lock Types/Modes </li></ul><ul><li>Oracle DDL Locks Modes </li></ul><ul><li>Oracle Lock Escalation </li></ul><ul><li>Deadlocks </li></ul><ul><li>Snapshot too old brief description </li></ul>
  3. 3. Oracle Locking a Brief Description <ul><li>Locks are mechanisms that prevent destructive interaction between transactions accessing the same resource. </li></ul><ul><li>General Object Type Affected By Locks: </li></ul><ul><li>User objects, such as tables and rows (structures and data) </li></ul><ul><li>System objects not visible to users, such as shared data structures in the memory and data dictionary rows </li></ul>
  4. 4. Oracle Isolation Levels <ul><li>Isolation Levels are how Oracle executes SQL statements in regards to read consistency and is directly related to what lock may be ignored. </li></ul><ul><li>Read Committed (Default) </li></ul><ul><li>Serializable Transactions  </li></ul><ul><li>Read-only  </li></ul>
  5. 5. Oracle Isolation Levels <ul><li>Read Committed (Oracle Default) </li></ul><ul><li>Each query executed by a transaction sees only data that was committed before the query (not the transaction) began. An Oracle query will never read dirty (uncommitted) data.   </li></ul><ul><li>Because Oracle does not prevent other transactions from modifying the data read by a query, that data may be changed by other transactions between two executions of the query </li></ul>
  6. 6. Oracle Isolation Levels <ul><li>Serializable Transactions  </li></ul><ul><li>See only those changes that were committed at the time the transaction began, plus those changes made by the transaction itself through INSERT, UPDATE, and DELETE statements. </li></ul><ul><li>* Note: Not Usable in Distributed Transactions </li></ul>
  7. 7. Oracle Isolation Levels <ul><li>Read-Only  </li></ul><ul><li>See only those changes that were committed at the time the transaction began and do not allow INSERT, UPDATE, and DELETE statements.  </li></ul>
  8. 8. Setting Oracle Isolation Level <ul><li>Setting at Transaction Level: </li></ul><ul><li>SET TRANSACTION ISOLATION LEVEL READ COMMITTED; </li></ul><ul><li>SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; </li></ul><ul><li>SET TRANSACTION ISOLATION LEVEL READ ONLY; </li></ul><ul><li>Setting at Session Level: </li></ul><ul><li>ALTER SESSION SET ISOLATION_LEVEL READ COMMITTED; </li></ul><ul><li>ALTER SESSION SET ISOLATION_LEVEL SERIALIZABLE; </li></ul><ul><li>ALTET SESSION SET ISOLATION_LEVEL READ ONLY; </li></ul>
  9. 9. Oracle Lock Duration <ul><li>All locks acquired by statements within a transaction are held for the duration of the transaction. </li></ul><ul><li>Oracle releases all locks acquired by the statements within a transaction when an explict or implied commit or roll back is executed. Oracle also releases locks acquired after a savepoint when rolling back to the savepoint. </li></ul><ul><li>* Note: Only transactions not waiting for the previously locked resources can acquire locks on now available resources. Waiting transactions continue to wait until after the original transaction commits or completely rolls back. </li></ul>
  10. 10. Oracle Lock Modes <ul><li>Exclusive Lock Mode  </li></ul><ul><li>Share Lock Mode  </li></ul>
  11. 11. Oracle Exclusive Lock Mode <ul><li>Exclusive Lock Mode  </li></ul><ul><li>Prevents the associates resource from being shared. This lock mode is obtained to modify data. The first transaction to lock a resource exclusively is the only transaction that can alter the resource until the exclusive lock is released.  </li></ul>
  12. 12. Oracle Share Lock Mode <ul><li>Share Lock Mode  </li></ul><ul><li>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. </li></ul>
  13. 13. Oracle Lock Types <ul><li>DML locks (data locks) </li></ul><ul><li>DDL locks (dictionary locks)  </li></ul><ul><li>Oracle Internal Locks/Latches </li></ul><ul><li>Oracle Distributed Locks </li></ul><ul><li>Oracle Parallell Cache Management Locks </li></ul>
  14. 14. Oracle DML Locks <ul><li>DML locks (data locks)  </li></ul><ul><li>DML locks protect data. For example, table locks lock entire tables, row locks lock selected rows. </li></ul><ul><li>DML operations can acquire data locks at two different levels: for specific rows and for entire tables.   </li></ul>
  15. 15. Oracle DML Lock Types <ul><li>Row Level Locks </li></ul><ul><li>Table Level Locks </li></ul>
  16. 16. Oracle Row Locks [TX] <ul><li>All DML locks Oracle acquires automatically are row-level locks. </li></ul><ul><li>No limit to the number of row locks held by a transaction. </li></ul><ul><li>Oracle does not escalate locks from the row level. </li></ul><ul><li>Row locking provides the lowest level of locking possible provides the best possible transaction concurrency. </li></ul><ul><li>Readers of data do not wait for writers of the same data rows.   </li></ul>
  17. 17. Oracle Row Level Locks [TX] Continued <ul><li>A modified row is always locked exclusively so that other users cannot modify the row until the transaction holding the lock is committed or rolled back. </li></ul><ul><li>If a transaction obtains a row lock for a row, the transaction also acquires a table lock for the corresponding table. The table lock prevents conflicting DDL operations that would override data changes in a current transaction. </li></ul><ul><li>* Note: A transaction gets an exclusive DML lock for each row modified by any of the following statements: INSERT, UPDATE, DELETE, and SELECT with the FOR UPDATE clause. </li></ul>
  18. 18. Oracle Table Level Lock [TM] <ul><li>A transaction acquires a table lock for DML statements such as INSERT/UPDATE/DELETE, SELECT with the FOR UPDATE, and LOCK TABLE. Reasons are to reserve DML access to the table on behalf of a transaction and prevent DDL operations </li></ul><ul><li>Table locks prevent the an exclusive DDL lock on the same table which prevents DDL operations. Example, a table cannot be altered or dropped if any uncommitted transaction holds a table lock for it. </li></ul><ul><li>A table lock can be held in several modes: row share (RS), row exclusive (RX), share (S), share row exclusive (SRX), and exclusive (X). </li></ul>
  19. 19. Oracle Table Level Lock [TM] Continued <ul><li>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.** </li></ul><ul><li>** Table Next Slide Explains restrictiveness of lock modes. </li></ul>
  20. 20. Oracle Table Lock Mode Table <ul><li>Lock Lock Modes Permitted? </li></ul><ul><li>SQL Mode RS RX S SRX X </li></ul><ul><li>SELECT Y Y Y Y Y </li></ul><ul><li>INSERT RX Y Y N N N </li></ul><ul><li>UPDATE RX Y* Y* N N N </li></ul><ul><li>DELETE RX Y* Y* N N N </li></ul><ul><li>SELECT.. FOR UPDATE OF.. RS Y* Y* Y* Y* N </li></ul><ul><li>LOCK TABLE <table_name> </li></ul><ul><li>IN ROW SHARE MODE RS Y Y Y Y N </li></ul><ul><li>IN ROW EXCLUSIVE MODE RX Y Y N N N </li></ul><ul><li>IN SHARE MODE S Y N Y N N </li></ul><ul><li>IN SHARE ROW EXCLUSIVE MODE SRX Y N N N N </li></ul><ul><li>IN EXCLUSIVE MODE X N N N N N </li></ul><ul><li>RS: row share RX: row exclusive S: share </li></ul><ul><li>SRX: share row exclusive X: exclusive </li></ul><ul><li>* Waits if another transaction has a lock </li></ul>
  21. 21. Oracle Table Lock Mode (RS) <ul><li>Row Share Table Lock (RS) </li></ul><ul><li>Indicates a transaction holding the lock on the table has locked rows in the table and intends to update them. </li></ul><ul><li>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. </li></ul><ul><li>Prohibited Operations: Lock Table in Exclusive Mode. </li></ul>
  22. 22. Oracle Table Lock Mode (RX) <ul><li>Row Exclusive Table Lock (RX) </li></ul><ul><li>Indicates that a transaction holding the lock has made one or more updates to rows in the table. A row exclusive table lock is acquired automatically by: INSERT, UPDATE, DELETE, LOCK TABLE.. IN ROW EXCLUSIVE MODE; A row exclusive table lock is slightly more restrictive than a row share table lock. </li></ul><ul><li>Permitted Operations: Allows other transactions to query, insert, update, delete, or lock rows in the same table. The row exclusive table locks allow multiple transactions to obtain simultaneous row exclusive and row share table locks in the same table. </li></ul><ul><li>Prohibited Operations: Prevents locking the table for exclusive reading or writing. Therefore, other transactions cannot concurrently lock the table: IN SHARE MODE, IN SHARE EXCLUSIVE MODE, or IN EXCLUSIVE MODE. </li></ul>
  23. 23. Oracle Table Lock Mode (S) <ul><li>Share Table Lock (S) </li></ul><ul><li>Acquired automatically for the table specified in the following statement: LOCK TABLE <table> IN SHARE MODE; </li></ul><ul><li>Permitted Operations: Allows other transactions only to query the table, to lock specific rows with SELECT . . . FOR UPDATE, or to execute LOCK TABLE . . . IN SHARE MODE; no updates are allowed by other transactions. Multiple transactions can hold share table locks for the same table concurrently. No transaction can update the table (with SELECT.. FOR UPDATE). Therefore, a transaction that has a share table lock can update the table only if no other transaction has a share table lock on the same table. </li></ul><ul><li>Prohibited Operations: Prevents other transactions from modifying the same table or lock table: I N SHARE ROW EXCLUSIVE MODE, IN EXCLUSIVE MODE, or IN ROW EXCLUSIVE MODE. </li></ul>
  24. 24. Oracle Table Lock Mode (SRX) <ul><li>Share Row Exclusive Table Lock (SRX) </li></ul><ul><li>More restrictive than a share table lock. A share row exclusive table lock is acquired for a table as follows: LOCK TABLE <table> IN SHARE ROW EXCLUSIVE MODE; </li></ul><ul><li>Permitted Operations: Only one transaction at a time can acquire a share row exclusive table lock on a given table. A share row exclusive table lock held by a transaction allows other transactions to query or lock specific rows using SELECT with the FOR UPDATE clause, but not to update the table. </li></ul><ul><li>Prohibited Operations: Prevents other transactions from obtaining row exclusive table locks and modifying the same table. A share row exclusive table lock also prohibits other transactions from obtaining share, share row exclusive, and exclusive table locks. </li></ul>
  25. 25. Oracle Table Lock Mode (X) <ul><li>Exclusive Table Lock (X) </li></ul><ul><li>Most restrictive mode of table lock, allowing the transaction that holds the lock exclusive write access to the table. An exclusive table lock is acquired by: LOCK TABLE <table> IN EXCLUSIVE MODE; </li></ul><ul><li>Permitted Operations: Only one transaction can obtain an exclusive table lock for a table. An exclusive table lock permits other transactions only to query the table. </li></ul><ul><li>Prohibited Operations: Prohibits other transactions from performing any type of DML statement or placing any type of lock on the table. </li></ul>
  26. 26. Oracle DDL Locks <ul><li>DDL locks (dictionary locks)  </li></ul><ul><li>Protects the definition of an object while being used by a DDL operation. Recall that a DDL statement implicitly commits. </li></ul><ul><li>Create Procedure will automatically acquire DDL locks for all schema objects referenced in the procedure definition. The DDL locks prevent objects referenced in the procedure from being altered/dropped before the compile is complete. </li></ul><ul><li>Cannot explicitly request DDL locks. Individual schema objects that are modified or referenced are locked during DDL operations; the whole data dictionary is never locked. </li></ul><ul><li>Three categories: exclusive DDL locks, share DDL locks, and breakable parse locks. </li></ul>
  27. 27. Oracle DDL Lock Modes <ul><li>Exclusive DDL Locks </li></ul><ul><li>Shared DDL Locks </li></ul><ul><li>Breakable Parse Locks </li></ul>
  28. 28. Oracle Exclusive DDL Lock <ul><li>Most DDL operations require exclusive DDL locks for a resource to prevent destructive interference with other DDL operations on the same object. </li></ul><ul><li>In the acquisition of an exclusive DDL lock, if another DDL lock is already held on the object by another operation, the lock get waits until the other DDL lock is released before proceeding. </li></ul><ul><li>DDL operations also acquire DML locks (data locks) on the schema object to be modified. </li></ul>
  29. 29. Oracle Shared DDL Lock <ul><li>Some DDL need a share DDL lock for an object to prevent destructive interference other conflict DDL operations, but allow data concurrency for other DDL. For example, when a CREATE PROCEDURE executes, the transaction acquires share DDL locks for all referenced tables. Other transactions can concurrently create procedures that reference the same tables and therefore acquire concurrent share DDL locks on the same tables, but no transaction can acquire an exclusive DDL lock on any referenced table. No transaction can alter or drop a referenced table. As a result, a transaction that holds a share DDL lock is guaranteed that the definition of the referenced schema object will remain constant for the duration of the transaction. </li></ul>
  30. 30. Oracle Shared DDL Lock Continued <ul><li>Gotten on an object for DDL statements that have: AUDIT, NOAUDIT, COMMENT, CREATE [OR REPLACE] VIEW/PROCEDURE/PACKAGE/ PACKAGE BODY/FUNCTION/ TRIGGER, CREATE SYNONYM, and CREATE TABLE (when the CLUSTER parameter is not included). </li></ul>
  31. 31. Oracle Breakable Parse Locks <ul><li>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 a referenced object is altered or dropped. A parse lock does not disallow any DDL operation and can be broken to allow conflicting DDL operations. </li></ul><ul><li>Gotten during the parse phase of SQL statement execution and held as long as the shared SQL area for that statement remains in the shared pool. </li></ul>
  32. 32. Oracle Internal Locks/Latches <ul><li>Internal Locks/Latches   </li></ul><ul><li>Internal locks and latches protect Oracle internal database structures such like datafiles. Internal locks and latches are entirely handled by Oracle internal functions and are automatic. Some Internal Latches can be turned by an Oracle DBA.   </li></ul>
  33. 33. Oracle Latches <ul><li>Latches are low-level serialization mechanisms to protect shared data structures in the system global area (SGA). Latches protect the oracle lists like list of users currently accessing the database and protect the data structures describing the blocks in the buffer cache. A server or background process acquires a latch for a very short time while manipulating or looking at one of these structures. The implementation of latches is operating system dependent, particularly in regard to whether and how long a process will wait for a latch. </li></ul>
  34. 34. Oracle Internal Locks <ul><li>Data Dictionary Locks </li></ul><ul><ul><li>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. </li></ul></ul><ul><li>File and Log Management Locks </li></ul><ul><ul><li>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. </li></ul></ul><ul><li>Tablespace and Rollback Segment Locks </li></ul><ul><ul><li>Protect tablespaces and rollback segments. Example, all instances accessing 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. </li></ul></ul>
  35. 35. Oracle Distributed Locks <ul><li>Distributed Locks  </li></ul><ul><li>Distributed locks ensure that the data and other resources distributed among the various instances consistent. Distributed locks are held by instances rather than transactions.   </li></ul>
  36. 36. Oracle Parallel Cache Management Locks <ul><li>Parallel Cache Management (PCM) Locks  </li></ul><ul><li>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.   </li></ul>
  37. 37. Data Lock Escalation <ul><li>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 or performed. </li></ul><ul><li>Oracle automatically converts a table lock of lower restrictiveness to one of higher restrictiveness as appropriate. For example, assume that a transaction uses a SELECT statement with the FOR UPDATE clause to lock rows of a table. As a result, it acquires the exclusive row locks and a row share table lock for the table. If the transaction later updates one or more of the locked rows, the row share table lock is automatically converted to a row exclusive table lock. </li></ul>
  38. 38. Oracle Lock Escalation Continued <ul><li>Occurs when many locks are held at one level like rows, and the database raises the locks to a higher level like a table lock. If many row locks on a table, the database will automatically escalate the row locks to a single table lock for the transaction. The physical number of locks is reduced, but the restrictiveness is increased. ** </li></ul><ul><li>Lock escalation increases the possiblity of deadlocks. Imagine the situation where the system is trying to escalate locks on behalf of transaction T1 but cannot because of the locks held by transaction T2. A deadlock is created if transaction T2 also requires escalation of the same data before it can proceed. </li></ul><ul><li>** Note: Oracle dose not do Lock Escalation to Exclusive Table Level for numerous row locks. </li></ul>
  39. 39. Deadlocks <ul><li>Oracle automatically detects deadlock situations and resolves them by rolling back one of the statements involved in the deadlock. This releases one set of the conflicting row locks. A corresponding message also is returned to the transaction that undergoes the rollback. </li></ul><ul><li>Deadlocks often occur when transactions override Oracle default locking. Oracle itself does no lock escalation and does not use read locks for queries and does not use page-level locking , deadlocks rarely occur in Oracle. </li></ul><ul><li>Deadlocks can usually be avoided if transactions accessing the 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 should consider acquiring the most exclusive (least compatible) lock first </li></ul><ul><li>Always close explicit cursors when finished to free locks. </li></ul>
  40. 40. Snapshot Too Old <ul><li>Snapshot too old occurs when: </li></ul><ul><ul><li>(a) transaction reads a table (a) </li></ul></ul><ul><ul><li>Another transaction (b) updates a row in table (a) </li></ul></ul><ul><ul><li>The update transaction (b) commits, the commit releases the rollback segment for another transaction to use. </li></ul></ul><ul><ul><li>Another transaction (c) uses that same rollback segment and from update transaction (b) and overwrites the rollback information from the update transaction (b) </li></ul></ul>
  41. 41. Snapshot Too Old <ul><ul><li>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 too old error to be returned. </li></ul></ul>
  42. 42. Snapshot Too Old <ul><li>Things that can affect occurance of snapshot too old. </li></ul><ul><ul><li>Rollback Segment Max Size </li></ul></ul><ul><ul><li>Rollback Segment Optimal Size </li></ul></ul><ul><ul><li>Number of Rollback Segments </li></ul></ul><ul><ul><li>Proper application transaction activity </li></ul></ul>
  43. 43. Rollback Segment Max Size <ul><li>The maximum size a rollback segment can grow too can have an impact on how long a particular change can be held in a rollback segment before being over wrote causing the snapshot too old error. </li></ul>
  44. 44. Rollback Segment Optimal Size <ul><li>The rollback segment can have what is called and optimal size. The optimal size is what a rollback segment 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 DML operation and the rollback segment must grow beyond optimal and then commits. The rollback information contained in the space beyond optimal can not be accessed if the rollback segment shrinks back to optimal. </li></ul>
  45. 45. Number of Rollback Segments <ul><li>The total number of rollback segments in the database can affect the snapshot too old by making it less likely that a rollback segment would be used by another transaction. </li></ul><ul><li>Note: Additional rollback segments dose not ensure that more rollback segments are used in lower transaction activity environments </li></ul>
  46. 46. Proper Application Transaction Activity <ul><li>98% of all snapshot too old errors are due to conflicting transactional activity, mostly in batch processing jobs. </li></ul><ul><li>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. </li></ul>
  47. 47. Proper Application Transaction Activity <ul><li>Keep Updates under the Rollback segment optimal if you can not consider speaking with a DBA about rollbacks with high optimal size. </li></ul><ul><li>Use special rollback segments for large DML jobs. </li></ul><ul><li>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 a larger optimal size. </li></ul>
  48. 48. Proper Application Transaction Activity <ul><li>Keep transactions as fast as possible. The faster activity completes will reduce the chances of updates that may cause snapshot too old. </li></ul>
  49. 49. CREDITS <ul><li>Oracle Metalink White Paper 70120 </li></ul><ul><li>Oracle 8.1.7 Documentation </li></ul><ul><li>My own knowledge and experience using Oracle </li></ul>
  50. 50. ?? QUESTIONS ??