DFC2033 DATABASE SYSTEM
Course Learning Outcome
CLO2 : Develop a database using a
concurrency control and data recovery
technique to manage the database system.
¨ Describe the properties of database transaction:
¨ Perform a transaction of a given database using SQL statements.
¨ Use START TRANSACTION and COMMIT statements.
¨ State the purpose of concurrency control.
¨ Explain the interference problems arise from simultaneous access to database:
a. Lost update
b. Uncommitted dependency
c. Inconsistent retrieval
¨ Describe the tools used by DBMS to prevent the interference problems : locks, Two-Phase
Locking (2PL) protocol.
¨ Describe the recovery tools:
a. Transaction log
c. Database back-up
What is Transaction ?
¨ A transaction is a unit of work that should be
processed reliably without interference from other
users and without loss of data due to failures.
¨ Examples of transactions are withdrawing cash at
an ATM, making an airline reservation and
registering for a course.
¨ The purpose of concurrency control is to prevent
two different users (or two different connections by
the same user) from trying to update the same
data at the same time. Concurrency control can
also prevent one user from seeing out-of-date
data while another user is updating the same data.
The following examples explain why concurrency control is needed. For both examples,
suppose that your checking account contains $1,000. During the day you deposit $300 and
spend $200 from that account. At the end of the day your account should have $1,100.
¨ Example 1: No concurrency control
At 11:00 AM, bank teller #1 looks up your account and sees that you have $1,000. The
teller subtracts the $200 check, but is not able to save the updated account balance ($800)
immediately. At 11:01 AM, another teller #2 looks up your account and still sees the $1,000
balance. Teller #2 then adds your $300 deposit and saves your new account balance as
$1,300. At 11:09 AM, bank teller #1 returns to the terminal, finishes entering and saving
the updated value that is calculated to be $800. That $800 value writes over the $1300. At
the end of the day, your account has $800 when it should have had $1,100 ($1000 + 300 -
¨ Example 2: Concurrency control
When teller #1 starts working on your account, a lock is placed on the account. When teller
#2 tries to read or update your account while teller #1 is updating your account, teller #2
will not be given access and gets an error message. After teller #1 has finished the update,
teller #2 can proceed. At the end of the day, your account has $1,100 ($1000 - 200 +
Interference problem arise from simultaneous access
¨ Lost Update
¨ Uncommitted Dependency
¨ Inconsistent Retrieval
¨ Successfully completed update is overridden by
¨ Lost updates occur when two or more transactions
select the same row and then update the row
based on the value originally selected.
¨ Each transaction is unaware of other transactions.
The last updates overwrites updates made by the
other transactions which results in lost data.
¨ Occurs when one transaction can see intermediate
results of another transaction before it has
¨ Uncommitted dependency occurs when a second
transaction selects a row that is being updated by
¨ The second transaction is reading data that has not
been committed yet and may be changed by the
transaction updating the row.
¨ Occurs when transaction reads several values but
second transaction updates some of them during
execution of first.
Database Concurrency Control
Two-Phase Locking Techniques -The algorithm
(a) Locking (Growing)
(b) Unlocking (Shrinking).
¨ Locking (Growing) Phase: A transaction applies locks (read
or write) on desired data items one at a time.
¨ Unlocking (Shrinking) Phase: A transaction unlocks its locked
data items one at a time.
For a transaction these two phases must be mutually exclusively,
that is, during locking phase unlocking phase must not start and
during unlocking phase locking phase must not begin.
¨ Deadlock prevention
¤ A transaction locks all data items it refers to before it
¤ This way of locking prevents deadlock since a
transaction never waits for a data item.
¤ The conservative two-phase locking uses this approach.
¨ Deadlock detection and resolution
¤ In this approach, deadlocks are allowed to happen. The
scheduler maintains a wait-for-graph for detecting cycle. If a
cycle exists, then one transaction involved in the cycle is selected
(victim) and rolled-back.
¤ A wait-for-graph is created using the lock table. As soon as a
transaction is blocked, it is added to the graph. When a chain
like: Ti waits for Tj waits for Tk waits for Ti or Tj occurs, then this
creates a cycle. One of the transaction o
Types of Recovery Tools
¨ Transaction Log
¤ A table that contains a history of database changes.
The recovery manager uses the log table to recover
¤ The act of writing a checkpoint to log and writing log
and database buffer to disk.
¨ Database Backup
¤ A copy of all or part of disk. Is used when the disk
containing the database or log is damaged.