Transaction & Concurrency Control
Transaction & Concurrency Control
Transaction & Concurrency Control
Transaction &
Concurrency Control
What Is a Transaction?
• A transaction is a logical unit of work that must be
either entirely completed or aborted; no
intermediate states are acceptable.
– Most real-world database transactions are formed
by two or more database requests.
– A database request is the equivalent of a single
SQL statement in an application program or
transaction.
– A transaction that changes the contents of the
database must alter the database from one
consistent database state to another.
– To ensure consistency of the database, every
transaction must begin with the database in a
known consistent state.
Transaction
You store
40 in your
database
40
X = 40
Transaction
Take
40 out 10
X = 40 - 10
Transaction
You left
remaining
30 in your
database 30
X = 30
Example Of A Transaction
What Is a Transaction?
• Evaluating Transaction Results
– 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.
What Is a Transaction?
• Evaluating Transaction Results
– 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.
• Adding $500.00 to customer Y’s accounts
receivable.
UPDATE PRODUCT
SET PROD_QOH = PROD_QOH - 100
WHERE PROD_CODE = ‘X’;
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.
What Is a Transaction?
• 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’;
What Is a Transaction?
• Transaction Properties
– Atomicity requires that all operations
of a transaction be completed; if not,
the transaction is aborted.
– Durability indicates the permanence of
the database’s consistent state.
– Serializability describes the result of
the concurrent execution of several
transactions. This property is important
in multi-user and distributed
databases.
– Isolation means that the data used
during the execution of a transaction
cannot be used by a second transaction
What Is a Transaction?
• 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).
What Is a Transaction?
• 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;
What Is a Transaction?
• 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
A Transaction Log
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.
•
Concurrency Control
• Lost Updates
– Two concurrent transactions update
PROD_QOH:
TRANSACTION COMPUTATION
T1: Purchase 100 units PROD_QOH = PROD_QOH + 100
T2: Sell 30 units PROD_QOH = PROD_QOH - 30
T1
Read = 35
Add + 100
PROD_
Total = 135 QOH
Normal Execution of
2 Transaction
T1 T2
+ 100 - 30
PROD_
= 135 QOH = 105
Lost Update
( 2 transaction executed
simultaneously)
T1 T2
Read = 35 Read = 35
+ 100 - 30
PROD_
= 135 QOH =5
Table 9.2 Normal Execution Of Two Transactions
T1
Read = 35
+ 100
PROD_
= 135 QOH
z
z
z
Uncommitted Data
Problem
T1 T2
+ 100 - 30
PROD_
= 135 QOH = 105
z
z
z
Uncommitted Data
Problem
T1 T2
+ 100 - 30
PROD_
= 135 QOH = 105
z
z
z
Rollback = 35
Concurrency Control
• Inconsistent Retrievals
– Inconsistent retrievals occur when a
transaction calculates some summary
(aggregate) functions over a set of data
while other transactions are updating the
data.
– 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.
Retrieval During Update
Retrieval During Update
T1 T2
SUM UPDATE
(PROD_QOH) PROD_QO
PROD_ H +30
QOH
? = 35 ?
Transaction Results: Data Entry Correction
Inconsistent Retrievals
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.
– 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
Read/Write Conflict Scenarios:
Conflicting Database Operations Matrix
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.
• All lock of information is managed by
a lock manager.
Concurrency Control with
Locking Methods
• Lock Granularity
– Lock granularity indicates the
level of lock use.
• Database level (See Figure 9.2)
• Table level (See Figure 9.3)
• Page level (See Figure 9.4)
• Row level (See Figure 9.5)
• Field level
A Database-Level Locking Sequence
An Example Of A Table-Level Lock
An Example Of A Page-Level Lock
An Example Of A Row-Level Lock
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.
An Example Of A Binary Lock
Concurrency Control
with Locking Methods
Shared Locks
Exclusive Locks
• A shared lock exists
• An exclusive lock exists when concurrent
when access is specially transactions are
reserved for the granted READ access
transaction that locked on the basis of a
the object. common lock.
• The exclusive lock must • A shared lock produces
be used when the no conflict as long as
potential for conflict the concurrent
exists. transactions are read
• An exclusive lock is only.
issued when a • A shared lock is issued
transaction wants to when a transaction
write (update) a data wants to read data
item and no locks are from the database and
Concurrency Control
with Locking Methods
• Potential Problems with Locks
– The resulting transaction
schedule may not be
serializable.
– The schedule may create
deadlocks.
• Solutions
– Two-phase locking for the
serializability problem.
– Deadlock detection and
prevention techniques for the
Concurrency Control with 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.
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.
Two-Phase Locking Protocol
Concurrency Control
with Locking Methods
• Deadlocks (Deadly Embrace)
– Deadlocks exist when two
transactions T1 and T2 exist in the
following mode:
– 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.
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
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 the database.
Database Recovery
Management
• Recovery restores a database from a given
state, usually inconsistent, to a previously
consistent state.