database concurrency and transactions - tal olier
TRANSCRIPT
![Page 1: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/1.jpg)
Avishai Krepel & Tal Olier
Database for DevelopersSession 6: Transactions
![Page 2: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/2.jpg)
Agenda Preface
Database transactions and the ACID model
Locking - basics
Concurrency
Locking - advanced
![Page 3: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/3.jpg)
So many users, so little time… User A interacts with data X
User B interacts with data Y
User C interacts with data X and Y
Conflict?
Multi user applications has a parallel aspect…
![Page 4: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/4.jpg)
Ideal world Suggestion - each user working on it’s own copy of the data,
and sees other users’ (copy of the) data when required, updated to the correct time required.
Concerns: How do we decide who owns the data?
What is the correct time ?
![Page 5: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/5.jpg)
Ideal world (cont.) Throughput compromise! User A works on Sundays, Tuesdays and Thursdays User B works on Mondays, Wednesdays and Fridays User C will have to work Saturdays…
![Page 6: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/6.jpg)
What is a database transaction? Something we perform on our data?
Will it keeps my data safe?
How does it lets multiple users to do their work simultaneously?
From Babylon: “interaction between two parties, negotiation, settlement;
business deal” What is the contract?
![Page 7: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/7.jpg)
The ACID model A set of rules for “keep our data safe”
Acronym for Atomicity Consistency Isolation Durability
![Page 8: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/8.jpg)
ACID - Atomicity
“All in one proposition”
![Page 9: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/9.jpg)
ACID - Consistency Data’s state is kept consistent
Data structures kept correct
Failed transaction’s impact is canceled
![Page 10: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/10.jpg)
ACID - Isolation Transaction separation until finished Modification's separation Data visibility separation
![Page 11: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/11.jpg)
ACID - Durability Modification persistency
Crash recovery Data recovery Log implementation
![Page 12: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/12.jpg)
Locking (basics)
![Page 13: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/13.jpg)
Locking Resource regulation
Possible in several levels Object Page/Block Row
Conceptual types: Exclusive lock Share lock
![Page 14: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/14.jpg)
Exclusive lock Prevents sharing
Obtained to modify data
State assured until lock is removed
![Page 15: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/15.jpg)
Share lockAllows limited sharing Used by data readers to block writers Share likes “to share” with share
![Page 16: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/16.jpg)
• Data’s state is kept consistent
• Data structures kept correct
• Failed transaction’s impact is canceled …
… Consistency
![Page 17: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/17.jpg)
Read consistency Data changes according to time
Readers and writers conflicts
Default level Oracle: statement SQL Server: none
![Page 18: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/18.jpg)
Stmt level read consistency (Oracle)
SELECT statement starts at 08:00 (SCN 10023)
UPDATE statement update data in block #3 and block #5 at 09:00 (SCN 10024)
The SELECT gets to blocks #3 and #5 at 10:00 and reads them from the rollback segment (UNDO tablespace)
1 -2 -3 -4 -5 -6 -7 -
![Page 19: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/19.jpg)
Concurrency
![Page 20: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/20.jpg)
Concurrency side effects Dirty reads
Nonrepeatable (fuzzy) reads
Phantom reads (or phantoms)
![Page 21: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/21.jpg)
Isolation levels
![Page 22: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/22.jpg)
Isolation LevelsThe SQL standard defines four levels:
Read uncommitted
Read committed
Repeatable read
Serializable
![Page 23: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/23.jpg)
Isolation Levels - Read uncommitted The only rule is – “no rules”
One transaction may see uncommitted changes made by some other transaction
![Page 24: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/24.jpg)
Isolation Levels - Read committed The default isolation level
Query sees only data committed before (what?)
Good for conflicting transactions
![Page 25: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/25.jpg)
Isolation Levels - Repeatable read Read data can not be changed
The transaction require specific locks
The transaction will see new data committed
![Page 26: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/26.jpg)
Isolation Levels - Serializable Create the feeling that no other user changed
data
Implementations difference Oracle: The transaction sees only changes
committed before it started SQL Server: The transaction sees the changes as if
all transactions are ordered by time
Require extra developer work
![Page 27: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/27.jpg)
Read phenomena by isolation levels
![Page 28: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/28.jpg)
Isolation levels – Oracle and SQL Server Oracle offers the following
isolation levels Read committed Serializable isolation levels Read-only mode that is not
part of SQL92
Read committed is the default
SQL Server offers all 4 isolation levels: Read uncommitted Read committed Repeatable read Serializable
Read committed is the default
![Page 29: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/29.jpg)
Setting isolation level
Oracle SQL Server SET TRANSACTION ISOLATION
LEVEL READ COMMITTED; SET TRANSACTION ISOLATION
LEVEL SERIALIZABLE; SET TRANSACTION READ ONLY; ALTER SESSION SET
ISOLATION_LEVEL SERIALIZABLE;
ALTER SESSION SET ISOLATION_LEVEL READ COMMITTED;
SET TRANSACTIONISOLATION LEVEL READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE
![Page 30: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/30.jpg)
Back to locking (advanced…)
![Page 31: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/31.jpg)
Readers and writers
Oracle SQL Server
Data versioning based Readers do not block
writers Writers do not block
readers
Read-lock mechanism based Readers block writers Writers block readers
![Page 32: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/32.jpg)
Locking - OracleTypes of locks
![Page 33: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/33.jpg)
Oracle DML locks Assure integrity of data being accessed by multiple users
Both table and row locks are used
Partition is considered a table
![Page 34: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/34.jpg)
Row locks (Oracle DML locks)A transaction acquires an exclusive row lock for each individual
row modified by one of the following statements:
INSERT
UPDATE
DELETE
SELECT (only with the FORUPDATE clause)
![Page 35: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/35.jpg)
Table locks (Oracle DML locks)
LOCK TABLE <table name> IN EXCLUSIVE MODE
![Page 36: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/36.jpg)
Oracle manual (explicit ) data lockingPossible via one of the following:
The SET TRANSACTION ISOLATION LEVEL statement
The LOCK TABLE statement
The SELECT ... FOR UPDATE statement
Locks acquired by these statements are released after the transaction commits or rolls back
![Page 37: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/37.jpg)
Locking – SQL ServerResources that can be locked by SQL Server
![Page 38: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/38.jpg)
Locking explicitly – SQL Server Done via hints (proprietary syntax)
Example:select * from t1with (PAGLOCK ,HOLDLOCK)where c1 between 17 and 32
![Page 39: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/39.jpg)
Locking explicitly – SQL Server (cont.) Some more examples of hints NOLOCK/READUNCOMMITTED – dirty read PAGLOCK – takes page locks instead of row locks (statement
scoped) TABLOCK/X - takes page locks (share, exclusive) instead of
row locks (statement scoped) UPDLOCK/XLOCK – takes exclusive locks for read rows
(transaction scoped)
![Page 40: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/40.jpg)
Locking and transactions Transaction modes are managed at the session level
Connection is the object that encapsulates the session “state”
![Page 41: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/41.jpg)
Transaction management - syntax
Oracle SQL Server
BEGINupdate emp set salary = salary * 0.95;update emp set salary = salary * 0.95 where manager_flag = 1;
COMMIT;END;/
BEGIN TRANSACTION
update emp set salary = salary * 0.95;
update emp set salary = salary * 0.95 where manager_flag = 1;
COMMIT;
GO
![Page 42: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/42.jpg)
Transaction – possible endings Commit – makes changes visible
Rollback – cancel changes
commit/rollback Remove all locks Start a new implicit transaction
![Page 43: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/43.jpg)
Auto commit Every SQL statement is committed (upon completion)
The default mode for ADO, OLE DB, ODBC, DB-Library and Java
![Page 44: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/44.jpg)
SQL tools behavior Notice the auto-commit setting prior to updating records
SQL Server SQL Server management studio (SSMS) Every query window is a different connection
Oracle Oracle SQL developer (until release 2) All windows of the same connection share the same transaction (and
locks)
![Page 45: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/45.jpg)
Partial rollback
Oracle “savepoint”SQL Server “save transaction”
begin transaction trans1
insert into t1 values (1, 'one');
save transaction two
insert into 1 values (2, 'two');
rollback transaction two
commit
begin
insert into t1 values (1, 'one');
savepoint two;
insert into 1 values (2, 'two');
rollback to savepoint two;
commit;
end;
![Page 46: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/46.jpg)
Partial rollback (cont.)
After a partial rollback transaction do free locks held by roll backed statements
After a partial rollback transaction do not free locks held by roll backed statements
Oracle “savepoint”SQL Server “save transaction”
![Page 47: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/47.jpg)
Select for update (demo)
Oracle SQL Server
SELECT ID FROM EMP FOR UPDATE
SELECT ID FROM EMP WITH (UPD LOCK)
![Page 48: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/48.jpg)
Locking example1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
101 102 103 104 105
106 107 108 109 110
111 112 113 114 115
116 117 118 119 120
121 122 123 124 125
…
![Page 49: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/49.jpg)
Lock escalation Occurs when numerous locks are held at one level of
granularity
Database decides to raise some/all of the locks to a higher level
Tradeoff between number of locks and restrictiveness
![Page 50: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/50.jpg)
Lock escalation – Oracle
Oracle never escalate locks!
![Page 51: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/51.jpg)
Lock escalation – SQL Server Escalation thresholds: T-SQL level – amount of locked rows (~5000) Instance level – amount of memory occupied by locks (40%)
Thresholds can be changed SQL Sever instance ‘locks’ parameter
Escalation can fail because of other locking transactions
![Page 52: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/52.jpg)
Lock escalation – SQL Server (cont.)
Escalation success Escalation failure
The full table lock is acquired
All heap or B-tree, page (PAGE), or row-level (RID) locks held by the transaction on the heap or index are released
Full lock cannot be acquired
Database Engine will continue to acquire row, key, or page locks
Database Engine will retry the lock escalation for each additional ~1,250 locks acquired by the transaction
![Page 53: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/53.jpg)
Lock escalation – demo (SQL Server) Demo table
Update of x rows from the table
Check the locks on the table
![Page 54: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/54.jpg)
Lock escalation – disable At instance level DBCC TRACEON (1211,-1) – disable memory or # of locks
escalations DBCC TRACEON (1224,-1) – disable # locks escalations
At table level (SQL Server 2008 only) ALTER TABLE SET LOCK_ESCALATION = DISABLE
At session level DBCC TRACEON (1211) DBCC TRACEON (1224)
![Page 55: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/55.jpg)
Lock escalation – DEV pitfallsNote: Cases when multiple updates/deletes from various clients
happen on the same table and spanned across many rows Select with updlock (select for update) is considered as update Oracle developers do not know this behavior
![Page 56: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/56.jpg)
Deadlock (example)
![Page 57: Database concurrency and transactions - Tal Olier](https://reader038.vdocuments.mx/reader038/viewer/2022102901/555e0eb3d8b42a99188b4d92/html5/thumbnails/57.jpg)
This is it.