Chapter .
10 Transaction Management and Concurrency Control
Chapter 10
Transaction Management and Concurrency Control
Discussion Focus
Why does a multi-user database environment give us a special interest in transaction management
and concurrency control?
Begin by exploring what a transaction is, what its components are, and why it must be managed
carefully even in a single-user database environment. A multi-user database environment makes
transaction management even more critical.
Emphasize the following points:
A transaction represents a real- world event such as the sale of a product.
A transaction must be a logical unit of work. That is, no portion of a transaction stands by itself.
For example, the product sale has an effect on inventory and, if it is a credit sale, it has an effect
on customer balances.
A transaction must take a database from one consistent state to another. Therefore, all parts of a
transaction must be executed or the transaction must be aborted. (A consistent state of the
database is one in which all data integrity constraints are satisfied.)
All transactions have four properties: Atomicity, Consistency, Isolation, and Durability. (These four
properties are also known as the ACID test of transactions.) In addition, multiple transactions must
conform to the property of serializability. Table IM10.1 provides a good summary of transaction
properties.
Table IM10.1 Transaction Properties.
atomicity: Unless all parts of the executed, the
transaction is aborted
Single-user consistency. Indicates the permanence of the
Databases database’s consistent state.
durability: Once a transaction is committed, it
Multi-user cannot be rolled back
Databases isolation: Data used by one transaction cannot be
used by another transaction until the first transaction
is completed.
serializability: The result of the concurrent execution
of transactions is the same as though the transactions
were executed in serial order.
93
Chapter .10 Transaction Management and Concurrency Control
Note that SQL provides transaction support through
COMMIT (permanently saves changes to disk)
and
ROLLBACK (restores the previous database state)
Each SQL transaction is composed of several database requests, each one of which yields I/O
operations. A transaction log keeps track of all transactions that modify the database. The transaction log
data may be used for recovery (ROLLBACK) purposes.
Next, the concurrency control is the management of concurrent transaction execution. (Therefore, a
single-user database does not require concurrency control!) There are concurrency control issues
concerning lost updates, uncommitted data, and inconsistent databases. Note that multi-user DBMSs use
a process known as a scheduler to enforce concurrency control.
Since concurrency control is made possible through the use of locks, examine the various levels and
types of locks. Because the use of locks creates a possibility of producing deadlocks, read about the
detection, prevention, and avoidance strategies that enable the system to manage deadlock conditions.
Answers to Review Questions
1. Explain the following statement: a transaction is a logical unit of work. (add more than the
textbook explanation – add your understanding)
A transaction is a logical unit of work that must be entirely completed of aborted; no intermediate
states are accepted. In other words, a transaction, composed of several database requests, is treated
by the DBMS as a unit of work in which all transaction steps must be fully completed if the
transaction is to be accepted by the DBMS.
Acceptance of an incomplete transaction will yield an inconsistent database state. To avoid such a
state, the DBMS ensures that all of a transaction’s database operations are completed before they are
committed to the database. For example, a credit sale requires a minimum of three database
operations:
1. An invoice is created for the sold product.
2. The product’s inventory quantity on hand is reduced.
3. The customer accounts payable balance is increased by the amount listed on the invoice.
If only parts 1 and 2 are completed, the database will be left in an inconsistent state. Unless all three
parts are completed, the entire sales transaction is canceled.
2. What is a consistent database state, and how is it achieved?
94
Chapter .10 Transaction Management and Concurrency Control
A consistent database state is one in which all data integrity constraints are satisfied. To achieve a
consistent database state, a transaction must take the database from one consistent state to another.
3. The DBMS does not guarantee that the semantic meaning of the transaction truly represents
the real-world event. What are the possible consequences of that limitation? Give an example.
The database is designed to verify the syntactic accuracy of the database commands given by the
user to be executed by the DBMS. The DBMS will check that the database exists, that the referenced
attributes exist in the selected tables, that the attribute data types are correct, and so on.
Unfortunately, the DBMS is not designed to guarantee that the syntactically correct transaction
accurately represents the real-world event.
For example, if the end user sells 10 units of product 100179, the DBMS cannot detect errors such as
the operator entering 10 units of product 100197. The DBMS will execute the transaction, and the
database will end up in a technically consistent state but in a real-world inconsistent state because
the wrong product was updated.
4. List and discuss the five transaction properties.
Atomicity – requires that all parts of a transaction must be completed or the transaction is aborted.
This property ensures that the database will remain in a consistent state.
Consistency – indicates the permanence of the database consistent state.
Isolation – means that the data required by an executing transaction cannot be accessed by any other
transaction until the first transaction finishes. This property ensures data consistency for concurrently
executing transactions.
Durability – indicates that the database will be in a permanent consistent state after the execution of
a transaction. In other words, once a consistent state is reached, it cannot be lost.
Serializability – means that a series of concurrent transactions will yield the same result as if they
were executed one after another.
95
Chapter .10 Transaction Management and Concurrency Control
5. What does serializability of transactions mean?
Serializability of transactions means that a series of concurrent transactions will yield the same result
as if they were executed one after another.
6. What is a transaction log, and what is its function?
The transaction log is a special DBMS table that contains a description of all the database
transactions executed by the DBMS. The database transaction log plays a crucial role in maintaining
database concurrency control and integrity.
7. What is a scheduler, what does it do, and why is its activity important to concurrency control?
The scheduler is the DBMS component that establishes the order in which concurrent database
operations are executed. The scheduler interleaves the execution of the database operations to ensure
the serializability of transactions. In other words, the scheduler guarantees that the execution of
concurrent transactions will yield the same result as though the transactions were executed one after
another. The scheduler is important because it is the DBMS component that will ensure transaction
serializability.
8. What is a lock, and how, in general, does it work?
A lock is a mechanism used in a concurrency control to guarantee the exclusive use of a data element
to the transaction that owns the lock. For example, if the data element X is currently locked by
transaction T1, transaction T2 will not have access to the data element X until T1 releases its lock.
9. What are the different levels of lock granuality?
Database-level – entire database is locked by one lock
Table-level – table is locked by one lock
Page-level – diskpage is locked by one lock
Row-level – one row is locked by one lock
Field-level – one field in one row is locked by one lock
10. Read this discussion on what is a deadlock, and how can it be avoided. There are several
strategies for dealing with deadlocks. Then, answer a, b, & c below:
Base on Chapter 10’s Section 10.3.4, Deadlocks. Although locks prevent serious data
inconsistencies, their use may lead to two major problems:
1. The transaction schedule dictated by the locking requirements may not be serializable, thus
causing data integrity and consistency problems.
2. The schedule may create deadlocks. Database deadlocks are the equivalent of a traffic
gridlock in a big city and are caused by two transactions waiting for each other to unlock
data.
96
Chapter .10 Transaction Management and Concurrency Control
Table 10.13 in the text illustrates the scenario that leads to a deadlock.) The table has been
reproduced below for your convenience.
TABLE 10.13 How a Deadlock Condition is Created
TIME TRANSACTION REPLY LOCK STATUS
0 Data X Data Y
1 T1:LOCK(X) OK Unlocked Unlocked
2 T2: LOCK(Y) OK Locked Unlocked
3 T1:LOCK(Y) WAIT Locked Locked
4 T2:LOCK(X) WAIT Locked Deadlock Locked
5 T1:LOCK(Y) WAIT Locked Locked
6 T2:LOCK(X) WAIT Locked Locked
7 T1:LOCK(Y) WAIT Locked Locked
8 T2:LOCK(X) WAIT Locked Locked
9 T1:LOCK(Y) WAIT Locked Locked
… ………….. …….. ……… …….…
… ………….. …….. ……… …….…
… ………….. …….. ……… …….…
… ………….. …….. ……… ………
In a real world DBMS, many more transactions can be executed simultaneously, thereby increasing
the probability of generating deadlocks. Note that deadlocks are 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.
Three basic techniques exist to control deadlocks:
Discuss in your words the following:
a. Deadlock Prevention
A transaction requesting a new lock is aborted if there is a possibility that a deadlock may occur. If
the transaction is aborted, all the changes made by this transaction are rolled back and all locks are
released. The transaction is then re-scheduled for execution. Deadlock prevention works because it
avoids the conditions that lead to deadlocking.
b. Deadlock Detection
The DBMS periodically tests the database for deadlocks. If a deadlock is found, one of the
transactions is aborted and the other transaction continues.
c. Deadlock Avoidance
97
Chapter .10 Transaction Management and Concurrency Control
The transaction must obtain all the locks it needs before it can be executed. This technique avoids
rollback of conflicting transactions by requiring that locks be obtained in succession. However, the
serial lock assignment required in deadlock avoidance increases the response times.
98