You are on page 1of 35

Transaction Concept

● A transaction is a unit of program execution that


accesses and possibly updates various data items.
● A transaction must see a consistent database.
● During transaction execution the database may be
inconsistent.
● When the transaction is committed, the database must
be consistent.
● Two main issues to deal with:
★ Failures of various kinds, such as hardware failures and
system crashes
★ Concurrent execution of multiple transactions

Database System Concepts 15.1 ©Silberschatz, Korth and Sudarshan


ACID Properties
To preserve integrity of data, the database system must ensure:
● Atomicity. Either all operations of the transaction are
properly reflected in the database or none are.
● Consistency. Execution of a transaction in isolation
preserves the consistency of the database.
● Isolation. Although multiple transactions may execute
concurrently, each transaction must be unaware of other
concurrently executing transactions. Intermediate
transaction results must be hidden from other concurrently
executed transactions.
★ That is, for every pair of transactions Ti and Tj, it appears to Ti
that either Tj, finished execution before Ti started, or Tj started
execution after Ti finished.
● Durability. After a transaction completes successfully, the
changes it has made to the database persist, even if there
are system failures.

Database System Concepts 15.2 ©Silberschatz, Korth and Sudarshan


Example of Fund Transfer
● Transaction to transfer $50 from account A to account B:
1. read(A)
2. A := A – 50
3. write(A)
4. read(B)
5. B := B + 50
6. write(B)
● Consistency requirement – the sum of A and B is unchanged
by the execution of the transaction.
● Atomicity requirement — if the transaction fails after step 3 and
before step 6, the system should ensure that its updates are
not reflected in the database, else an inconsistency will result.

Database System Concepts 15.3 ©Silberschatz, Korth and Sudarshan


Example of Fund Transfer (Cont.)

● Durability requirement — once the user has been notified


that the transaction has completed (i.e., the transfer of the
$50 has taken place), the updates to the database by the
transaction must persist despite failures.
● Isolation requirement — if between steps 3 and 6, another
transaction is allowed to access the partially updated
database, it will see an inconsistent database
(the sum A + B will be less than it should be).
Can be ensured trivially by running transactions serially,
that is one after the other. However, executing multiple
transactions concurrently has significant benefits, as we
will see.

Database System Concepts 15.4 ©Silberschatz, Korth and Sudarshan


Transaction State
● 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 – only if no internal logical error
★ kill the transaction
● Committed, after successful completion.

Database System Concepts 15.5 ©Silberschatz, Korth and Sudarshan


Transaction State (Cont.)

Database System Concepts 15.6 ©Silberschatz, Korth and Sudarshan


Schedules

● Schedules – sequences that indicate the chronological order in


which instructions of concurrent transactions are executed
★ a schedule for a set of transactions must consist of all instructions of
those transactions
★ must preserve the order in which the instructions appear in each
individual transaction.

Database System Concepts 15.7 ©Silberschatz, Korth and Sudarshan


Example Schedules
● Let T1 transfer $50 from A to B, and T2 transfer 10% of
the balance from A to B. The following is a serial
schedule (Schedule 1 in the text), in which T1 is
followed by T2.

Database System Concepts 15.8 ©Silberschatz, Korth and Sudarshan


Example Schedule (Cont.)
● Let T1 and T2 be the transactions defined previously. The
following schedule (Schedule 3 in the text) is not a serial
schedule, but it is equivalent to Schedule 1.

In both Schedule 1 and 3, the sum A + B is preserved.


Database System Concepts 15.9 ©Silberschatz, Korth and Sudarshan
Example Schedules (Cont.)
● The following concurrent schedule (Schedule 4 in the
text) does not preserve the value of the the sum A + B.

Database System Concepts 15.10 ©Silberschatz, Korth and Sudarshan


Serializability

Database System Concepts 15.11 ©Silberschatz, Korth and Sudarshan


Serializability
● Basic Assumption – Each transaction preserves database
consistency.
● Thus serial execution of a set of transactions preserves
database consistency.
● A (possibly concurrent) schedule is serializable if it is
equivalent to a serial schedule. Different forms of schedule
equivalence give rise to the notions of:
1. conflict serializability
2. view serializability
● We ignore operations other than read and write instructions,
and we assume that transactions may perform arbitrary
computations on data in local buffers in between reads and
writes. Our simplified schedules consist of only read and
write instructions.

Database System Concepts 15.12 ©Silberschatz, Korth and Sudarshan


Conflict Serializability

● Instructions li and lj of transactions Ti and Tj respectively, conflict


