Download presentation
Presentation is loading. Please wait.
1
Concurrency control using transactions 1Transactions
2
Transaction, the concept Logical unit of work on the database – Examples Transfer money from one bank account to another Reserve a seat on a flight Transaction boundaries are defined by the database user / application programmer – That’s us! 2Transactions
3
Interleaved processing versus parallel processing 3Transactions
4
Transaction operations Reading data – Read_item(X) Reads data X from the database 1.Find the address of the disk block containing X 2.Copy the disk block from hard disk to buffer in main memory 3.Copy data from the buffer to program variable X Writing data – Write_item(X) Writes data X to the database 1.Find the address of the disk block containing X 2.Copy the disk block from hard disk to buffer in main memory 3.Copy item X from program variable to buffer 4.Store the updated buffer on hard disk SELECT = Read_item(X) INSERT = Write_item(X) UPDATE = {Read_item(X)} + Write_item(X) 4Transactions
5
Example transactions a)Transferring N kroner from account X to account Y b)Updating account X, adding M kroner 5Transactions
6
Concurrency problems: Lost update + dirty read 6Transactions
7
Concurrency problems: Incorrect analysis 7Transactions
8
Concurrency problems: Unrepeatable reads A transaction reads the same data item twice, with different results! Example: Ticket reservation 1.The seat is free 2.After at little while, your decide to buy the ticket 3.But now the seat is not longer free Someone else bought it while you are ”thinking”. 8Transactions
9
Transaction states Transactions can end in two ways – COMMIT_TRANSACTION Successful end of transaction – ROLLBACK / ABORT Unsuccessful end of transaction 9Transactions
10
ACID properties of a transaction Atomicity – A transaction is an atomic unit of processing: It should either be performed in its entirety OR not performed at all. – Rollback means rollback the transaction as if it never happened. Consistency – A transaction takes the database from one consistent state to another Isolation – Transactions should be executed in isolation from other transactions Durability – The results of a committed transaction can never be undone Transactions10
11
Schedules of transactions Schedule, a definition – A schedule of a set of transactions is an ordering of the operations of the transactions Examples, fig. 20.3 – S a : r 1 (X); r 2 (X); w 1 (X);r 1 (Y); w 2 (X); w 1 (Y) T 1 reads X, T 2 reads X, T 1 writes X, etc. – S b : r 1 (X); w 1 (X); r 2 (X); w 2 (X); r 1 (Y); abort T 1 Serial schedule – No concurrency permitted T1 finishes before T2 starts, or vice versa – Not good: Too much waiting time (Conflict) serializable schedule – Schedule where the end result is (conflict) equal to the result of some serial schedule – This is OK. But how do we achieve it? Transactions11
12
Serial and non-serial schedules Transactions12
13
Testing for (conflict) serializability of a schedule A simple(?) way to test if a schedule is serializable – Make a precedence graph Nodes: Transactions Edges – T j does Read_item(X) AFTER T i does Write_item(X) » T i → T j – T j does Write_item(X) AFTER T i does Read_item(X) » T i → T j – T j does Write_item(X) AFTER T i does Write_item(X) » T i → T j – Schedule is serializable if the precedence graph has no cycles Transactions13
14
Example: Precedence graphs Transactions14
15
Transaction support in Microsoft SQL Server BEGIN TRANSACTION COMMIT TRANSACTION ROLLBACK TRANSACTION Documentation – http://msdn.microsoft.com/en- us/library/ms174377.aspx Transactions15
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.