Dbms - Module 3
Dbms - Module 3
TRANSACTIONS CONCEPTS
Introduction
A transaction is a unit of program execution that accesses and possibly updates
various data items.
The transaction consists of all operations executed between the statements begin
and end of the transaction
Transaction operations: Access to the database is accomplished in a transaction
by the following two operations:
read (X): Performs the reading operation of data item X from the database
write (X): Performs the writing operation of data item X to the database
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, e.g. hardware failures and system crashes
Concurrency, for simultaneous execution of multiple transactions
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
Durability: After a transaction completes successfully, the changes it has made
to the database persist, even if there are system failures
Example of Fund Transfer: Let Ti be a transaction that transfers 50 from
account A to B. This transaction can be illustrated as follows
1
DBMS - Module III - TRANSACTION PROCESSING
Transfer $50 from account A to B:
Ti : read(A)
A := A – 50
write(A)
read(B)
B := B + 50
write(B)
2
DBMS - Module III - TRANSACTION PROCESSING
Concurrency control schemes: these are mechanisms to achieve isolation
to control the interaction among the concurrent transactions in order to
prevent them from destroying the consistency of the database
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
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.
The following concurrent schedule (Schedule 4 in the book) does not preserve the
value of the the sum A + B
3
DBMS - Module III - TRANSACTION PROCESSING
Serializable Schedule
A serializable schedule over a set S of committed transactions is a schedule whose
effect on any consistent database is guaranteed to be identical to that of some
complete serial schedule over S. i.e., even though the actions of transactions are
interleaved, the result of executing transactions serially in different order may
produce different results.
Example: The schedule shown in the following figure is serializable.
T1 T2
R(A)
W(A)
R(A)
W(A)
R(B)
W(B)
R(B)
W(A)
Commit
Commit
Even though the actions of T1 and T2 are interleaved, the result of this schedule is
equivalent to first running T1 entirely and then running and T2 entirely. Actually
T1‘s read and write of B is not influenced by T2‘s actions on B, and the net effect is
the same if these actions are the serial schedule First T1, then T2. This schedule is
also serializable if first T2, then T1. Therefore if T1 and T2 are submitted
concurrently to a DBMS, either of these two schedules could be chosen as first
A DBMS might sometimes execute transactions which is not a serial execution i.e.,
not serializable.
This can be happen for two reasons:
First the DBMS might use a concurrency control method that ensures the
executed schedule itself.
Second, SQL gives programmers the authority to instruct the DBMS to
choose non-serializable schedule.
4
DBMS - Module III - TRANSACTION PROCESSING
Anomalies due to Interleaved execution
There are three main situations when the actions of two transactions T1 and T2
conflict with each other in the interleaved execution on the same data object.
Write-Read (WR) Conflict: Reading Uncommitted data.
Read-Write (RW) Conflict: Unrepeatable Reads
Write-Write (WW) Conflict: Overwriting Uncommitted Data.
Reading Uncommitted Data (WR Conflicts)
Dirty Read: The first source of anomalies is that a transaction T2 could
read a database object A that has been just modified by another transaction
T1, which has not yet committed, such a read is called a dirty read.
Example: Consider two transactions T1 and T2, where T1 stands for
transferring $100 from A to B and T2 stands for incrementing both A and B
by 6% of their accounts. Suppose that their actions are interleaved as
follows:
(i) T1 deducts $100 from account A, then immediately
(ii) T2 reads accounts of A and B adds 6% interest to each, and then,
(iii) T1 adds $100 to account B.
This corresponding schedule is illustrated as follows:
T1 T2
R(A)
A: = A -100
W(A)
R(A)
A: = A + 0.06 A
W(A)
R(B)
B:= B+.06 B
W(B)
R(B) Commit
B: = B + 100
W(B)
Commit
The problem here is T2 has added incorrect 6% interest to each A and B. Because
before commitment that $100 is deducted from A, it has added 6% to account A
before commitment that $100 is credited to B, it has added 6% to account B. thus,
the result of this schedule is different from the result of the other schedule which
is serializable: first T1 then T2.
5
DBMS - Module III - TRANSACTION PROCESSING
situation could not arise in a serial execute of two transactions: this, it is called as
unrepeatable read.
Example: Suppose that both T1 and T2 reads the same value of A, say 5. Then T1
has incremented A value to 6 but before commitment as A value 6, T2 has
decremented A value from 5 to 4. Thus, instead of answer of A value as 5, i.e., from
to 5 we got an answer 4 which is incorrect.
Serializability
Basic Assumption – Each transaction, on its own, preserves database consistency
i.e. serial execution 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 conflict
serializability and view serializability
Simplifying assumptions:
ignore operations other than read and write instructions
assume that transactions may perform arbitrary computations on data in
local buffers between reads and writes
simplified schedules consist only of reads and writes
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 ordering
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:
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, where Q is a data item and Ti
is a transaction:
1. If Ti reads the initial value of Q in schedule S, then Ti must, in schedule
S´, also read the initial value of Q
2. If 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. The transaction (if any) that performs the final write(Q) operation in
schedule S (for any data item Q) must perform the final write(Q) operation
in schedule S´
NB: View equivalence is also based purely on reads and writes
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 book) — a schedule which is view-serializable but not conflict
serializable
Every view serializable schedule that is not conflict serializable has blind writes
8
DBMS - Module III - TRANSACTION PROCESSING
Concurrency Control
What is Concurrency?
Concurrency in terms of databases means allowing multiple users to access the data
contained within a database at the same time. If concurrent access is not managed by the
Database Management System (DBMS) so that simultaneous operations don't interfere
with one another problems can occur when various transactions interleave, resulting in an
inconsistent database.
10
DBMS - Module III - TRANSACTION PROCESSING
Needs of Concurrency Control
Timestamp
A monotonically increasing variable (integer) indicating the age of an operation
or a transaction. A larger timestamp value indicates a more recent event or
operation.
Timestamp based algorithm uses timestamp to serialize the execution of
concurrent transactions.
If read_TS(X) > TS(T) or if write_TS(X) > TS(T), then an younger transaction has
already read the data item so abort and roll-back T and reject the operation.
If the condition in part (a) does not exist, then execute write_item(X) of T and set
write_TS(X) to TS(T).
If write_TS(X) > TS(T), then an younger transaction has already written to the
data item so abort and roll-back T and reject the operation.
If write_TS(X) TS(T), then execute read_item(X) of T and set read_TS(X) to the
larger of TS(T) and the current read_TS(X).
3. Strict Timestamp Ordering :
Transaction T issues a write_item(X) operation:
If TS(T) > read_TS(X), then delay T until the transaction T’ that wrote or read X has
terminated (committed or aborted).
Transaction T issues a read_item(X) operation:
If TS(T) > write_TS(X), then delay T until the transaction T’ that wrote or read X has
terminated (committed or aborted).
11
DBMS - Module III - TRANSACTION PROCESSING
Locking Mechanisms
Lock-Based Concurrency Control
12
DBMS - Module III - TRANSACTION PROCESSING
T1 T2
X(A)
R(A)
A: = A +10
W(A)
X(B)
R(B)
B: = B + 10
W(B)
Commit
X(A)
R(A)
A: = A + 0.20 A
W(A)
X(B)
R(B)
B:= B+.20 B
W(B)
Commit
Using strict 2PL, the transaction first acquire a lock performs the action. However
T2 cannot be interleaved and hence results in correct execution.
Recoverability
Need to address the effect of transaction failures on concurrently running
transactions.
Recoverable schedule: if a transaction Tj reads a data item previously written by a
transaction Ti , the commit operation of Ti appears before the commit operation
of Tj
The following schedule (Schedule 11) is not recoverable if T9 commits
immediately after the read
13
DBMS - Module III - TRANSACTION PROCESSING
If T8 should abort, T9 would have read (and possibly shown to the user) an
inconsistent database state. Hence database must ensure that schedules are
recoverable
Cascading rollback – a single transaction failure leads to a series of transaction
rollbacks
Consider the following schedule where none of the transactions has yet
committed (so the schedule is recoverable)
If T10 fails, T11 and T12 must also be rolled back
Can lead to the undoing of a significant amount of work
In DBMS the concurrency can be controlled without locking also, by using the
following techniques:
Optimistic Concurrency Control
Timestamp-Based Concurrency Control
Multiversion Concurrency Control
So, each transaction can be assigned a timestamp at startup, and we can ensure, at
execution time, that if an action ai of transaction Ti conflicts with action aj of
transaction Tj, ai occurs before aj if TS(Ti)< TS(Tj). If an action violates this
ordering, the transaction is aborted and restarted.
14
DBMS - Module III - TRANSACTION PROCESSING
The timestamp concurrency control is implemented by giving every database
object O a read timestamp RTS(O) and a write timestamp WTS(O). If transaction T
wants to read object O, and TS(T)<WTS(O), the order of the read with respect to
the most recent write on O would violate the timestamp order between this
transaction and the writer. Therefore, T is aborted and restarted with anew,
larger timestamp, if TS(T)> WTS(O), T reads O, and RTS(O) is set to the larger of
RTS(O) and TS(T).
1. If TS(T)<RTS(O), the write actions conflicts with the most recent read action of
O, and T is therefore aborted and restarted.
2. If TS(T)<WTS(O), a simple approach would be to abort T because it writes
action conflicts with the most recent write of O and is out of timestamp order.
However, we can safely ignore such writes and continue. Ignoring outdated
writes is called the Thomas Write Rule.
3. Otherwise, T writes O and WTS(O) is set to TS(T).
Thomas’s Write Rule: As roll back restart doesn’t occur in the time stamp
method, Thomas’s write rule ahs been used.
1. When transaction i wants to write the value of some data item ‘D’ which is
already being read by some younger transaction then it is not possible for
transaction i to write its value hence, it must be aborted, rolled back and
restarted with a new time stamp value.
2. When a transaction i wants to write a new value to some data item ‘D’ on
which the write operation has already been applied by some younger
transaction then the write operation requested by the transaction i is
neglected and is allowed to proceed with its normal execution.
3. Whereas in other operations a transaction; is permitted to continue with its
execution and its write time stamp is changed along with the change in
transactional time stamp.
The time stamp protocol just presented above, permits schedules that are not
recoverable, as illustrated in the following figure:
15
DBMS - Module III - TRANSACTION PROCESSING
If TS(T1) = and TS(T2)=2, this schedule is permitted by the time stamp protocol
(with or without the Thomas write Rule). This timestamp protocol can be
modified to disallow such schedules by buffering all write actions until the
transaction commits.
In the above example, when T1 wants to write A, WTS(A) is updated to reflect this
action, but the change to A is not carried out immediately; instead, it is recorded in
a private workspace, or buffer. When T2 wants to read A, then its timestamp is
compared with WTS (A), and the read is seen to be permissible. However, T2 is
blocked until T1 completes. If T1 commits, its change to A is copies from the
buffer; otherwise, the changes in the buffer are discarded. T2 is then allowed to read A.
Deadlocks
Deadlock is a situation where two or more transactions wait for locks held by
other to be released.
Example: T1 has lock on A and T2 has lock on B. If T1 requests for lock on B by
holding lock on A. Similarly, T2 requests for lock on B. Either T1 nor T2 can
continue with the execution. This is called deadlock.
Deadlocks can be handled in three ways.
i) Time-outs
ii) Deadlock prevention
iii) Deadlock detection and recovery
Timeouts: With this approach, the transaction waits for predefined amount of
time before acquiring the lock. If the time-outs, DBMS assumes that so there
could be a deadlock and aborts the transaction holding the object.
Deadlock prevention: DBMS looks ahead to determine a deadlock. If a
deadlock is predicted, then it aborts the transaction and never allows a
deadlock to occur.
Two algorithms are used for the purpose.
i) Wait-Dies
ii) Wound-Wait.
Deadlock Detection: DBMS waits until a deadlock occurs and then takes
measures to solve the deadlock problem. It constructs a wait- for graph (WFG)
for the purpose.
16
DBMS - Module III - TRANSACTION PROCESSING
Performance of Locking
Long-based techniques use two methods to acquire serializabilty.
i) Blocking
ii) Aborting
If the system reaches the thrash point, the DBA takes effective measures to reduce
the number of transactions.
The following steps are taken to increase throughput:
i) Reducing the situation where two objects request for same lock.
ii) Each transaction should be allowed to hold the lock for a short period of
time such that other transactions are not blocked for a long time.
iii) Avoiding hot spots. A frequently accessed database object is known as hot
spots. This hot spot reduces the system performance drastically.
17
DBMS - Module III - TRANSACTION PROCESSING
Recovery Techniques
A computer system, like any other mechanical or electrical device, is subject to failure.
There are many causes of such failure, such as disk crash, power failure, software
error, etc. In each of these cases, information may be lost. Therefore, the database
system maintains an integral part known as recovery manager, which is responsible
for the restorage of the database to a consistent state that existed prior to the
occurrence of the failures.
The recovery manager of a DBMS is responsible for ensuring transaction atomicity
and durability. It ensures atomicity by undoing the action of transactions, that do not
commit, and durability by making sure that all actions of Data Base Management
Systems 1 committed transactions survive system crashes (example: the central part
of the system is dumped by an error) and media failure (example: a disk is corrupted).
When a DBMS is restarted after crashes, the recovery manager is given control and
must bring the database to a consistent state. The recovery manager is also
responsible for undoing the actions of aborted transactions. „
System/transaction failures
There are two types of errors that may cause a transaction failure:
1. Logical Error: The transaction can no longer continue with its normal execution
with some internal conditions such as bad input, data not found, overflow or resource
limits exceeded.
2. System Error: The system has entered an undesirable state (example: deadlock),
as a result of which a transaction cannot continue with its normal execution. This
transaction can be re-executed at a later time.
3. System Crash: there is a hardware failure or an error in the database software or
the operating system, the causes the loss of the content of temporary storage and
brings transaction processing to a halt. The content of permanent storage remains
same and is not corrupted.
4. Disk Failure: A disk block loses its content as result of either a head crash or
failure brings a data transfer operating. Copies of the data on other disks, or backups
on tapes, are used to recover from the failure.
18
DBMS - Module III - TRANSACTION PROCESSING
commit_transaction: This signals a successful end of the transaction so that
any changes (updates) executed by the transaction can be safely committed to
the database and will not be undone.
rollback (or abort): This signals that the transaction has ended
unsuccessfully, so that any changes or effects that the transaction may have
applied to the database must be undone.
Recovery techniques use the following operators:
undo: Similar to rollback except that it applies to a single operation rather
than to a whole transaction.
redo: This specifies that certain transaction operations must be redone to
ensure that all the operations of a committed transaction have been applied
successfully to the database.
Two Main Techniques
Deferred Update
No physical updates to db until after a transaction commits.
During the commit, log records are made then changes are made
permanent on disk. What if a Transaction fails?
No UNDO required
REDO may be necessary if the changes have not yet been made
permanent before the failure
Immediate Update
Physical updates to db may happen before a transaction commits.
All changes are written to the permanent log (on disk) before changes are
made to the DB. What if a Transaction fails?
After changes are made but before commit – need to UNDO the changes
REDO may be necessary if the changes have not yet been made permanent before
the failure
Recovery based on Deferred Update
Deferred update
Changes are made in memory and after T commits, the changes are made permanent
on disk.
Changes are recorded in buffers and in log file during T’s execution.
At the commit point, the log is force-written to disk and updates are made in database.
No need to ever UNDO operations because changes are never made permanent
REDO is needed if the transaction fails after the commit but before the changes are
made on disk.
Hence the name is NO UNDO/REDO algorithm
19
DBMS - Module III - TRANSACTION PROCESSING
Recovery based on Immediate Update
Immediate update:
Updates to disk can happen at any time
But, updates must still first be recorded in the system logs (on disk) before
changes are made to the database.
Need to provide facilities to UNDO operations which have affected the db
2 flavors of this algorithm
1. UNDO/NO_REDO recovery algorithm
if the recovery technique ensures that all updates are made to the database on
disk before T commits, we do not need to REDO any committed transactions
UNDO/REDO Algorithm
2 lists maintained:
Commit list: committed transactions since last checkpoint
Active list: active transactions
UNDO all write operations of active transactions.
Undone in the reverse of the order in which they were written to the log
REDO all write operations of the committed transactions
In the order in which they were written to the log.
Shadow paging
An alternative to log-based crash-recovery techniques is shadow paging. Shadow
paging may require fewer disk accesses when compared to the log-based recovery
method.
The database is partitioned into some number of fixed-length blocks, which are
referred as pages. The term page is borrowed from operating systems, since we
are using a paging scheme for memory management.
Assume that there are n pages, numbered from 1 to n. (n may be in hundreds or
thousands). These pages are not stored in any particular order on disk. But, there
is a way to find the ith page of the database for any given i, by using a page table, as
illustrated in the following figure.
X Y
X' Y'
Database
Data Base Management Systems
When a transaction starts, both pages are identical. The shadow page table is never
changed over the duration of the transaction. The current page table may be changed
when a transaction performs a write operation. Thus, all input and output operations
use the current page table to locate the database pages on disk.
21