if and only if there exists some item Q accessed by both li and lj,
and at least one of these instructions wrote Q.
1. li = read(Q), lj = read(Q). li and lj don’t conflict.
2. li = read(Q), lj = write(Q). They conflict.
3. li = write(Q), lj = read(Q). They conflict
4. li = write(Q), lj = write(Q). They conflict
● Intuitively, a conflict between li and lj forces a (logical) temporal
order between them. If li and lj are consecutive in a schedule
and they do not conflict, their results would remain the same
even if they had been interchanged in the schedule.

Database System Concepts 15.13 ©Silberschatz, Korth and Sudarshan


Conflict Serializability (Cont.)
● If a schedule S can be transformed into a schedule S´ by a
series of swaps of non-conflicting instructions, we say that
S and S´ are conflict equivalent.
● We say that a schedule S is conflict serializable if it is
conflict equivalent to a serial schedule
● Example of a schedule that is not conflict serializable:
T3 T4
read(Q)
write(Q)
write(Q)

We are unable to swap instructions in the above schedule


to obtain either the serial schedule < T3, T4 >, or the serial
schedule < T4, T3 >.

Database System Concepts 15.14 ©Silberschatz, Korth and Sudarshan


Conflict Serializability (Cont.)

● Schedule 3 below can be transformed into Schedule 1, a


serial schedule where T2 follows T1, by series of swaps of
non-conflicting instructions. Therefore Schedule 3 is conflict
serializable.

Database System Concepts 15.15 ©Silberschatz, Korth and Sudarshan


Database System Concepts 15.16 ©Silberschatz, Korth and Sudarshan
View Serializability
● Let S and S´ be two schedules with the same set of transactions.
S and S´ are view equivalent if the following three conditions are
met:
1. For each data item Q, if transaction Ti reads the initial value of Q in
schedule S, then transaction Ti must, in schedule S´, also read the
initial value of Q.
2. For each data item Q if transaction Ti executes read(Q) in schedule S,
and that value was produced by transaction Tj (if any), then
transaction Ti must in schedule S´ also read the value of Q that was
produced by transaction Tj .
3. For each data item Q, the transaction (if any) that performs the final
write(Q) operation in schedule S must perform the final write(Q)
operation in schedule S´.
As can be seen, view equivalence is also based purely on reads
and writes alone.

Database System Concepts 15.17 ©Silberschatz, Korth and Sudarshan


View Serializability (Cont.)

● A schedule S is view serializable it is view equivalent to a serial


schedule.
● Every conflict serializable schedule is also view serializable.
● Schedule 9 (from text) — a schedule which is view-serializable
but not conflict serializable.

● Every view serializable schedule that is not conflict


serializable has blind writes.

Database System Concepts 15.18 ©Silberschatz, Korth and Sudarshan


Other Notions of Serializability
● Schedule 8 (from text) given below produces same
outcome as the serial schedule < T1, T5 >, yet is not
conflict equivalent or view equivalent to it.

● Determining such equivalence requires analysis of


operations other than read and write.

Database System Concepts 15.19 ©Silberschatz, Korth and Sudarshan


There are various modes in which a data item may be locked. In this section, we
restrict our attention to two modes:
1. Shared. If a transaction Ti has obtained a shared-mode lock (denoted by S)
on item Q, then Ti can read, but cannot write, Q.
2. Exclusive. If a transaction Ti has obtained an exclusive-mode lock (denoted
by X) on item Q, then Ti can both read andwrite Q.

Database System Concepts 15.20 ©Silberschatz, Korth and Sudarshan


Lock-Based Protocols

A lock is a mechanism to control concurrent access to a data item


Data items can be locked in two modes :
1. exclusive (X) mode. Data item can be both read as well as
written. X-lock is requested using lock-X instruction.
2. shared (S) mode. Data item can only be read. S-lock is
requested using lock-S instruction.
Lock requests are made to concurrency-control manager. Transaction
can proceed only after request is granted.

Database System Concepts 15.21 ©Silberschatz, Korth and Sudarshan


Lock-Based Protocols (Cont.)

Lock-compatibility matrix

A transaction may be granted a lock on an item if the requested lock


is compatible with locks already held on the item by other
transactions
Any number of transactions can hold shared locks on an item, but if
any transaction holds an exclusive on the item no other transaction
may hold any lock on the item.
If a lock cannot be granted, the requesting transaction is made to
wait till all incompatible locks held by other transactions have been
released. The lock is then granted.

Database System Concepts 15.22 ©Silberschatz, Korth and Sudarshan


Lock-Based Protocols (Cont.)

Example of a transaction performing locking:


