©Bob Godfrey, 2002, 2005 Lecture 17: Transaction Integrity and Concurrency BSA206 Database Management Systems
©Bob Godfrey, 2002, 2005 TRANSACTION A discrete unit of work which must be completely processed or not processed at all
Lecture 17 / Slide 3 Transaction Management Transactions represent real world events eg enter a customer order In a database if you read and/or write you make use of a transaction For a transaction to occur a database must be in a consistent state
Lecture 17 / Slide 4 Transaction Management in SQL Transaction support is provided through the use of 2 SQL statements: –COMMIT –ROLLBACK Terminating a transaction: –a COMMIT or ROLLBACK is performed –a DDL command is performed –the user disconnects –the user process is abnormally terminated
Lecture 17 / Slide 5 Transaction Properties Atomicity requires that all parts of a transaction must be completed and if not the transaction is aborted Durability indicates the permanence of the database’s consistent state Serialisability describes the result of the concurrent execution of several transactions Isolation means that the data used during the execution of a transaction cannot be used by a second transaction, until the first one is completed
Lecture 17 / Slide 6 Transaction Boundaries BEGIN TRANSACTION Action 1 Action 2 etc. COMMIT ROLLBACK
Lecture 17 / Slide 7 Lost Update Transaction ATransaction B Read a/c X balance $150 Increase balance by $50 Decrease balance by $100 Write a/c X with balance $200 Write a/c X with balance $50 * A/c X should be $100 ($150 + $50 - $100 + $100, not $50)
Lecture 17 / Slide 8 Uncommitted Dependency Transaction ATransaction B Read a/c X balance $150 Increase balance by $50 Write a/c X with balance $200 Read a/c X balance $200 Rollback transaction (balance $150)
Lecture 17 / Slide 9 Inconsistent Analysis Three A/Cs with balances of X(150), Y(350) and Z(500) total = $1000 Transaction ATransaction B Read a/c X, add balance of $150 to total = $150 Read a/c X balance $150 Decrease balance by $100 Write a/c X with balance $50 Read a/c Y balance $350 Increase balance by $100 Write a/c X with balance $450 Read a/c Y, add balance of $450 to total = $600 Read a/c Z, add balance of $500 to total = $1100 Print report total of $1100 !!! (Three A/Cs with balances of X(50), Y(450) and Z(500) total = $1000)
Lecture 17 / Slide 10 Record Locking (Pessimistic Control) A concurrency processing procedure that ensures that only one user at a time can access or alter a record that is currently accessed. A lock guarantees exclusive use of a data item to a current transaction. A transaction acquires a lock at access and it will be unlocked when the transaction is completed.
Lecture 17 / Slide 11 Optimistic Concurrency Control Based on the assumption that the majority of database operations do not conflict. Each transaction is executed, with no restrictions, until it is committed. Each transaction moves through phases: read validation write
Lecture 17 / Slide 12 Lock Granularity Indicates the level of lock use, which can take place at the database, table, record, and field levels. Locking database or table means only one end user can view or use at a time Record locking may mean that access to popular items are constantly delayed Field locking permits more than one person to view but only one to change
Lecture 17 / Slide 13 Locking & SQL LOCK TABLE tablename IN lockmode MODE [NOWAIT] Lockmode is one of: exclusive share row share/share update row exclusive share row exclusive NOWAIT If this option is specified then the user is not kept waiting for the lock to be applied
Lecture 17 / Slide 14 Deadlock Transaction ATransaction B Read a/c X balance $150 and place shared(read) lock Read a/c X balance $150 and place shared(read) lock Increase balance by $50 Decrease balance by $100 Write a/c X with balance $200 (denied X-lock so waits) Write a/c X with balance $250 (denied X-lock so waits)
Lecture 17 / Slide 15 Controlling Deadlocks Deadlock Prevention –a transaction requesting a new lock is aborted if there is a possibility that a deadlock may occur Deadlock Detection –the DBMS periodically checks for deadlock and if found one of the transactions is aborted and rolled back
Lecture 17 / Slide 16 Controlling Deadlock Two-phase Locking –all locks required for a transaction are acquired before any locks are released