data concurrency
DESCRIPTION
Data Concurrency. David Konopnicki 1997 Revised by Mordo Shalom 2004. Agenda. Data concurrency, integrity and consistency The automatic locking mechanism in ORACLE Explicit (manual) locking ORACLE lock management services. What are concurrency and consistency. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/1.jpg)
11
Data ConcurrencyData Concurrency
David Konopnicki 1997David Konopnicki 1997
Revised by Mordo Shalom 2004Revised by Mordo Shalom 2004
![Page 2: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/2.jpg)
22
AgendaAgenda
Data concurrency, integrity and consistencyData concurrency, integrity and consistency The automatic locking mechanism in The automatic locking mechanism in
ORACLEORACLE Explicit (manual) lockingExplicit (manual) locking ORACLE lock management servicesORACLE lock management services
![Page 3: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/3.jpg)
33
What are concurrency and consistencyWhat are concurrency and consistency
In single user database, a user can modify In single user database, a user can modify data without concern of other users data without concern of other users modifying or accessing the same data.modifying or accessing the same data.
In multi-user database, several users In multi-user database, several users accessing the same data items is a problem.accessing the same data items is a problem.
Transaction: a set of user actions (e.g. SQL Transaction: a set of user actions (e.g. SQL queries) that must be executed together in queries) that must be executed together in some level of isolation. some level of isolation.
![Page 4: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/4.jpg)
44
Transactions in ORACLETransactions in ORACLE
Transactions are started automatically.Transactions are started automatically. A transaction is ended by:A transaction is ended by:
Commit (explicitly or by disconnect)Commit (explicitly or by disconnect) Rollback Rollback (explicity or by abort) (explicity or by abort) A DDL statement which begins a new transactionA DDL statement which begins a new transaction
A new transaction is started automatically with the A new transaction is started automatically with the next statement. next statement.
Autonomous transactions: Autonomous transactions: Transactions nested in timeTransactions nested in time The inner transaction may commit while the outer abortsThe inner transaction may commit while the outer aborts
![Page 5: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/5.jpg)
55
Error HandlingError Handling
Statement level rollback: Effects of an Statement level rollback: Effects of an erroneous statement are allways rolled back.erroneous statement are allways rolled back.
Resumable Errors (Out of Space, etc.), Resumable Errors (Out of Space, etc.), Resumable Statements .Resumable Statements .
Errors returned to the program may be Errors returned to the program may be handled by it, then the transaction continues, handled by it, then the transaction continues, otherwise it abortsotherwise it aborts
![Page 6: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/6.jpg)
66
Commit (Regular Transactions)Commit (Regular Transactions) Before Commit, changes were made to:Before Commit, changes were made to:
The database buffers The database buffers Rollback segmentsRollback segments Redo Log buffersRedo Log buffers
During Commit:During Commit: The SCN number of the transaction is written to the The SCN number of the transaction is written to the
rollback segments.rollback segments. Redo log buffers are writen to disc.Redo log buffers are writen to disc. The SCN is written to the redo log. (COMMIT)The SCN is written to the redo log. (COMMIT) Locks are releasedLocks are released
![Page 7: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/7.jpg)
77
Commit (Discrete Transactions)Commit (Discrete Transactions) Short, Nondistributed transactionsShort, Nondistributed transactions Declared as such by Declared as such by
BEGIN_DISCRETE_TRANSACTIONBEGIN_DISCRETE_TRANSACTION Before Commit, changes were made to:Before Commit, changes were made to:
Special Redo Log buffersSpecial Redo Log buffers During Commit:During Commit:
Redo log buffers are writen to disc.Redo log buffers are writen to disc. The SCN is written to the redo log. (COMMIT)The SCN is written to the redo log. (COMMIT) Database buffers are updatedDatabase buffers are updated Locks are releasedLocks are released
![Page 8: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/8.jpg)
88
Types of RollbackTypes of Rollback Statement levelStatement level Rollback to savepointRollback to savepoint Rollback on user requestRollback on user request Rollback because abnormal process Rollback because abnormal process
terminationtermination Multiple Rollbacks because abnormal instance Multiple Rollbacks because abnormal instance
terminationtermination Rollback of incomplete transactions during Rollback of incomplete transactions during
recovery.recovery.
![Page 9: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/9.jpg)
99
RollbackRollback Before Rollback, changes were made to:Before Rollback, changes were made to:
The database buffers The database buffers Rollback segmentsRollback segments Redo Log buffersRedo Log buffers
During Rollback:During Rollback: The rollback segments are applied to the database The rollback segments are applied to the database
buffers.buffers. Locks are releasedLocks are released
![Page 10: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/10.jpg)
1010
2PC – Two Phase Commit2PC – Two Phase Commit In distributed transactions.In distributed transactions. Transparent to user.Transparent to user. First phase: All the servers are put in pre-First phase: All the servers are put in pre-
commit status (some of them may abort).commit status (some of them may abort). Second phase: If all the servers are in pre-Second phase: If all the servers are in pre-
commit, all of them commit otherwise all of commit, all of them commit otherwise all of them roll back.them roll back.
RECO process resumes in-doubt 2PC’s RECO process resumes in-doubt 2PC’s (network failures etc..)(network failures etc..)
![Page 11: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/11.jpg)
1111
DefinitionsDefinitions
Data concurrency: Coordination access to Data concurrency: Coordination access to data by several users.data by several users.
Data consistency: A user sees a consistent Data consistency: A user sees a consistent view of the data i.e. all data committed by view of the data i.e. all data committed by other transactions as of that time and all the other transactions as of that time and all the changes made by the user up to that time.changes made by the user up to that time.
![Page 12: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/12.jpg)
1212
General concurrency issuesGeneral concurrency issues
Some of the problems:Some of the problems: Inconsistent reads (in one query)Inconsistent reads (in one query) Non-repeatable reads (phantoms)Non-repeatable reads (phantoms) Dirty readsDirty reads Lost updatesLost updates Destructive DDL operationsDestructive DDL operations
![Page 13: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/13.jpg)
1313
General locking conceptsGeneral locking concepts
Locks Locks are used to prevent destructive are used to prevent destructive interactions between users accessing the interactions between users accessing the same resources.same resources.
Resources Resources are:are: User objects (tables and rows)User objects (tables and rows) System objectsSystem objects
![Page 14: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/14.jpg)
1414
Restrictiveness of locksRestrictiveness of locks
Exclusive locks: prohibit the sharing of a Exclusive locks: prohibit the sharing of a resourceresource
Share locks: allow sharingShare locks: allow sharing
Share locks allow a higher degree of data Share locks allow a higher degree of data concurrency.concurrency.
![Page 15: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/15.jpg)
1515
DeadlocksDeadlocks
Transaction 1 Time Transaction 2Update EMPset sal = sal * 1.1whereempno=1000;
1 Update EMPset sal = sal * 1.1whereempno=2000;
Update EMPset sal=sal*1.1whereempno=2000;
2 Update EMPset sal = sal * 1.1whereempno=1000;
DEADLOCK 3 ERROR
![Page 16: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/16.jpg)
1616
Solving DeadlocksSolving Deadlocks
Local deadlocks are resolved using a wait-Local deadlocks are resolved using a wait-for graph.for graph.
In distributed ORACLE, timeout is used.In distributed ORACLE, timeout is used. Deadlock is solved by rolling-back a Deadlock is solved by rolling-back a
statement in one of the transactions statement in one of the transactions involved in the deadlock.involved in the deadlock.
![Page 17: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/17.jpg)
1717
Lock EscalationLock Escalation
Locks on rows -> locks on table.Locks on rows -> locks on table. Causes a lot of deadlocksCauses a lot of deadlocks Therefore: ORACLE never escalates locks.Therefore: ORACLE never escalates locks.
![Page 18: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/18.jpg)
1818
Lock conversionLock conversion
Re-request on an already locked item.Re-request on an already locked item. For a less restrictive lock: No problem.For a less restrictive lock: No problem. For a more restrictive lock: May wait.For a more restrictive lock: May wait.
![Page 19: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/19.jpg)
1919
Transactions and Data ConcurrencyTransactions and Data Concurrency
All locks acquired by statements within a All locks acquired by statements within a transaction are hold for the duration of the transaction are hold for the duration of the transaction.transaction.
That changes made by statements of a That changes made by statements of a transaction only become visible to other transaction only become visible to other transactions after the first one is committed.transactions after the first one is committed.
The locks acquired by a transaction are The locks acquired by a transaction are released when it is committed or rolled-back.released when it is committed or rolled-back.
![Page 20: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/20.jpg)
2020
Multi-version consistency modelMulti-version consistency model
ORACLE provides two levels of read-ORACLE provides two levels of read-consistency:consistency: Statement level read consistency that is always Statement level read consistency that is always
enforced.enforced. Transaction level read consistency.Transaction level read consistency.
read-only transaction.read-only transaction. set manually using exclusive locks.set manually using exclusive locks.
![Page 21: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/21.jpg)
2121
Rollback Segment (for read consistency)Rollback Segment (for read consistency)
Current SCNCurrent SCN
10231023
10241024
10231023
10241024
10231023
10241024
10231023
10231023
10231023
RollbackRollbackSegmentsSegments
![Page 22: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/22.jpg)
2222
ISO-SQL92 Isolation LevelsISO-SQL92 Isolation Levels
Isolation Level
Dirty Read
Nonrepeatable Read
Phantom Read
Read Uncommitted
Y Y Y
Read Committed
N Y Y
Repeatable Read
N N Y
Serializable N N N
![Page 23: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/23.jpg)
2323
Oracle 9i Isolation LevelsOracle 9i Isolation Levels
Isolation Level Nonrepeatable and Phantom Reads
Read Committed Y
Serializable/ Read Only
N
Serializable Transactions may getSerializable Transactions may get::
Can not serialize access … errorCan not serialize access … error..
![Page 24: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/24.jpg)
2424
How ORACLE locks dataHow ORACLE locks data
Readers do not wait for readers.Readers do not wait for readers. Writers do not wait for readers.Writers do not wait for readers. Writers only waits for writers if they Writers only waits for writers if they
attempt to update the same rows at the same attempt to update the same rows at the same time.time.
ORACLE automatically locks data at the ORACLE automatically locks data at the lowest level of restrictiveness.lowest level of restrictiveness.
![Page 25: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/25.jpg)
2525
Two types of locksTwo types of locks
DML: protects data in tables.DML: protects data in tables. Row locks.Row locks. Table locks.Table locks.
DDL: protects the schema.DDL: protects the schema.
![Page 26: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/26.jpg)
2626
Row locksRow locks
Inserted when a row is modified by Inserted when a row is modified by INSERT, UPDATE, DELETE and INSERT, UPDATE, DELETE and SELECT ... FOR UPDATE.SELECT ... FOR UPDATE.
Acquiring a row lock necessitates the Acquiring a row lock necessitates the acquisition of a table lock (DDL).acquisition of a table lock (DDL).
A row lock is alwaysA row lock is always exclusive ( exclusive (remember, remember, only writers wait for each other). only writers wait for each other).
![Page 27: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/27.jpg)
2727
Table locks - DMLTable locks - DML
DML statement Row locks? Mode of table lock
Select ... From ... No None
Insert ... Into ... Yes RX
Update Table ... Yes RX
Delete From Table ... Yes RX
Select From Table ... For Update of ...
Yes RS
![Page 28: Data Concurrency](https://reader036.vdocuments.net/reader036/viewer/2022081519/568141fd550346895daddb92/html5/thumbnails/28.jpg)
2828
Locks CompatibilityLocks Compatibility
RS RX S SRX X
RS Y Y Y Y N
RX Y Y N N N
S Y N Y N N
SRX Y N N N N
X N N N N N