Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases 11
Computer Science 202 Computer Science 202
Transaction Management and Transaction Management and
Concurrency Control Concurrency Control
2 2 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Objectives Objectives
To learn what a transaction is.
To learn what a transaction is.
To learn how an incomplete transaction can yield an To learn how an incomplete transaction can yield an inconsistent database.
inconsistent database.
To learn how an inconsistent database can be avoided To learn how an inconsistent database can be avoided through proper transaction management.
through proper transaction management.
To learn what concurrency control is.
To learn what concurrency control is.
To learn how concurrency control failure affects a To learn how concurrency control failure affects a database.
database.
To learn how concurrency control is accomplished.
To learn how concurrency control is accomplished.
To learn what database recovery management is.
To learn what database recovery management is.
To understand the use of transaction logs in recovery To understand the use of transaction logs in recovery management.
management.
3 3 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
What is a Transaction?
What is a Transaction?
A logical unit of work on a database A logical unit of work on a database
n
n
Read (select) and write (Update/insert/delete) Read (select) and write (Update/insert/delete) Database request is equivalent to a single SQL Database request is equivalent to a single SQL statement
statement
A transaction can consist of many DB Requests A transaction can consist of many DB Requests A transaction must be completed, or must be A transaction must be completed, or must be aborted
aborted
Consistency of a database is paramount Consistency of a database is paramount DB should be maintained in a consistent state DB should be maintained in a consistent state
4 4 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Example Transaction Example Transaction
5 5 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
So what's the problem?
So what's the problem?
To ensure consistency To ensure consistency
n
n
A Transaction must start with the DB in a A Transaction must start with the DB in a known state
known state
n
n
The transaction must complete and leave the The transaction must complete and leave the DB in a known state
DB in a known state
A transaction is likely to modify DB A transaction is likely to modify DB contents
contents
n
n
Protection must be given for the areas of the Protection must be given for the areas of the DB that the transaction works with.
DB that the transaction works with.
6 6 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Transaction Evaluation (I) Transaction Evaluation (I)
Examine current account balance Examine current account balance
Consistent state after transaction Consistent state after transaction No changes made to Database No changes made to Database SELECT ACC_NUM, ACC_BALANCE FROM CHECKACC
WHERE ACC_NUM = ‘0908110638’;
7 7 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Transaction Evaluation (II) Transaction Evaluation (II)
Register credit sale of 100 units of product X to customer Register credit sale of 100 units of product X to customer Y for R500
Y for R500
Consistent state only if both transactions are fully Consistent state only if both transactions are fully completed
completed
DBMS doesn’t guarantee that the semantic meaning of a DBMS doesn’t guarantee that the semantic meaning of a transaction represents real
transaction represents real- -world event world event UPDATE PRODUCT
SET PROD_QOH = PROD_QOH - 100 WHERE PROD_CODE = ‘X’;
UPDATE ACCT_RECEIVABLE
SET ACCT_BALANCE = ACCT_BALANCE + 500 WHERE ACCT_NUM = ‘Y’;
8 8 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Transaction Properties Transaction Properties
Atomicity Atomicity
n
n
All parts of must be completed OR none All parts of must be completed OR none
n
n
Indivisible Work unit Indivisible Work unit Durability
Durability
n
n
On completion DB is in a consistent state On completion DB is in a consistent state Serializability
Serializability
n
n
Concurrent execution of multiple transactions Concurrent execution of multiple transactions
n
n
Concurrency can mean that transactions are handled Concurrency can mean that transactions are handled in a serial manner
in a serial manner Isolation
Isolation
n
n
Data used in a transaction cannot be used by another Data used in a transaction cannot be used by another un till the first completes
un till the first completes
9 9 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Database Properties Database Properties
Single User Single User
n
n
Automatically ensures serializability and isolation, by Automatically ensures serializability and isolation, by design ( only single user)
design ( only single user)
n
n
Durability & Atomicity need to be provided by the Durability & Atomicity need to be provided by the DBMS
DBMS Multi User Multi User
n
n
Multiple concurrent transactions Multiple concurrent transactions
n
n
DBMS must enforce serializability and isolation DBMS must enforce serializability and isolation
n
n
Provide concurrency control management Provide concurrency control management
10 10 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
SQL Transaction Management SQL Transaction Management
Transaction support Transaction support
n
n
COMMIT COMMIT
n
n
ROLLBACK ROLLBACK
User initiated transaction sequence must User initiated transaction sequence must continue until:
continue until:
n
n
COMMIT statement is reached COMMIT statement is reached
n
n
ROLLBACK statement is reached ROLLBACK statement is reached
n
n
End of a program reached (COMMIT) End of a program reached (COMMIT)
n
n
Abnormal program termination ( Abnormal program termination (ROLLBACK) ROLLBACK)
11 11 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
SQL Example SQL Example
BEGIN TRANSACTION; --Not ALL support THIS UPDATE PRODUCT
SET PROD_QOH = PROD_QOH - 100 WHERE PROD_CODE = ‘X’;
UPDATE ACCT_RECEIVABLE
SET ACCT_BALANCE = ACCT_BALANCE + 500 WHERE ACCT_NUM = ‘Y’;
COMMIT;
12 12 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Transaction Log Transaction Log
Transaction log keeps track of all updates Transaction log keeps track of all updates affecting the database
affecting the database
Log is maintained by the DBMS Log is maintained by the DBMS Used for DB recovery in the event of a Used for DB recovery in the event of a ROLLBACK
ROLLBACK
Can be used by some DBMS for recovery Can be used by some DBMS for recovery following a crash
following a crash
Used to help ensure Durability of Used to help ensure Durability of Transactions
Transactions
13 13 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Transaction Log Transaction Log
Transaction log holds information to allow Transaction log holds information to allow a recovery
a recovery
14 14 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Concurrency Control Concurrency Control
Ensures serializability of transactions Ensures serializability of transactions Protects against problems that could arise Protects against problems that could arise from simultaneous execution
from simultaneous execution
n
n
Data Integrity Data Integrity
n
n
Consistency Consistency Handles Handles
n
n
Lost Updates Lost Updates
n
n
Uncommitted Data Uncommitted Data
n
n
Inconsistent Data Retrievals Inconsistent Data Retrievals
15 15 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Concurrency Issues Concurrency Issues
Lost Updates Lost Updates
n
n
An update is lost and the result is Erroneous data An update is lost and the result is Erroneous data being retrieved
being retrieved Uncommitted Data Uncommitted Data
n
n
Two concurrent transactions Two concurrent transactions
n
n
T1 Rolls back after T2 Has already accessed updated T1 Rolls back after T2 Has already accessed updated information
information
Inconsistent Retrievals Inconsistent Retrievals
n
n
Usually with Aggregation functions Usually with Aggregation functions
n
n
T2 performs an aggregation while T1 is Updating T2 performs an aggregation while T1 is Updating values
values
16 16 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Lost Updates Lost Updates
Normal Transaction Normal Transaction
17 17 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Lost Updates Lost Updates
Lost Update results in Error Lost Update results in Error
18 18 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Uncommitted Data Uncommitted Data
Correct Processing
Correct Processing
19 19 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Uncommitted Data Uncommitted Data
Problems resulting from Uncommitted Data Problems resulting from Uncommitted Data
20 20 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Inconsistent Data Retrieval Inconsistent Data Retrieval
Retrieval during an Update Retrieval during an Update
21 21 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Inconsistent Data Retrieval Inconsistent Data Retrieval
Resultant Output Resultant Output
22 22 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
The Scheduler The Scheduler
Scheduler provides overall management of the Scheduler provides overall management of the concurrency process
concurrency process Ordering of transactions
Ordering of transactions à à serializability serializability Ordering based on Control Algorithms Ordering based on Control Algorithms
n n
Locking Locking
n
n
Time stamping Time stamping
Why Not use FIFO ordering ? Why Not use FIFO ordering ?
n
n
NOT effective use of CPU NOT effective use of CPU
n
n
Poor Response times Poor Response times
23 23 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Conflicting Operations Conflicting Operations
24 24 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Locking Methods Locking Methods
A Lock guarantees exclusive access to a A Lock guarantees exclusive access to a DB object
DB object
Prevents access to inconsistent Data Prevents access to inconsistent Data Automatically managed by DBMS Automatically managed by DBMS A
A Lock Manager Lock Manager process is responsible for process is responsible for all Locking within the DB
all Locking within the DB
Lock Granularity can have a big effect on Lock Granularity can have a big effect on performance
performance
25 25 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Locking granularity Locking granularity
The Level at which the locks are applied The Level at which the locks are applied
n
n
Database Level Database Level
n
n
Table Level Table Level
n
n
Page Level Page Level
n
n
Row Level Row Level
n
n
Field Level Field Level
Locks can be of two types Locks can be of two types
n n
Binary Binary
n
n
Shared/Exclusive Shared/Exclusive
26 26 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Database Level Database Level
Entire Database is locked Entire Database is locked Prevents use of
Prevents use of any any other tables by other tables by transactions T2, T3…. until T1 has transactions T2, T3…. until T1 has completed
completed Very Slow
Very Slow performance performance
27 27 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Database Level Database Level
28 28 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Table Level Table Level
Locks are applied to specific tables Locks are applied to specific tables T2 can access tables within the DB T2 can access tables within the DB provided that they are not locked by T1 provided that they are not locked by T1 Better performance than DB Level locks Better performance than DB Level locks Can still be problematic with multiple Can still be problematic with multiple accesses to a Table
accesses to a Table
Slowdown even if T2 and T1 are Slowdown even if T2 and T1 are accessing different portions of a table.
accessing different portions of a table.
29 29 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Table Level Table Level
30 30 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Page Level Page Level
Locks are specific to a physical disk page Locks are specific to a physical disk page Disk page is equivalent to a storage block Disk page is equivalent to a storage block on disk
on disk
Size of page is dependant on underlying Size of page is dependant on underlying HW, OS and File system
HW, OS and File system
n
n
4K , 8K , 16K , 32K 4K , 8K , 16K , 32K
T2 cannot access a page locked by T1
T2 cannot access a page locked by T1
T2 CAN access the same table if the
T2 CAN access the same table if the
portion needed is in a different disk page
portion needed is in a different disk page
31 31 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Page Level Page Level
32 32 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Row Level Row Level
Non restrictive locking mechanism Non restrictive locking mechanism
Locks are restricted to the particular ROW Locks are restricted to the particular ROW that T1, may be using. T2 May access the that T1, may be using. T2 May access the rest of the DB.
rest of the DB.
Higher granularity results in increased Higher granularity results in increased overhead
overhead
33 33 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Row Level Row Level
34 34 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Field Level Field Level
Allows Transactions to access the same Allows Transactions to access the same row
row
Provides locking on fields within the row Provides locking on fields within the row Extremely high overhead
Extremely high overhead
35 35 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Binary Locks Binary Locks
Only has two states Only has two states
n n
Locked Locked
n
n
Unlocked Unlocked
Applies to all levels of granularity Applies to all levels of granularity DBMS manages the locking process DBMS manages the locking process Binary locks are generally exclusive Binary locks are generally exclusive
36 36 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Binary Locks
Binary Locks
37 37 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Shared/Exclusive Locks Shared/Exclusive Locks
Exclusive Locks Exclusive Locks
n
n
Only the locking transaction has access Only the locking transaction has access
n
n
Used for write operations Used for write operations Shared Locks
Shared Locks
n
n
Concurrent transactions may have READ access to a Concurrent transactions may have READ access to a locked object
locked object
n
n
No conflict provided all transactions are READ No conflict provided all transactions are READ
n
n
Cannot be issued if an exclusive lock for the object Cannot be issued if an exclusive lock for the object exists
exists
38 38 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Shared/Exclusive Locks Shared/Exclusive Locks
Locking states Locking states
n
n
READ_LOCK READ_LOCK
n
n
WRITE_LOCK WRITE_LOCK
n
n
UNLOCK UNLOCK Locking problems Locking problems
n
n
Transactions may not be serializable Transactions may not be serializable Can be solved with TWO
Can be solved with TWO- -PHASE locking PHASE locking
n
n
Scheduler may create deadlocks Scheduler may create deadlocks
39 39 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Two
Two- - Phase Locking Phase Locking
Growing phase Growing phase Shrinking phase Shrinking phase Governing rules Governing rules
n
n
Two transactions cannot have conflicting Two transactions cannot have conflicting locks
locks
n
n
No unlock operation can precede a lock No unlock operation can precede a lock operation in the same transaction operation in the same transaction
n
n
No data objects are updated until all locks are No data objects are updated until all locks are obtained
obtained
40 40 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Two
Two- -Phase Locking Phase Locking
41 41 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Deadlocks Deadlocks
Occurs when two transactions are waiting Occurs when two transactions are waiting for the other to unlock data
for the other to unlock data T1 = Accesses DATA X & DATA Y T1 = Accesses DATA X & DATA Y T2 = Accesses DATA Y & DATA X T2 = Accesses DATA Y & DATA X Also known as a ‘
Also known as a ‘deadly embrace deadly embrace’ ’
42 42 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Deadlock Example
Deadlock Example
43 43 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Deadlock Control Deadlock Control
Deadlock Prevention Deadlock Prevention
n
n
New locks are aborted if there is a likelihood they New locks are aborted if there is a likelihood they would cause a deadlock
would cause a deadlock
n
n
Transaction re Transaction re- -scheduled scheduled Deadlock Detection Deadlock Detection
n
n
DBMS checks DB for deadlocks DBMS checks DB for deadlocks
n
n
In the case of a deadlock one transaction is aborted In the case of a deadlock one transaction is aborted and then restarted
and then restarted Deadlock Avoidance Deadlock Avoidance
n
n
Transaction must obtain ALL locks it needs prior to Transaction must obtain ALL locks it needs prior to execution (two
execution (two - -phase locking) phase locking)
n
n
Much slower due to serialization of transactions Much slower due to serialization of transactions
44 44 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Time stamping Time stamping
Timestamps can be used to assist with Timestamps can be used to assist with scheduling algorithms
scheduling algorithms
Each transaction is assigned a unique stamp Each transaction is assigned a unique stamp based on the order of submission to DBMS based on the order of submission to DBMS R/W operations can take place provided they R/W operations can take place provided they have the same timestamp
have the same timestamp
Transactions are serialized based on timestamp Transactions are serialized based on timestamp order
order
Disadvantages Disadvantages
n
n
Increased admin Increased admin
n
n
Increased memory (Last read & Last Update) Increased memory (Last read & Last Update)
45 45 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Optimistic Methods Optimistic Methods
Assumption that most transactions to do Assumption that most transactions to do not conflict
not conflict
Transaction executed without restrictions Transaction executed without restrictions until committed
until committed Phases:
Phases:
n
n
Read Phase Read Phase
n
n
Validation Phase Validation Phase
n
n
Write Phase Write Phase
46 46 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Database Recovery Management Database Recovery Management
Restores a database from an inconsistent Restores a database from an inconsistent to a previously consistent state
to a previously consistent state
Based on the atomic transaction property Based on the atomic transaction property Level of backup
Level of backup
n
n
Full backup Full backup
n
n
Differential Differential
n
n
Transaction log Transaction log
47 47 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Database Failures Database Failures
Software Software
n
n
Viruses, Operating System, DBMS Viruses, Operating System, DBMS Hardware
Hardware
n
n
Hard disks, Memory, Networks Hard disks, Memory, Networks Programming Exemption Programming Exemption
n
n
Abnormal termination, premature termination Abnormal termination, premature termination Transaction
Transaction
n
n
Deadlock results in termination of a transaction Deadlock results in termination of a transaction External
External
n
n
Fire, Flood, Theft Fire, Flood, Theft
48 48 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Transaction Recovery Transaction Recovery
Write
Write- -Ahead Logging Ahead Logging
n
n
Logs written BEFORE data is modified Logs written BEFORE data is modified Redundant transaction logs Redundant transaction logs
n
n
Multiple copies of transaction log maintained Multiple copies of transaction log maintained Buffers
Buffers
n
n
Memory buffers lead to faster processing Memory buffers lead to faster processing Checkpoints
Checkpoints
n
n
Updated buffers committed to disk Updated buffers committed to disk
n
n
Registered in Transaction log Registered in Transaction log
n
n
Buffers and Physical copies synchronised Buffers and Physical copies synchronised
49 49 Barry Irwin August 2003
Barry Irwin August 2003 Rhodes University CS202 : DatabasesRhodes University CS202 : Databases
Transaction Recovery Transaction Recovery
Deferred
Deferred- -write and Deferred write and Deferred- -update update
n
n
Changes are written to the transaction log Changes are written to the transaction log
n
n
Database updated after transaction reaches commit Database updated after transaction reaches commit point
point Write Write- -through through
n
n
Immediately updated by during execution Immediately updated by during execution
n
n
Before the transaction reaches its commit point Before the transaction reaches its commit point
n
n
Transaction log also updated Transaction log also updated
n
n
Transaction fails, database uses log information Transaction fails, database uses log information to ROLLBACK
to ROLLBACK