T2: lock-S(A);
read (A);
unlock(A);
lock-S(B);
read (B);
unlock(B);
display(A+B)
Locking as above is not sufficient to guarantee serializability — if A and B get
updated in-between the read of A and B, the displayed sum would be wrong.
A locking protocol is a set of rules followed by all transactions while
requesting and releasing locks. Locking protocols restrict the set of possible
schedules.

Database System Concepts 15.23 ©Silberschatz, Korth and Sudarshan


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 System Concepts 15.24 ©Silberschatz, Korth and Sudarshan


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.
Concurrency control manager can be designed to prevent
starvation.

Database System Concepts 15.25 ©Silberschatz, Korth and Sudarshan


The Two-Phase Locking Protocol

This is a protocol which ensures conflict-serializable schedules.


Phase 1: Growing Phase
transaction may obtain locks
transaction may not release locks
Phase 2: Shrinking Phase
transaction may release locks
transaction may not obtain locks
The protocol assures serializability. It can be proved that the
transactions can be serialized in the order of their lock points (i.e.
the point where a transaction acquired its final lock).

Database System Concepts 15.26 ©Silberschatz, Korth and Sudarshan


The Two-Phase Locking Protocol (Cont.)

Two-phase locking does not ensure freedom from deadlocks


Cascading roll-back is possible under two-phase locking. To avoid
this, follow a modified protocol called strict two-phase locking.
Here a transaction must hold all its exclusive locks till it
commits/aborts.
Rigorous two-phase locking is even stricter: here all locks are
held till commit/abort. In this protocol transactions can be serialized
in the order in which they commit.

Database System Concepts 15.27 ©Silberschatz, Korth and Sudarshan


The Two-Phase Locking Protocol (Cont.)

There can be conflict serializable schedules that cannot be obtained


if two-phase locking is used.
However, in the absence of extra information (e.g., ordering of
access to data), two-phase locking is needed for conflict
serializability in the following sense:
Given a transaction Ti that does not follow two-phase locking, we
can find a transaction Tj that uses two-phase locking, and a
schedule for Ti and Tj that is not conflict serializable.

Database System Concepts 15.28 ©Silberschatz, Korth and Sudarshan


Database System Concepts 15.29 ©Silberschatz, Korth and Sudarshan
Database System Concepts 15.30 ©Silberschatz, Korth and Sudarshan
Time Stamp Ordering Protocol
A timestamp is a tag that can be attached to any transaction or
any data item, which denotes a specific time on which the
transaction or the data item had been used in any way. A
timestamp can be implemented in 2 ways. One is to directly
assign the current value of the clock to the transaction or data
item. The other is to attach the value of a logical counter that
keeps increment as new timestamps are required.
The timestamp of a data item can be of 2 types:
W-timestamp(X):This means the latest time when the data
item X has been written into.
R-timestamp(X):This means the latest time when the data
item X has been read from. These 2 timestamps are updated
each time a successful read/write operation is performed on
the data item X.

Database System Concepts 15.31 ©Silberschatz, Korth and Sudarshan


Multi-Version Schemes of Concurrency
Control
Multi-version protocol minimizes the delay for reading
operation and maintains different versions of data
items.
For each writes operation performed, it creates a new
version of transaction data so that whenever any
transaction performs read operation to read that data
then the appropriate created data version is selected by
the control manager to make that read operation
conflict-free and successful.

Database System Concepts 15.32 ©Silberschatz, Korth and Sudarshan


When the write operation and new version of data is
created then that new version contains some
information that is given below
1.Content: This field contains the data value of that
version.
2.Write_timestamp: This field contains the timestamp of
the transaction whose new version is created.
3.Read_timestamp: This field contains the timestamp of
the transaction of that transaction that will read that
newly created value.

Database System Concepts 15.33 ©Silberschatz, Korth and Sudarshan


Let T1 and T2 be two transactions having timestamp values 15 and 10, respectively.

The transaction T2 calls for a write operation on data (let’s say X) from the database. As T2 calls write

operation, then a new version of data value X is created, which contains the value of X, timestamp of T2,

and timestamp of that transaction which will read X, but in this case, no one is reading the newly created

value so that filed remains empty.

X 10

Now let the transaction T1 (having timestamp 15) call a read operation to read the

newly created value X, then the newly created variable contained will be

X 10 15

Database System Concepts 15.34 ©Silberschatz, Korth and Sudarshan


If timestamp of T2 is less than or equal to timestamp of T1 then

Read(X) operation performed by T1: In this case, content of X is returned to T1.

Write(X) operation performed by T1: In this case, T1 will be rolled back if timestamp of

T1 is smaller than timestamp of read operation on X. And if timestamp of T1 is equal to

the timestamp of write operation on X then the new version is created again with new

contents.

Database System Concepts 15.35 ©Silberschatz, Korth and Sudarshan

You might also like