Chapter-6 - Transactions-Concurrency and Recovery
Chapter-6 - Transactions-Concurrency and Recovery
Chapter-6 - Transactions-Concurrency and Recovery
Collections of operations that form a single logical unit of work are called transactions.
Example:
a transfer of funds from one account to another account is a single operation from the customer’s
standpoint; within the database system, however, it consists of several operations.
Transaction Definition
A transaction is a unit of program execution that accesses and possibly updates various data
items.
A transaction is delimited by statements (or function calls) of the form begin transaction and end
transaction.
The transaction consists of all operations executed between the begin transaction and end
transaction.
PROPERTIES OF TRANSACTIONS (ACID PROPERTIES)
1. A (ATOMICITY)
Either all operations of the transaction are reflected properly in the database, or none are.
As a collection of steps, transaction must appear to the user as a single, indivisible unit.
Thus, if a transaction begins to execute but fails for whatever reason, any changes to the database
that the transaction may have made must be undone.
2. C (CONSISTENCY)
Execution of a transaction in isolation (that is, with no other transaction executing concurrently)
preserves the consistency of the database.
3. I (ISOLATION)
Since a transaction is a single unit, its actions cannot appear to be separated by other database
operations not part of the transaction.
Even though multiple transactions may execute concurrently, the system guarantees that, for
every pair of transactions T1 and T2, it appears to T1 that either T2 finished execution before T1
started or T2 started execution after T1 finished. Thus, each transaction is unaware of other
transactions executing concurrently in the system.
Even a single SQL statement involves many separate accesses (read/write) to the database, and a
transaction may consist of several SQL statements.
Therefore, the database system must take special actions to ensure that transactions operate
properly without interference from other concurrently executing database statements.
4. D (DURABILITY)
After a transaction completes successfully, the changes it has made to the database persist, even
if there are system failures.
Even if the system ensures correct execution of a transaction, this serves little purpose if the system
subsequently crashes and, as a result, the system “forgets” about the transaction.
Thus, a transaction’s actions must persist across crashes. This property is referred to as
durability.
TRANSACTION MANAGEMENT
Transaction management component ensures that the database remains in a consistent (correct)
state despite system failures (e.g., power failures and operating system crashes) and transaction
failures.
Concurrency control manager controls the interaction among the concurrent transactions, to
ensure the consistency of the database.
Transaction Model
Consider a simple bank application consisting of several accounts and a set of transactions that
access and update those accounts.
it transfers the data item X from the database to a variable, also called X, in a buffer in main
memory belonging to the transaction that executed the read operation.
it transfers the value in the variable X in the main-memory buffer of the transaction that executed
the write to the data item X in the database.
Example: transaction
Consider an account A with ₹ 950 as balance while account B with ₹ 1050 as balance.
T1: read(A);
A := A − 50;
write(A);
read(B);
B := B + 50;
write(B).
ACID PROPERTIES
Consistency:
As a consistency requirement, the sum of A and B before an execution of the transaction and
after the execution of the transaction should be unchanged.
should be unchanged.
Atomicity:
Suppose that, during the execution of transaction T1, a failure occurs that prevents T1 from
completing its execution successfully. Further, suppose that the failure happened after the write(A)
operation but before the write(B) operation.
In this case, the values of accounts A and B reflected in the database are ₹900 and ₹1050 (instead
of ₹900 and ₹1100). The system destroyed ₹50 as a result of this failure.
Thus, because of the failure, the state of the system no longer reflects a real state of the world that
the database is supposed to capture. We term such a state an inconsistent state. We must ensure
that such inconsistencies are not visible in a database system.
If the atomicity property is present, all actions of the transaction are reflected in the database, or
none are.
Ensuring atomicity is handled by a component of the database called the recovery system.
Durability:
Once the execution of the transaction completes successfully, and the user who initiated the
transaction has been notified that the transfer of funds has taken place, it must be the case that no
system failure can result in a loss of data corresponding to this transfer of funds.
The durability property guarantees that, once a transaction completes successfully, all the
updates that it carried out on the database persist, even if there is a system failure after the
transaction completes execution.
• The updates carried out by the transaction have been written to disk before the transaction
completes.
• Information about the updates carried out by the transaction and written to disk is sufficient to
enable the database to reconstruct the updates when the database system is restarted after the
failure.
Isolation:
If several transactions are executed concurrently, their operations may interleave in some
undesirable way, resulting in an inconsistent state.
For example, the database is temporarily inconsistent while the transaction to transfer funds from
A to B is executing, with the deducted total written to A and the increased total yet to be written
to B. If a second concurrently running transaction reads A and B at this intermediate
point and computes A+B, it will observe an inconsistent value. Furthermore, if this second
transaction then performs updates on A and B based on the inconsistent values that it read, the
database may be left in an inconsistent state even after both transactions have completed.
The isolation property of a transaction ensures that the concurrent execution of transactions
results in a system state that is equivalent to a state that could have been obtained had these
transactions executed one at a time in some order.
Partially
Committe
committed d
Failed Aborted
When it finishes its final statement, it enters the partially committed state.
At this point, the transaction has completed its execution, but it is still possible that it may have to
be aborted, since the actual output may still be temporarily residing in main memory, and thus a
hardware failure may preclude its successful completion.
The database system then writes out enough information to disk that, even in the event of a failure,
the updates performed by the transaction can be re-created when the system restarts after the
failure. When the last of this information is written out, the transaction enters the committed state.
A transaction enters the failed state after the system determines that the transaction can no longer
proceed with its normal execution (for example, because of hardware or logical errors). Such a
transaction must be rolled back. Then, it enters the aborted state.
At this point, the system has two options: restart the transaction or kill the transaction.
CONCURRENT EXECUTIONS
• A transaction consists of many steps. Some involve I/O activity; others involve CPU
activity. The CPU and the disks in a computer system can operate in parallel.
Therefore, I/O activity can be done in parallel with processing at the CPU. This
increases the throughput of the system—that is, the number of transactions executed
in a given amount of time.
• Correspondingly, the processor and disk utilization also increase; that is , the processor
and disk spend less time idle, or not performing any useful work.
• Reduced waiting time for transactions: short transactions need not wait behind long ones.
Moreover, it also reduces the average response time, that is , the average time for a transaction
to be completed after it has been submitted.
Schedule is a sequence of instructions that specify 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.
• A transaction that successfully completes its execution will have a commit instruction as the
last statement.
• A transaction that fails to successfully complete its execution will have an abort instruction
as the last statement.
• Serial
• Concurrent
Let T1 and T2 be two transactions that transfer funds from one account to another.
It is defined as:
T1: read(A);
A := A − 50;
write(A);
read(B);
B := B + 50;
write(B).
Transaction T2 transfers 10 percent of the balance from account A to account B.
It is defined as:
T2: read(A);
temp := A * 0.1;
A := A − temp;
write(A);
read(B);
B := B + temp;
write(B).
Schedule-1
Suppose the current values of accounts A and B are ₹1000 and ₹2000, respectively.
Suppose also that the two transactions are executed one at a time in the order T1 followed by T2.
This execution sequence appears as in schedule-1,
The final values of accounts A and B, after the execution T1 and T2 takes place, are ₹855 and
₹2145, respectively. Thus, the total amount of money in accounts A and B, that is, the sum A + B
is preserved after the execution of both transactions.
Schedule-2
Similarly, if the transactions are executed one at a time in the order T2 followed by T1, then the
corresponding execution sequence is as in schedule-2,
Again, the sum A + B is preserved, and the final values of accounts A and B are ₹850 and ₹2150,
respectively.
Each serial schedule consists of a sequence of instructions from various transactions, where the
instructions belonging to one single transaction appear together in that schedule.
CONCURRENT SCHEDULE
• When the database system executes several transactions concurrently, the corresponding
schedule no longer needs to be serial.
• If two transactions are running concurrently, the operating system may execute one
transaction for a little while, then perform a context switch, execute the second transaction
for some time, and then switch back to the first transaction for some time, and so on.
• With multiple transactions, the CPU time is shared among all the transactions.
The concurrency-control component of the database system ensures that any schedule that is
executed will leave the database in a consistent state.
After this execution takes place, it will give same state as the one in which the transactions are
executed serially in the order T1 followed by T2 (schedule-1).
In Schedules 1, 2 and 3, the sum A + B is preserved. But not all concurrent executions result in a
correct (consistent) state.
Consider a schedule-4 as
After the execution of this schedule, we arrive at a state where the final values of accounts A and
B are ₹950 and ₹2100, respectively.
This final state is an inconsistent state, since we have gained ₹50 in the process of the concurrent
execution.
If control of concurrent execution is left entirely to the operating system, many possible schedules,
including ones that leave the database in an inconsistent state,
SERIALIZABILITY
We can ensure consistency of the database under concurrent execution by making sure that any
schedule that is executed has the same effect as a schedule that could have occurred without any
concurrent execution. That is, the schedule should, in some sense, be equivalent to a serial
schedule. Such schedules are called serializable schedules.
1. Conflict serializability
2. View serializability
We assume that transactions may perform arbitrary computations on data in local buffers in
between reads and writes.
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.
Schedule-3
can be represented as
Consider a schedule S in which there are two consecutive instructions, instructions I1 and I2, of
transactions T1 and T2 respectively (I1 ≠ I2). If I1 and I2 refer to different data items, then we can
swap I1 and I2 without affecting the results of any instruction in the schedule. However, if I1 and
I2 refer to the same data item Q, then the order of the two steps may matter.
With only read and write instructions, there are four cases that we need to consider:
T1 T2 Conflict or not?
I1 = read(Q) I2 = read(Q) I1 and I2 don’t conflict
Thus, only in the case where both I1 and I2 are read instructions, the order of their execution
does not matter.
Thus, I1 and I2 conflict if they are instructions in different transactions on the same data item,
and at least one of these instructions is a write operation.
In Schedule-3
The write(A) instruction of T1 conflicts with the read(A) instruction of T2 as they are instructions
in different transactions on the same data item, and one of these instructions is a write
operation.
However, the write(A) instruction of T2 does not conflict with the read(B) instruction of T1,
because the two instructions access different data items.
NONCONFLICTING INSTRUCTIONS
If I1 and I2 are instructions of different transactions and I1 and I2 do not conflict, then we can swap
the order of I1 and I2 to produce a new schedule S’.
S is equivalent to S’, since all instructions appear in the same order in both schedules except for
I1 and I2, whose order does not matter.
In Schedule-3
Since the write(A) instruction of T2 in schedule-3 does not conflict with the read(B) instruction
of T1, we can swap these instructions to generate an equivalent schedule, schedule-5.
Schedule-3 is equivalent schedule-5 and both produce the same final system state.
The final result of these swaps, schedule 6 is a serial schedule (same as schedule-1)
Schedule 3
Schedule 5
CONFLICT SERIALIZABLE SCHEDULE
schedule-3
Schedule-1
VIEW SERIALIZABLE SCHEDULE
Precedence graph
Precedence graph is simple and efficient method for determining conflict serializability of a
schedule.
Consider a schedule S.
where
E is a set of edges.
The set of vertices consists of all the transactions participating in the schedule.
Above precedence graph contains the single edge T1 → T2, since all the instructions of T1 are
executed before the first instruction of T2 is executed.
Above precedence graph shows the precedence graph for schedule 2 with the single edge T2 →
T1, since all the instructions of T2 are executed before the first instruction of T1 is executed.
The precedence graph contains the edge T1 →T2, because T1 executes read(A) before T2 executes
write(A). It also contains the edge T2→T1, because T2 executes read(B) before T1 executes
write(B).
TESTING FOR SERIALIZABILITY
If the precedence graph for S has a cycle, then schedule S is not conflict serializable.
For example, the precedence graph for schedule-4 as shown below and has a cycle,
LOCK-BASED PROTOCOLS
One way to ensure isolation is to require that data items be accessed in a mutually exclusive
manner; that is, while one transaction is accessing a data item, no other transaction can
modify that data item.
The most common method used to implement this requirement is to allow a transaction to access
a data item only if it is currently holding a lock on that item.
Locks
2. Exclusive - (denoted by X)
The use of these two lock modes allows multiple transactions to read a data item but limits
write access to just one transaction at a time.
Every transaction request a lock in an appropriate mode on data item Q, depending on the types
of operations that it will perform on Q.
The transaction can proceed with the operation only after the concurrency-control manager grants
the lock to the transaction.
Let A and B be two accounts that are accessed by transactions T1 and T2.
Transaction T2 displays the total amount of money in accounts A and B that is, sum (A + B).
Suppose that the values of accounts A and B are ₹100 and ₹200, respectively. If these two
transactions are executed serially, either in the order T1, T2 or the order T2, T1, then transaction
T2 will display the value ₹300.
CONCURRENCY CONTROL
• Timestamp-based protocols
In any transaction, the point in the schedule where the transaction has obtained its final lock (the
end of its growing phase) is called the lock point of the transaction.
Lock Table
Dark rectangles indicate granted locks while light colored ones indicate waiting requests.
Lock table records the type of lock (exclusive or shared) granted or requested.
New request is added to the end of the queue of requests for the data item, and granted if it is
compatible with all earlier locks.
Unlock requests result in the request being deleted, and later requests are checked to see if they
can now be granted.
If transaction aborts, all waiting or granted requests of the transaction are deleted.
TIMESTAMP-BASED PROTOCOLS
A timestamp-ordering is a method for determining the serializability order to select an ordering
among transactions in advance.
Timestamps
With each transaction T1 in the system, we associate a unique fixed timestamp, denoted by TS
(T1). This timestamp is assigned by the database system before the transaction T1 starts execution.
If a transaction T1 has been assigned timestamp TS(T1 ), and a new transaction T2 enters the
system, then TS(T1 ) < TS(T2) that is, T1 is older than T2.
Thus, if TS(T1 ) < TS(T2), then the system must ensure that the produced schedule is equivalent
to a serial schedule in which transaction T1 appears before transaction T2 .
To implement this scheme, we associate with each data item Q, two timestamp values:
• W-timestamp(Q) denotes the largest timestamp of any transaction that executed write(Q)
successfully.
• R-timestamp(Q) denotes the largest timestamp of any transaction that executed read(Q)
successfully.
These timestamps are updated whenever a new read(Q) or write(Q) instruction is executed.
Transaction Transaction
with smaller with larger
timestamp timestamp
The protocol ensures freedom from deadlock, since no transaction ever waits.
DEADLOCK
A system is in a deadlock state if there exists a set of transactions such that every transaction
in the set is waiting for another transaction in the set.
If there exists a set of waiting transactions {T0, T1, . . . , Tn} such that T0 is waiting for a data
item that T1 holds, and T1 is waiting for a data item that T2 holds, and . . . , and Tn−1 is waiting
for a data item that Tn holds, and Tn is waiting for a data item that T0 holds.
None of the transactions can make progress in such a situation.
The only remedy to this undesirable situation is for the system to invoke some drastic action, such
as rolling back some of the transactions involved in the deadlock.
Rollback of a transaction may be partial: that is, a transaction may be rolled back to the point
where it obtained a lock whose release resolves the deadlock.
DEADLOCK HANDLING
There are two principal methods for dealing with the deadlock problem.
• deadlock prevention
• deadlock detection and deadlock recovery
Allows the system to enter a deadlock state, and then try to recover.
Prevention is commonly used if the probability that the system would enter a deadlock state is
relatively high; otherwise, detection and recovery are more efficient.
DEADLOCK PREVENTION
• One approach ensures that no cyclic waits can occur by ordering the requests for locks, or
requiring all locks to be acquired together.
• Each transaction locks all its data items before it begins execution.
The other approach is to perform transaction rollback whenever the wait could potentially result
in a deadlock.
The second approach for preventing deadlocks is to use preemption and transaction rollbacks.
In preemption, when a transaction T2 requests a lock that transaction T1 holds, the lock granted to
T1 may be preempted by rolling back of T1, and granting of the lock to T2.
To control the preemption, we assign a unique timestamp, based on a counter or on the system
clock, to each transaction when it begins. The system uses these timestamps only to decide
whether a transaction should wait or roll back.
• wait–die
• wound–wait
WAIT–DIE SCHEME
• When transaction T1 requests a data item currently held by T2 , T1 is allowed to wait only
if it has a timestamp smaller than that of T2 (that is, T1 is older than T2 ). Otherwise, T1
is rolled back (dies).
Example:
Transactions T1, T2, and T3 have timestamps 5, 10, and 15, respectively.
If T1 requests a data item held by T2, then T1 will wait. (smaller timestamp).
If T3 requests a data item held by T2, then T3 will be rolled back. (larger timestamp).
WOUND–WAIT SCHEME
The wound–wait scheme is a preemptive technique & counterpart to the wait–die scheme.
• When transaction T1 requests a data item currently held by T2 , T1 is allowed to wait only
if it has a timestamp larger than that of T2 (that is, T1 is younger than T2 ). Otherwise, T2
is rolled back (T2 is wounded by T1 ).
Example:
Transactions T1, T2, and T3 have timestamps 5, 10, and 15, respectively.
If T1 requests a data item held by T2, then the data item will be preempted from T2, and T2 will
be rolled back (wound).
The major problem with both of these schemes is that unnecessary rollbacks may occur.
In this approach, a transaction that has requested a lock waits for at most a specified amount of
time. If the lock has not been granted within that time, the transaction is said to time out, and it
rolls itself back and restarts.
If there was in fact a deadlock, one or more transactions involved in the deadlock will time out
and roll back, allowing the others to proceed.
If a system does not employ some protocol that ensures deadlock freedom, then a detection and
recovery scheme must be used.
An algorithm that examines the state of the system is invoked periodically to determine whether
a deadlock has occurred.
If one has, then the system must attempt to recover from the deadlock.
• Provide an algorithm that uses this information to determine whether the system has
entered a deadlock state.
• Recover from the deadlock when the detection algorithm determines that a deadlock
exists.
DEADLOCK DETECTION
Deadlocks in the system can be represented as directed graph called as wait-for graph.
E is a set of edges.
The wait-for graph for transactions T1 and T2 with edge between them can be represented as
T2 T3
T1
T4
Since the graph has no cycle, the system is not in a deadlock state.
Suppose now that transaction T3 is requesting an item held by T4. The edge T3 → T4 is added
to the wait-for graph, resulting in the new system state in figure as,
T2 T3
T1
T4
This time, the graph contains the cycle: T2 →T3 →T4 →T2.
Thus, transactions T2, T3, and T4 are all deadlocked.
When a detection algorithm determines that a deadlock exists, the system must recover from
the deadlock.
The most common solution is to roll back one or more transactions to break the deadlock. Three
actions need to be taken:
1. Selection of a victim
2. Rollback
3. Starvation
1. SELECTION OF A VICTIM -
Given a set of deadlocked transactions, we must determine which transaction (or transactions) to
roll back to break the deadlock.
We should roll back those transactions that will incur the minimum cost.
• How long the transaction has computed, and how much longer the transaction will compute
before it completes its designated task.
• How many more data items the transaction needs for it to complete.
2. ROLLBACK-
Once we have decided that a particular transaction must be rolled back, we must determine how
far this transaction should be rolled back.
• total rollback
• partial rollback
1. Total rollback: It is simplest solution. It aborts the transaction and then restart it.
2. Partial rollback: It rolls back the transaction only as far as necessary to break the deadlock.
Such partial rollback requires the system to maintain additional information about the state of
all the running transactions.
Specifically, the sequence of lock requests/grants and updates performed by the transaction
needs to be recorded.
The deadlock detection mechanism should decide which locks the selected transaction needs to
release in order to break the deadlock.
The selected transaction must be rolled back to the point where it obtained the first of these locks,
undoing all actions it took after that point.
The recovery mechanism must be capable of resuming execution after a partial rollback.
3. STARVATION-
In a system where the selection of victims is based primarily on cost factors, it may happen that
the same transaction is always picked as a victim.
As a result, this transaction never completes its designated task, thus there is starvation.
We must ensure that a transaction can be picked as a victim only a (small) finite number of times.
The most common solution is to include the number of rollbacks in the cost factor.
• disk crash,
• power outage,
• software error,
• In any failure, information may be lost. Therefore, the database system must take actions
in advance to ensure that the atomicity and durability properties of transactions are
preserved.
• An integral part of a database system is a recovery scheme that can restore the database to
the consistent state that existed before the failure.
• The recovery scheme must also provide high availability; that is, it must minimize the
time for which the database is not usable after a failure.
Failure Classification
Following are the various types of failure that may occur in a system:
1. Transaction failure -
There are two types of errors that may cause a transaction to fail:
• Logical error-
The transaction can no longer continue with its normal execution because of some internal
condition, such as bad input, data not found, overflow, or resource limit exceeded.
• System error-
The system has entered an undesirable state (for example, deadlock), as a result of which a
transaction cannot continue with its normal execution. The transaction, however, can be re-
executed at a later time.
2. System crash-
3. Disk failure-
• A disk block loses its content as a result of either a head crash or failure during a data-
transfer operation.
• Copies of the data on other disks, or archival backups on tertiary media, such as DVD or tapes,
are used to recover from the failure.
STORAGE STRUCTURE
The various data items in the database may be stored and accessed in a number of different
storage media.
The different storage media can be distinguished by their relative speed, capacity, and resilience
to failure.
1. Volatile storage
2. Non-volatile storage
3. Stable storage
1. Volatile storage:
2. Nonvolatile storage:
3. Stable storage:
• A failure may occur after one of these modifications have been made but before both of
them are made permanent.
• Modifying the database without ensuring that the transaction will commit, may leave the
database in an inconsistent state.
• Not modifying the database may result in lost updates if failure occurs just after
transaction commits.
1. Actions taken during normal transaction processing to ensure enough information exists to
recover from failures.
2. Actions taken after a failure to recover the database contents to a state that ensures atomicity,
consistency and durability.
RECOVERY TECHNIQUES
• Shadow-paging
Shadow-copy scheme:
In the shadow-copy scheme, a transaction that wants to update the database first creates a
complete copy of the database. All updates are done on the new database copy, leaving the
original copy, the shadow copy, untouched.
If at any point the transaction has to be aborted, the system merely deletes the new copy. The
old copy of the database has not been affected.
The current copy of the database is identified by a pointer, called db-pointer, which is stored on
disk.
If the transaction partially commits (that is, executes its final statement), it is committed as
follows:
• First, the operating system is asked to make sure that all pages of the new copy of the database
have been written out to disk.
• After the operating system has written all the pages to disk, the database system updates the
pointer db-pointer to point to the new copy of the database; the new copy then becomes
the current copy of the database.
• The transaction is said to have been committed at the point where the updated db-pointer is
written to disk.
Shadow copy schemes are commonly used by text editors (saving the file is equivalent to
transaction commit, while quitting without saving the file is equivalent to transaction abort).
Shadow copying can be used for small databases, but copying a large database would be
extremely expensive.
Shadow copy
Shadow Paging
• The scheme uses a page table containing pointers to all pages; the page table itself and
all updated pages are copied to a new location.
• Any page which is not updated by a transaction is not copied, but instead the new page
table just stores a pointer to the original page.
• When a transaction commits, it atomically updates the pointer to the page table, which
acts as db-pointer, to point to the new copy.
• Shadow paging unfortunately does not work well with concurrent transactions and is not
widely used in databases.
The most widely used structure for recording database modifications is the log.
The log is a sequence of log records, recording all the update activities in the database.
Log record indicates that Transaction Ti has performed a write operation on data item Xj ,
where V1 is the value before the write, and V2 is the value after the write.
Once a log record exists, we can output the modification to the database if that is desirable.
When a system with concurrent transactions crashes and recovers, it behaves in the following
manner –
• The recovery system reads the logs backwards from the end to the last checkpoint.
• It maintains two lists, an undo-list and a redo-list.
• If the recovery system sees a log with <Tn, Start> and <Tn, Commit> or just <Tn,
Commit>, it puts the transaction in the redo-list.
• If the recovery system sees a log with <Tn, Start> but no commit or abort log found, it
puts the transaction in undo-list.
All the transactions in the undo-list are then undone and their logs are removed.
All the transactions in the redo-list and their previous logs are removed and then redone before
saving their logs.
Example-Recovery with checkpoints