Transaction & Concurrency
Control
Lecture 10
Carlos Coronel, Steven Morris, Peter Rob, Database Systems: Design,
Implementation, and Management, 9th Edition, Cengage Learning
Learning Outcomes
What a database transaction is and what its
properties are
What concurrency control is and what role it
plays in maintaining the database’s integrity
What locking methods are and how they work
How stamping methods are used for
concurrency control
How optimistic methods are used for
concurrency control
How database recovery management is used
to maintain database integrity
2 TDB2111 Database Systems 5/22/12
What is a Transaction?
Transfer $100 from your account to your Mom’s
account:
Debit $100 from your bank account (Subtract $100)
Credit $100 to your mom's bank account (Add $100)
3 TDB2111 Database Systems 5/22/12
What Is a Transaction?
A transaction is a logical unit of work (UoW) that must
be either entirely completed or aborted; no
intermediate states are acceptable.
One or more SQL statements altogether treated as one
single unit
Most real-world database transactions are formed by
two or more database requests.
A transaction starts with any SQL statement and ends
with a COMMIT or ROLLBACK
COMMIT statement makes changes permanent to the
database
ROLLBACK statement reverses changes
COMMIT and ROLLBACK statements release all locks
4 TDB2111 Database Systems 5/22/12
Database Request is a Transaction
A database request is the equivalent of a single
SQL statement in an application program or
transaction.
SQL code represents a transaction because
database was accessed
To ensure consistency of the database, every
transaction must begin with the database in a
known consistent state.
A transaction that changes the contents of the
database must alter the database from one
consistent database state to another.
5 TDB2111 Database Systems 5/22/12
Example
6 TDB2111 Database Systems 5/22/12
Evaluating Transaction Results
Not all transactions update database
Examining the current balance for an
account:
SELECT ACC_NUM, ACC_BALANCE
FROM CHECKACC
WHERE ACC_NUM = ‘0908110638’;
The database remains in a consistent state after
the transaction, because it did not alter the
database.
7 TDB2111 Database Systems 5/22/12
Evaluating Transaction Results
Improper or incomplete transactions can have devastating
effect on database integrity
An accountant wishes to register the credit sale of 100 units
of product X to customer Y in the amount of $500.00:
Reducing product X’s Quantity on hand by 100.
UPDATE PRODUCT
SET PROD_QOH = PROD_QOH - 100
WHERE PROD_CODE = ‘X’;
Adding $500.00 to customer Y’s accounts receivable.
UPDATE ACCREC
SET AR_BALANCE = AR_BALANCE + 500
WHERE AR_NUM = ‘Y’;
If the above two transactions are not completely executed,
the transaction yields an inconsistent database.
8 TDB2111 Database Systems 5/22/12
Evaluating Transaction Results
The DBMS does not guarantee that the semantic
meaning of the transaction truly represents the
real-world event.
Although the syntax of the following UPDATE
command is correct, its use yields incorrect
results.
UPDATE PRODUCT
SET PROD_QOH = PROD_QOH + 10
WHERE PROD_CODE = ‘X’;
9 TDB2111 Database Systems 5/22/12
Transaction Properties (ACIDS)
Atomicity requires that all operations of a transaction be
completed; if not, the transaction is aborted.
Consistency indicates the permanence of the database’s
consistent state.
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.
Durability means that once transaction change are
committed, they cannot be undone or lost
Serializability describes the result of the concurrent
execution of several transactions yield consistent result.
This property is important in multi-user and distributed
databases.
Multiuser databases are subject to multiple concurrent
(multiple users accessing the same resources at the same
10 TDB2111 Database Systems 5/22/12
time) transactions
Transaction Management with SQL
Transaction support is provided by two SQL
statements: COMMIT and ROLLBACK.
When a transaction sequence is initiated, it must
continue through all succeeding SQL statements
until one of the following four events occurs:
A COMMIT statement is reached.
A ROLLBACK statement is reached.
The end of a program is successfully reached
(COMMIT).
The program is abnormally terminated
(ROLLBACK).
11 TDB2111 Database Systems 5/22/12
Transaction Management with SQL
Example:
UPDATE PRODUCT
SET PROD_QOH = PROD_QOH - 100
WHERE PROD_CODE = ‘345TYX’;
UPDATE ACCREC
SET AR_BALANCE = AR_BALANCE + 3500
WHERE AR_NUM = ‘60120010’;
COMMIT;
12 TDB2111 Database Systems 5/22/12
The Transaction Log
A transaction log keeps track of all transactions
that update the database.
The information stored in the log is used by the
DBMS for a recovery requirement triggered by a
ROLLBACK statement or a system failure.
The transaction log stores before-and-after data
about the database and any of the tables, rows,
and attribute values that participated in the
transaction.
The transaction log is itself a database, and it is
managed by the DBMS like any other database.
13 TDB2111 Database Systems 5/22/12
Transaction Logs
Typically, a transaction log stores:
A record to indicate beginning of transaction
Type of operation (UPDATE, INSERT)
Names of objects affected
“Before” and “After” values of fields updated
Pointers to previous and next transaction log entries for
the same transaction
Ending COMMIT of the transaction
14 TDB2111 Database Systems 5/22/12
A Transaction Log
15 TDB2111 Database Systems 5/22/12
Concurrency Control
Concurrency control coordinates simultaneous
execution of transactions in a multiprocessing
database.
The objective of concurrency control is to ensure
the serializability of transactions in a multi-user
database environment.
Simultaneous execution of transactions over a
shared database can create several data integrity
and consistency problems:
Lost Updates.
Uncommitted Data.
Inconsistent retrievals.
16 TDB2111 Database Systems 5/22/12
Concurrency control - Lost Updates
Two concurrent
transactions
update same data
element
One of the updates
is lost
Overwritten by
the other
transaction
17 TDB2111 Database Systems 5/22/12
Concurrency control - Lost Updates
Table 9.2 Normal Execution Of Two Transactions
Table 9.3 Lost Updates
18 TDB2111
TDB2111 Database Systems
Database Systems 5/22/12
5/22/12
Concurrency Control- Uncommitted Data
Two transactions are
executed concurrently.
First transaction rolled
back after second already
accessed uncommitted
data.
Thus violating the
isolation property of the
transaction.
19 TDB2111 Database Systems 5/22/12
Concurrency Control- Uncommitted Data
Table 9.4 Correct Execution Of Two Transactions
Table 9.5 An Uncommitted Data Problem
T2 interrupt and read the value of T1 before T1 commit
20 TDB2111
TDB2111 Database Systems
Database Systems 5/22/12
5/22/12
Concurrency Control –inconsistent retrievals
Inconsistent Retrievals
Example:
T1 calculates the total quantity on hand of the products
stored in the PRODUCT table.
At the same time, T2 updates the quantity on hand
(PROD_QOH) for two of the PRODUCT table’s products.
21 TDB2111 Database Systems 5/22/12
Concurrency Control –inconsistent retrievals
First transaction accesses
data.
Second transaction alters the
data.
First transaction accesses the
data again.
Transaction might read some
data before/after they are
changed.
Yields inconsistent results
22 TDB2111 Database Systems 5/22/12
Retrieval During Update
23 TDB2111 Database Systems 5/22/12
Transaction Results:
Data Entry Correction
24 TDB2111 Database Systems 5/22/12
Inconsistent Retrievals
25 TDB2111 Database Systems 5/22/12
Concurrency Control
The Scheduler
The scheduler establishes the order in which the
operations within concurrent transactions are
executed.
The scheduler 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 or time stamping
methods.
The scheduler also makes sure that the
computer’s CPU is used efficiently.
Serializable
26 schedule
TDB2111 Database Systems 5/22/12
Read/Write Conflict Scenarios:
Conflicting Database Operations Matrix
27 TDB2111 Database Systems 5/22/12
Concurrency Control with Locking
Methods
Concurrency can be controlled using locks.
A lock guarantees exclusive use of a data item
to a current transaction.
A transaction acquires a lock prior to data
access; the lock is released (unlocked) when
the transaction is completed.
Required to prevent another transaction from
reading inconsistent data
All lock of information is managed by a lock
manager.
Responsible for assigning and policing the locks
used by transactions
28 TDB2111 Database Systems 5/22/12
Concurrency Control with Locking
Methods
Lock Granularity
Lock granularity indicates the level of lock use.
Database level (See Figure 10.3)
Table level (See Figure 10.4)
Page level (See Figure 10.5)
Row level (See Figure 10.6)
Field level
29 TDB2111 Database Systems 5/22/12
A Database-Level Locking Sequence
30 TDB2111 Database Systems 5/22/12
An Example Of A Table-Level Lock
31 TDB2111 Database Systems 5/22/12
An Example Of A Page-Level Lock
32 TDB2111 Database Systems 5/22/12
An Example Of A Row-Level Lock
33 TDB2111 Database Systems 5/22/12
Concurrency Control with Locking
Methods
Binary Locks
A binary lock has only two states: locked (1) or
unlocked (0).
If an object is locked by a transaction, no other
transaction can use that object.
If an object is unlocked, any transaction can lock
the object for its use.
A transaction must unlock the object after its
termination.
Every transaction requires a lock and unlock
operation for each data item that is accessed.
34 TDB2111 Database Systems 5/22/12
Concurrency Control with Locking
Methods
App B hangs until App A commits or rolls back which
releases the lock.
35 TDB2111 Database Systems 5/22/12
An Example Of A Binary Lock
36 TDB2111 Database Systems 5/22/12
Concurrency Control with Locking
Methods
Exclusive Locks Shared Locks
An exclusive lock exists A shared lock exists when
when access is specially concurrent transactions are
reserved for the transaction granted READ access on the
that locked the object. basis of a common lock.
The exclusive lock must be A shared lock produces no
used when the potential for conflict as long as the
conflict exists. concurrent transactions are
read only.
An exclusive lock is issued
when a transaction wants to A shared lock is issued when
write (update) a data item a transaction wants to read
and no locks are currently data from the database and
held on that data item. no exclusive lock is held on
(MUTUAL EXCLUSIVE RULE) that data item.
TDB2111 Database Systems 5/22/12
Exclusive Lock (X)
App B hangs until App A commits or rolls back which
releases the lock.
38 TDB2111 Database Systems 5/22/12
Shared Lock (S)
App B retrieves currently committed value.
39 TDB2111 Database Systems 5/22/12
Potential Problems with Locks
Potential Problems with Locks
Problem 1: The resulting transaction schedule may
not be serializable.
Problem 2: The schedule may create deadlocks.
Deadlocks (Deadly Embrace)
Condition that occurs when two transactions wait
for each other to unlock data
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
40 TDB2111 Database Systems 5/22/12
Deadlocks
Occurs when two or more applications wait indefinitely for a
resource
Each application is holding a resource that the other needs
Waiting is never resolved
41 TDB2111 Database Systems 5/22/12
Deadlocks
Deadlocks are commonly caused by poor
application design
DB2 provides a deadlock detector
Use DLCHKTIME (db cfg) to set the time interval
for checking for deadlocks
When a deadlock is detected, DB2 uses an
internal algorithm to pick which transaction to
roll back, and which one to continue.
The transaction that is forced to roll back gets a
SQL error. The rollback causes all of its locks to
be released
42 TDB2111 Database Systems 5/22/12
Solutions
Solutions
For serializability: Two-phase locking can be used
For deadlock: Deadlock detection and prevention
techniques for the deadlock problem.
43 TDB2111 Database Systems 5/22/12
Deadlocks solutions – locking methods
Two-Phase Locking
The two-phase locking protocol defines how
transactions acquire and relinquish locks. It
guarantees serializability, but it does not prevent
deadlocks.
In a growing phase, a transaction acquires all the
required locks without unlocking any data. Once
all locks have been acquired, the transaction is in
its locked point.
In a shrinking phase, a transaction releases all
locks and cannot obtain any new locks.
44 TDB2111 Database Systems 5/22/12
Concurrency Control with Locking
Methods
Rules for Two-Phase Locking Protocol
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.
45 TDB2111 Database Systems 5/22/12
Two-Phase Locking Protocol
46 TDB2111 Database Systems 5/22/12
Concurrency Control with Locking
Methods
Three Techniques to Control Deadlocks:
Deadlock Prevention
A transaction requesting a new lock is aborted if there is
a possibility that a deadlock can occur.
Deadlock Detection
The DBMS periodically tests the database for deadlocks.
If a deadlock is found, one of the transactions (“victim”)
is aborted, and the other transaction continues.
Deadlock Avoidance
The transaction must obtain all the locks it needs before
it can be executed.
47 TDB2111 Database Systems 5/22/12
Concurrency Control with Locking
Methods
Choice of deadlock control method depends on
database environment
Low probability of deadlock; detection recommended
High probability; prevention recommended
48 TDB2111 Database Systems 5/22/12
Concurrency Control with Time
Stamping Methods
The time stamping approach assigns a global
unique time stamp to each transaction to
schedule concurrent transactions.
The time stamp value produces an explicit
order in which transactions are submitted to
the DBMS.
Time stamps must have two properties:
Uniqueness: assures that no equal time stamp
values can exist.
Monotonicity: assures that time stamp values
always increase.
The DBMS executes conflicting operations in
time stamp order
49 toDatabase
TDB2111 ensureSystems
the serializability.
5/22/12
Time stamping methods
A transaction requesting a lock on an object
which is already locked, it can be served by 2
different schemes:
WAIT/DIE
Older transaction waits and younger is rolled back and
rescheduled
When transaction Ti requests a data item currently held by Tj, Ti
is allowed to wait only if it has a timestamp smaller than that of
Tj (That is Ti is older than Tj), otherwise Ti is rolled back (dies).
WOUND/WAIT
Older transaction rolls back younger transaction and
reschedules it
When Transaction Ti requests a data item currently held by Tj,
Ti is allowed to wait only if it has a timestamp larger than that of
50 Tj, otherwise TjTDB2111
is rolled back (Tj is Systems
Database wounded by Ti).
5/22/12
Time stamping methods
Timestamp
51 TDB2111 Database Systems 5/22/12
Time stamping methods
By default, an application waits indefinitely to
obtain any needed locks
DB2 uses LOCKTIMEOUT (db cfg):
Specifies the number of seconds to wait for a
lock
Default value is -1 or infinite wait
52 TDB2111 Database Systems 5/22/12
Concurrency Control with Optimistic
Methods
Optimistic Methods
It is based on the assumption that the majority of
the database operations do not conflict.
A transaction is executed without restrictions until
it is committed.
Each transaction moves through two or three
phases:
Read Phase: The transaction reads the database,
executes the needed computations, and makes the
updates to a private copy of the database values.
Validation Phase: The transaction is validated to assure
that the changes made will not affect the integrity and
consistency of the database.
Write Phase: The changes are permanently applied to
53 the database.TDB2111 Database Systems 5/22/12
Database Recovery Management
Recovery restores a database from a given
state, usually inconsistent, to a previously
consistent state.
Recovery techniques are based on the atomic
transaction property:
All portions of transaction are treated as single
logical unit of work
All operations are applied and completed to
produce consistent database
If transaction operation cannot be completed:
Transaction aborted
Changes to database are rolled back
54 TDB2111 Database Systems 5/22/12
Database Recovery Management
Levels of Backup
Full backup of the database
It backs up or dumps the whole database.
Differential backup of the database
Only the last modifications done to the database are
copied.
Backup of the transaction log only
It backs up all the transaction log operations that are
not reflected in a previous backup copy of the database.
55 TDB2111 Database Systems 5/22/12
Database Recovery Management
Database Failures
Software
Operating system, DBMS, application programs, viruses
Hardware
Memory chip errors, disk crashes, bad disk sectors, disk
full errors
Programming Exemption
Application programs, end users
Transaction
Deadlocks
External
Fire, earthquake, flood
56 TDB2111 Database Systems 5/22/12
Transaction Recovery
Write-ahead-log protocol: ensures
transaction logs are written before data is
updated
Redundant transaction logs: ensure
physical disk failure will not impair ability to
recover
Buffers: temporary storage areas in primary
memory
Checkpoints: operations in which DBMS
writes all its updated buffers to disk
57 TDB2111 Database Systems 5/22/12
Transaction Recovery (cont’d.)
Deferred-write technique
Only transaction log is updated
Recovery process: identify last checkpoint
If transaction committed before checkpoint
Do nothing
If transaction committed after checkpoint
Use transaction log to redo the transaction
If transaction had ROLLBACK operation
Do nothing
58 TDB2111 Database Systems 5/22/12
Transaction Recovery (cont’d.)
Write-through technique
Database is immediately updated by transaction
operations during transaction’s execution
Recovery process: identify last checkpoint
If transaction committed before checkpoint
Do nothing
If transaction committed after last checkpoint
DBMS redoes the transaction using “after” values
If transaction had ROLLBACK or was left active
Do nothing because no updates were made
59 TDB2111 Database Systems 5/22/12
60 TDB2111 Database Systems 5/22/12