Chapter 9
Transaction Management and
Concurrency Control
What is a Transaction?
Any action that reads from and/or writes
to a database may consist of
Simple SELECT statement to generate a list of
table contents
A series of related UPDATE statements to
change the values of attributes in various
tables
A series of INSERT statements to add rows to
one or more tables
A combination of SELECT, UPDATE, and
INSERT statements
Database Systems 6e/ Rob & Coronel Slide 2
What is a Transaction?
A logical unit of work that must be either
entirely completed or aborted
Successful transaction changes the database
from one consistent state to another
One in which all data integrity constraints are
satisfied
Most real-world database transactions are
formed by two or more database requests
The equivalent of a single SQL statement in an
application program or transaction
Database Systems 6e/ Rob & Coronel Slide 3
The Relational Schema
for the Ch09_SaleCo Database
Database Systems 6e/ Rob & Coronel Slide 4
Evaluating Transaction Results
Not all transactions update the database
SQL code represents a transaction because
database was accessed
Improper or incomplete transactions can have
a devastating effect on database integrity
Some DBMSs provide means by which user can
define enforceable constraints based on
business rules
Other integrity rules are enforced automatically
by the DBMS when table structures are properly
defined, thereby letting the DBMS validate some
transactions
Database Systems 6e/ Rob & Coronel Slide 5
Transaction Processing
For example, a transaction may involve
The creation of a new invoice
Insertion of an row in the LINE table
Decreasing the quantity on hand by 1
Updating the customer balance
Creating a new account transaction row
If the system fails between the first and
last step, the database will no longer be
in a consistent state Figure 9.2
Database Systems 6e/ Rob & Coronel Slide 6
Transaction Properties
Atomicity
Requires that all operations (SQL requests) of
a transaction be completed; if not, then the
transaction is aborted
A transaction is treated as a single, indivisible,
logical unit of work
Durability
Indicates permanence of database’s consistent
state
When a transaction is complete, the database
reaches a consistent state. That state can not
be lost even if the system fails
Database Systems 6e/ Rob & Coronel Slide 7
Transaction Properties
Serializability
Ensures that the concurrent execution of
several transactions yields consistent results
Multiple, concurrent transactions appear as if
they executed in serial order (one after
another)
Isolation
Data used during execution of a transaction
cannot be used by second transaction until first
one is completed
Database Systems 6e/ Rob & Coronel Slide 8
Transaction Properties
Consistency
Indicates the permanence of database’s
consistent state
Database Systems 6e/ Rob & Coronel Slide 9
Transaction Management with SQL
ANSI has defined standards that govern
SQL database transactions
Transaction support is provided by two SQL
statements: COMMIT and ROLLBACK
ANSI standards require that, when a
transaction sequence is initiated by a user
or an application program,it must continue
through all succeeding SQL statements until
one of four events occurs
Database Systems 6e/ Rob & Coronel Slide 10
Transaction Management with SQL
1. A COMMIT statement is reached- all changes
are permanently recorded within the database
2. A ROLLBACK is reached – all changes are
aborted and the database is restored to a
previous consistent state
3. The end of the program is successfully
reached – equivalent to a COMMIT
4. The program abnormally terminates and a
rollback occurs
Database Systems 6e/ Rob & Coronel Slide 11
Transaction States
Active – the initial state; the transaction stays in this state
while it is executing
Partially committed – after the final statement has been
executed.
Failed – after the discovery that normal execution can no
longer proceed.
Aborted – after the transaction has been rolled back and the
database restored to its state prior to the start of the
transaction. Two options after it has been aborted:
restart the transaction
can be done only if no internal logical error
kill the transaction
Committed – after successful completion.
To guarantee atomicity, external observable action should all
be performed (in order) after the transaction is committed.
Database Systems 6e/ Rob & Coronel Slide 12
States Transition Diagram
Partially
committed committed
active
failed aborted
Database Systems 6e/ Rob & Coronel Slide 13
The Transaction Log
Keeps track of all transactions that updatethe
database. It contains:
A record for the beginning of transaction
For each transaction component (SQL statement)
Type of operation being performed (update, delete,
insert)
Names of objects affected by the transaction (the name
of the table)
“Before” and “after” values for updated fields
Pointers to previous and next transaction log entries for
the same transaction
The ending (COMMIT) of the transaction
Increases processing overhead but the ability to restore a
corrupted database is worth the price
Database Systems 6e/ Rob & Coronel Slide 14
The Transaction Log
Increases processing overhead but the ability to
restore a corrupted database is worth the price
If a system failure occurs, the DBMS will examine
the log for all uncommitted or incomplete
transactions and it will restore the database to a
previous state
The log it itself a database and to maintain its
integrity many DBMSs will implement it on several
different disks to reduce the risk of system failure
Database Systems 6e/ Rob & Coronel Slide 15
A Transaction Log
Database Systems 6e/ Rob & Coronel Slide 16
Concurrency Control
The coordination of the simultaneous execution of
transactions in a multiprocessing database is
known as concurrency control
The objective of concurrency control is to ensure
the serializability of transactions in a multiuser
database environment
Database Systems 6e/ Rob & Coronel Slide 17
Concurrency Control
Important simultaneous execution of
transactions over a shared database can
create several data integrity and consistency
problems
The three main problems are:
lost updates
uncommitted data
inconsistent retrievals
Database Systems 6e/ Rob & Coronel Slide 18
Normal Execution of Two Transactions
Database Systems 6e/ Rob & Coronel Slide 19
Lost Updates
Database Systems 6e/ Rob & Coronel Slide 20
Uncommited Data Problem
Uncommitted data occurs when two
transactions execute concurrently and
the first is rolled back after the second
has already accessed the uncommitted
data
This violates the isolation property of
transactions
Database Systems 6e/ Rob & Coronel Slide 21
Correct Execution of Two Transactions
Database Systems 6e/ Rob & Coronel Slide 22
An Uncommitted Data Problem
Database Systems 6e/ Rob & Coronel Slide 23
Inconsistent Retrieval Problem
Occur when a transaction calculates some
aggregate functions over a set of data while
transactions are updating the data
Some data may be read after they are changed and
some before they are changed yielding inconsistent
results
Database Systems 6e/ Rob & Coronel Slide 24
Retrieval During Update
Database Systems 6e/ Rob & Coronel Slide 25
Transaction Results: Data Entry Correction
Database Systems 6e/ Rob & Coronel Slide 26
Inconsistent Retrievals
Database Systems 6e/ Rob & Coronel Slide 27
The Scheduler
Special DBMS program: establishes
order of operations within which
concurrent transactions are executed
Interleaves the execution of database
operations to ensure serializability and
isolation of transactions
To determine the appropriate order, the
scheduler bases its actions on
concurrency control algorithms such as
locking and time stamping
Database Systems 6e/ Rob & Coronel Slide 28
The Scheduler (continued)
Ensures computer’s central processing
unit (CPU) is used efficiently
Default would be FIFO without preemption
– idle CPU (during I/O) is inefficient use of
the resource and result in unacceptable
response times in within the multiuser
DBMS environment
Facilitates data isolation to ensure that
two transactions do not update the
same data element at the same time
Database Systems 6e/ Rob & Coronel Slide 29
Read/Write Conflict Scenarios:
Conflicting Database Operations Matrix
Transactions T1 and T2 are executing concurrently over the
same data
When they both access the data and at least one is executing a
WRITE, a conflict can occur
Database Systems 6e/ Rob & Coronel Slide 30
Concurrency Control with Locking Methods
Lock
Guarantees exclusive use of a data item to a
current transaction
T2 does not have access to a data item that is
currently being used by T1
Transaction acquires a lock prior to data access; the
lock is released when the transaction is complete
Required to prevent another transaction from
reading inconsistent data
Lock manager
Responsible for assigning and policing the locks
used by the transactions
Database Systems 6e/ Rob & Coronel Slide 31
Lock Granularity
Indicates the level of lock use
Locking can take place at the following levels:
Database-level lock
Entire database is locked
Table-level lock
Entire table is locked
Page-level lock
Entire diskpage is locked
Row-level lock
Allows concurrent transactions to access different rows
of the same table, even if the rows are located on the
same page
Field-level lock
Allows concurrent transactions to access the same row,
as long as they require the use of different fields
(attributes) within that row
Database Systems 6e/ Rob & Coronel Slide 32
A Database-Level Locking Sequence
Good for batch processing but unsuitable for online multi-user
DBMSs
T1 and T2 can not access the same database concurrently even if
they use different tables
Database Systems 6e/ Rob & Coronel Slide 33
Table-Level Lock
T1 and T2 can access the same database concurrently as long as they use different tables
Can cause bottlenecks when many transactions are trying to access the same table (even if the
transactions want to access different parts of the table and would not interfere with each other)
Not suitable for multi-user DBMSs
Database Systems 6e/ Rob & Coronel Slide 34
Page-Level Lock
An entire disk page is locked (a table can span several pages and
each page can contain several rows of one or more tables)
Most frequently used multi-user DBMS locking method
Database Systems 6e/ Rob & Coronel Slide 35
Row-Level Lock
Concurrent transactions can access different rows of the same
table even if the rows are located on the same page
Improves data availability but with high overhead (each row has
a lock that must be read and written to)
Database Systems 6e/ Rob & Coronel Slide 36
Field-Level Lock
Allows concurrent transactions to
access the same row as long as they
require the use of different fields within
that row
Most flexible lock but requires an
extremely high level of overhead
Database Systems 6e/ Rob & Coronel Slide 37
Binary Locks
Has only two states: locked (1) or unlocked (0)
Eliminates “Lost Update” problem – the lock is not released until
the write statement is completed
Can not use PROD_QOH until it has been properly updated
Considered too restrictive to yield optimal concurrency conditions as it
locks even for two READs when no update is being done
Database Systems 6e/ Rob & Coronel Slide 38
Shared/Exclusive Locks
Exclusive lock
Access is specifically reserved for the transaction that locked the
object
Used when potential for conflict exists – when a transaction wants to
update a data item and no other transaction currently hold locks on
that data item
Granted if and only if no other locks are held on the data item
Shared lock
Concurrent transactions are granted Read access on the basis of a
common lock
Issued when a transaction wants to read data and no exclusive lock is
held on that data item
Multiple transactions can each have a shared lock on the same data
item if they are all just reading it
Mutual Exclusive Rule
Only one transaction at a time can own an exclusive lock on the same
object
Database Systems 6e/ Rob & Coronel Slide 39
Shared/Exclusive Locks
Increased overhead
The type of lock held must be known before a lock can be
granted
Three lock operations exist:
READ_LOCK to check the type of lock
WRITE_LOCK to issue the lock
UNLOCK to release the lock
A lock can be upgraded from share to exclusive and
downgraded from exclusive to share
Two possible major problems may occur
The resulting transaction schedule may not be serializable
The schedule may create deadlocks
Database Systems 6e/ Rob & Coronel Slide 40
Two-Phase Locking Protocol for Serializability
Defines how transactions acquire and
relinquish locks
Guarantees serializability, but it does not
prevent deadlocks
Growing phase, in which a transaction acquires
all the required locks without unlocking any
data
Shrinking phase, in which a transaction
releases all locks and cannot obtain any new
lock
Database Systems 6e/ Rob & Coronel Slide 41
Two-Phase Locking to Ensure Serializability
Governed by the following rules:
Two transactions cannot have conflicting locks
No unlock operation can precede a lock
operation in the same transaction
No data are affected until all locks are obtained
—that is, until the transaction is in its locked
point
Database Systems 6e/ Rob & Coronel Slide 42
Two-Phase Locking Protocol
Database Systems 6e/ Rob & Coronel Slide 43
Deadlocks
Condition that occurs when two transactions wait for
each other to unlock data
T1 needs data items X and Y; T needs Y and X
Each gets the its first piece of data but then waits to get the
second (which the other transaction is already holding) –
deadly embrace
Possible only if one of the transactions wants to
obtain an exclusive lock on a data item
No deadlock condition can exist among shared locks
Control through
Prevention
Detection
Avoidance
Database Systems 6e/ Rob & Coronel Slide 44
How a Deadlock Condition Is Created
Database Systems 6e/ Rob & Coronel Slide 45
Deadlock Detection
Wait-for graph without a cycle Wait-for graph with a cycle
Deadlocks can be described as a wait-for graph where:
vertices are all the transactions in the system
There is an edge Ti Tk in case Ti is waiting for Tk
When Ti requests a data item currently being held by Tk, then the
edge Ti Tk is inserted in the wait-for graph. This edge is removed
only when Tk is no longer holding a data item needed by Ti.
The system is in a deadlock state if and only if the wait-for graph
has a cycle. Must invoke a deadlock-detection algorithm
periodically to look for cycles.
Database Systems 6e/ Rob & Coronel Slide 46
Pitfalls of Lock-Based Protocols
Consider the partial schedule
Neither T3 nor T4 can make progress — executing lock-S(B)
causes T4 to wait for T3 to release its lock on B, while executing
lock-X(A) causes T3 to wait for T4 to release its lock on A.
Such a situation is called a deadlock.
To handle a deadlock one of T3 or T4 must be rolled back
and its locks released.
Database Systems 6e/ Rob & Coronel Slide 47
Pitfalls of Lock-Based Protocols (Cont.)
The potential for deadlock exists in most
locking protocols. Deadlocks are a necessary
evil.
Starvation is also possible if concurrency
control manager is badly designed. For
example:
A transaction may be waiting for an X-lock on an
item, while a sequence of other transactions
request and are granted an S-lock on the same
item.
The same transaction is repeatedly rolled back
due to deadlocks.
Database Systems 6e/ Rob & Coronel Slide 48
Pitfalls of Lock-Based Protocols (Cont.)
Concurrency control manager can be
designed to prevent starvation.
Two-phase locking does not ensure freedom
from deadlocks
Deadlock prevention protocols or deadlock
detection mechanisms are needed!
With detection mechanisms when deadlock is
detected :
Some transaction will have to rolled back (made
a victim) to break deadlock. Select that
transaction as victim that will incur minimum
cost.
Database Systems 6e/ Rob & Coronel Slide 49
Concurrency Control with Time
Stamping Methods
Assigns a global unique time stamp to each transaction
All database operations within the same transaction must have
the same time stamp
Produces an explicit order in which transactions are
submitted to the DBMS
Uniqueness
Ensures that no equal time stamp values can exist
Monotonicity
Ensures that time stamp values always increase
Disadvantage
Each value stored in the database requires two additional time
stamp fields – last read, last update
Database Systems 6e/ Rob & Coronel Slide 50
Wait/Die and Wound/Wait Schemes
Wait/die
Older transaction waits and the younger is rolled back
and rescheduled
Wound/wait
Older transaction rolls back the younger transaction
and reschedules it
In the situation where a transaction requests
multiple locks, each lock request has an
associated time-out value. If the lock is not
granted before the time-out expires, the
transaction is rolled back
Database Systems 6e/ Rob & Coronel Slide 51
Wait/Die and Wound/Wait
Concurrency Control Schemes
Database Systems 6e/ Rob & Coronel Slide 52
Concurrency Control with Optimistic Methods
Optimistic approach
Based on the assumption that the majority of
database operations do not conflict
Does not require locking or time stamping
techniques
Transaction is executed without restrictions until it
is committed
Acceptable for mostly read or query database
systems that require very few update transactions
Phases are read, validation, and write
Database Systems 6e/ Rob & Coronel Slide 53
Concurrency Control with Optimistic Methods
Phases are read, validation, and write
Read phase – transaction reads the database, executes
the needed computations and makes the updates to a
private copy of the database values.
All update operations of the transaction are recorded in
a temporary update file which is not accessed by the
remaining transactions
Validation phase – transaction is validated to ensure
that the changes made will not affect the integrity and
consistency of the database
If the validation test is positive, the transaction goes to
the writing phase. If negative, the transaction is
restarted and the changes discarded
Writing phase – the changes are permanently applied
to the database
Database Systems 6e/ Rob & Coronel Slide 54
Database Recovery
Backup Levels:
Full Backup – save whole
database plus transaction log
Differential Backup (save
only pages that changed since last backup)
Transaction Log Backup
(save only the transaction log)
Database Systems 6e/ Rob & Coronel Slide 55
Database Recovery
Database / System Failures:
Software (O.S., DBMS, Application
Programs, Viruses)
Hardware (Memory Chips, Disk
Crashes, Bad Sectors)
Programming Exemption (Application
Program rollbacks)
Transaction (Aborting transactions
due to deadlock detection)
External (Fire, Flood, sabotage, etc.)
Database Systems 6e/ Rob & Coronel Slide 56
Transaction Recovery
The database recovery process involves
bringing the database to a consistent state
after failure.
Transaction recovery procedures generally
make use of deferred-write and write-
through techniques
Database Systems 6e/ Rob & Coronel Slide 57
Transaction Recovery
Write-through
Database is immediately updated by
transaction operations during the transaction’s
execution, even before the transaction reaches
its commit point
If the transaction aborts before it reaches its
commit point, a ROLLBACK is done to restore
the database to a consistent state
A transaction that committed after the last checkpoint is
redone using the “after” values of the log
A transaction with a ROLLBACK after the last checkpoint
is rolled back using the “before” values in the log
Database Systems 6e/ Rob & Coronel Slide 58
Transaction Recovery
Deferred write
Transaction operations do not
immediately update the physical
database
Only the transaction log is updated
When updated buffers are being
written to disk, all other requests are
halted, not executed
Database Systems 6e/ Rob & Coronel Slide 59
Transaction Recovery
Deferred write, continued…
Redundant Transaction Logs – keep
several copies of log on different
devices, to offset failure.
Database Buffers – Buffers are used
to hold transaction logs and
dirty/modified pages
they increase processing time (overhead)
on updates especially when redundancy
is involved.
Database Systems 6e/ Rob & Coronel Slide 60
Transaction Recovery
Recover Database by using data
in the Transaction Log
If the transaction aborts before it
reaches its commit point, no
ROLLBACK is needed because the DB
was never updated
A transaction that performed a
COMMIT after the last checkpoint is
redone using the “after” values of the
transaction log
Database Systems 6e/ Rob & Coronel Slide 61
Database Recovery Management
Database recovery
Restores database from a given state, usually
inconsistent, to a previously consistent state
Based on the atomic transaction property
All portions of the transaction must be treated as
a single logical unit of work, in which all
operations must be applied and completed to
produce a consistent database
If transaction operation cannot be completed,
transaction must be aborted, and any changes
to the database must be rolled back (undone)
Database Systems 6e/ Rob & Coronel Slide 62
Checkpoints
Checkpoints are suited for deferred write strategy.
They are records registered in the transaction log.
They mark the last consistent state of the database.
They help reduce the time taken to recover from a
crash.
only transactions that executed since last checkpoint are
reviewed.
Writing updated buffers to the disk is scheduled
several times per hour
Saves information about active transactions and dirty
buffer pool pages
Database Systems 6e/ Rob & Coronel Slide 63
A Transaction Log for Transaction
Recovery Examples
Database Systems 6e/ Rob & Coronel Slide 64
Transaction Recovery Examples
T101 consists of 2 UPDATE statements that reduce
the QOH for product 54778-2T and increase the
customer balance for customer 10011 for a credit
sale of 2 units of that product
T106 represents a credit sale of 1 unit of 89-WRE-
Q to customer 10016 for $277.55 This transaction
consists of 3 INSERT and 2 UPDATE statements
T155 is an inventory update increasing QOH for
2232/QWE to 26 units
A DB checkpoint wrote all updated database
buffers to disk which is only for T101
Database Systems 6e/ Rob & Coronel Slide 65
Transaction Recovery Examples
Last checkpoint was 423
T101 started and finished before that checkpoint
so all changes were written to disk and no action
need be taken
T106 and T155 committed after the last
checkpoint so they will have their “after” values
written to disk using the log
Database Systems 6e/ Rob & Coronel Slide 66
ARIES Recovery - Algorithms for Recovery and
Isolation Exploiting Semantics
There are 3 phases in the Aries recovery
algorithm:
Analysis: Scan the log forward (from
the most recent checkpoint) to identify
all transactions that were active, and all
dirty pages in the buffer pool at the
time of the crash.
Redo: Redo all updates to dirty pages
in the buffer pool, as needed, to ensure
that all logged updates are in fact
carried out and written to disk.
Database Systems 6e/ Rob & Coronel Slide 67
ARIES Recovery - Algorithms for Recovery and
Isolation Exploiting Semantics
There are 3 phases in the Aries recovery
algorithm…continued:
Undo: The writes of all transactions
that were active at the crash are
undone
By restoring the before value of the update,
which is in the log record for the update.
working backwards in the log.
Some care must be taken to handle the
case of a crash occurring during the
recovery process!
Database Systems 6e/ Rob & Coronel Slide 68