Download presentation
Presentation is loading. Please wait.
Published byHelena Foster Modified over 9 years ago
1
1 Data Concurrency David Konopnicki 1997 Revised by Mordo Shalom 2004
2
2 Agenda ä Data concurrency, integrity and consistency ä The automatic locking mechanism in ORACLE ä Explicit (manual) locking ä ORACLE lock management services
3
3 What are concurrency and consistency ä In single user database, a user can modify data without concern of other users modifying or accessing the same data. ä In multi-user database, several users accessing the same data items is a problem. ä Transaction: a set of user actions (e.g. SQL queries) that must be executed together in some level of isolation.
4
4 Transactions in ORACLE ä Transactions are started automatically. ä A transaction is ended by: ä Commit (explicitly or by disconnect) ä Rollback (explicity or by abort) ä A DDL statement which begins a new transaction ä A new transaction is started automatically with the next statement. ä Autonomous transactions: ä Transactions nested in time ä The inner transaction may commit while the outer aborts
5
5 Error Handling ä Statement level rollback: Effects of an erroneous statement are allways rolled back. ä Resumable Errors (Out of Space, etc.), Resumable Statements. ä Errors returned to the program may be handled by it, then the transaction continues, otherwise it aborts
6
6 Commit (Regular Transactions) ä Before Commit, changes were made to: ä The database buffers ä Rollback segments ä Redo Log buffers ä During Commit: ä The SCN number of the transaction is written to the rollback segments. ä Redo log buffers are writen to disc. ä The SCN is written to the redo log. (COMMIT) ä Locks are released
7
7 Commit (Discrete Transactions) ä Short, Nondistributed transactions ä Declared as such by BEGIN_DISCRETE_TRANSACTION ä Before Commit, changes were made to: ä Special Redo Log buffers ä During Commit: ä Redo log buffers are writen to disc. ä The SCN is written to the redo log. (COMMIT) ä Database buffers are updated ä Locks are released
8
8 Types of Rollback ä Statement level ä Rollback to savepoint ä Rollback on user request ä Rollback because abnormal process termination ä Multiple Rollbacks because abnormal instance termination ä Rollback of incomplete transactions during recovery.
9
9 Rollback ä Before Rollback, changes were made to: ä The database buffers ä Rollback segments ä Redo Log buffers ä During Rollback: ä The rollback segments are applied to the database buffers. ä Locks are released
10
10 2PC – Two Phase Commit ä In distributed transactions. ä Transparent to user. ä First phase: All the servers are put in pre- commit status (some of them may abort). ä Second phase: If all the servers are in pre- commit, all of them commit otherwise all of them roll back. ä RECO process resumes in-doubt 2PC’s (network failures etc..)
11
11 Definitions ä Data concurrency: Coordination access to data by several users. ä Data consistency: A user sees a consistent view of the data i.e. all data committed by other transactions as of that time and all the changes made by the user up to that time.
12
12 General concurrency issues ä Some of the problems: ä Inconsistent reads (in one query) ä Non-repeatable reads (phantoms) ä Dirty reads ä Lost updates ä Destructive DDL operations
13
13 General locking concepts ä Locks are used to prevent destructive interactions between users accessing the same resources. ä Resources are: ä User objects (tables and rows) ä System objects
14
14 Restrictiveness of locks ä Exclusive locks: prohibit the sharing of a resource ä Share locks: allow sharing Share locks allow a higher degree of data concurrency.
15
15 Deadlocks
16
16 Solving Deadlocks ä Local deadlocks are resolved using a wait- for graph. ä In distributed ORACLE, timeout is used. ä Deadlock is solved by rolling-back a statement in one of the transactions involved in the deadlock.
17
17 Lock Escalation ä Locks on rows -> locks on table. ä Causes a lot of deadlocks ä Therefore: ORACLE never escalates locks.
18
18 Lock conversion ä Re-request on an already locked item. ä For a less restrictive lock: No problem. ä For a more restrictive lock: May wait.
19
19 Transactions and Data Concurrency ä All locks acquired by statements within a transaction are hold for the duration of the transaction. ä That changes made by statements of a transaction only become visible to other transactions after the first one is committed. ä The locks acquired by a transaction are released when it is committed or rolled- back.
20
20 Multi-version consistency model ä ORACLE provides two levels of read- consistency: ä Statement level read consistency that is always enforced. ä Transaction level read consistency. ä read-only transaction. ä set manually using exclusive locks.
21
21 Rollback Segment (for read consistency) Current SCN 1023 1024 1023 1024 1023 1024 1023 1023 1023 RollbackSegments
22
22 ISO-SQL92 Isolation Levels
23
23 Oracle 9i Isolation Levels Serializable Transactions may get: Can not serialize access … error.
24
24 How ORACLE locks data ä Readers do not wait for readers. ä Writers do not wait for readers. ä Writers only waits for writers if they attempt to update the same rows at the same time. ä ORACLE automatically locks data at the lowest level of restrictiveness.
25
25 Two types of locks ä DML: protects data in tables. ä Row locks. ä Table locks. ä DDL: protects the schema.
26
26 Row locks ä Inserted when a row is modified by INSERT, UPDATE, DELETE and SELECT... FOR UPDATE. ä Acquiring a row lock necessitates the acquisition of a table lock (DDL). ä A row lock is always exclusive (remember, only writers wait for each other).
27
27 Table locks - DML
28
28 Locks Compatibility
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.