Module #3 Transaction Concurrency Control and Recovery System
Module #3 Transaction Concurrency Control and Recovery System
This collection of steps must appear to the user as a single, indivisible unit.
Since a transaction is indivisible, it either executes in its entirety or not at all.
• read(X), which 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.
• write(X), which 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.
Transaction Model Cont...
Let Ti be a transaction that transfers $50 from account A to account B. This
transaction can be defined as:
Ti : read(A);
A := A − 50;
write(A);
read(B);
B := B + 50;
write(B).
Properties of the Transactions.
ACID
A -> Atomicity
C -> Consistency
I -> Isolation
D -> Durability
Properties of the Transactions Cont...
Atomicity:
Either all Operations of the transaction are reflected properly in the database or
none.
Consistency:
Isolation:
read(x) -> x is the dataitem, buffer in main memory, used to read the previous
balance/data.
A := A − 50;
write(A);
read(B);
B := B + 50;
write(B).
Atomicity:
Suppose Ti transaction with two accounts A and B, which has $1000 and $2000
respectively.
Simple Transactions Model Cont...
Account A wants to transfer $50 to account B, due to some failure A’s account
get debited but B’s is not get credited ie $950 in A’s account and $2000 is in B’s
account. This state is called inconsistent state.
To make it consistent the transaction will happen successfully, which means A’s
account debit $50 and B’s account get credit $50, so balance amount is $950 and
$2050.
If the system failures in any operation, then it restore the old value from log.
Simple Transactions Model Cont...
Consistency:
Example.
The isolation property does not ensure that a specific transaction will execute
first, only that they will not interfere with each other.
Simple Transactions Model Cont...
Isolation:
1. Serializable
2. Repeatable
3. Read Committed
4. Read Uncommitted.
1. Serializable.
2. Repeatable.
Read allows transactions to be accessed once that transaction has started, even
Simple Transactions Model Cont...
Isolation:
3. Read Committed.
Allow the data to be accessed after the data has been committed to the database,
but not before then.
4. Read Uncommitted.
It must be the case that no system failure can result in a loss of data
corresponding to this transfer of funds.
We assume for now that a failure of the computer system may result in loss of
data in main memory, but data written to disk are never lost.
1. The updates carried out by the transaction have been written to disk before
transaction completes.
2. 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 restart after failure.
Transactions Process.
Transactions States.
During its execution a transaction must be in any one of the following state.
Five different states are: Active State, Partially Committed state, Failed State,
Committed state and Terminated.
Transactions States Cont....
Active State:
The transaction starts executing from the first instruction begin_transaction, the
transaction will be consider in active state. During this state it performs
operations READ and WRITE on some data item.
A transaction goes into the partially committed state after the end of a
transaction.
Committed State:
When the transaction is committed to state, it has already completed its execution
successfully. Moreover, all of its changes are recorded to the database
permanently.
Transactions States Cont....
Failed State:
A transaction considers failed when any one of the checks fails or if the
transaction is aborted while it is in the active state.
Aborted State:
After the failed state, all the changes mades by the transaction has to rolled back
and the database has to be restored to its state prior to the start of the
transaction.
If these actions are completed by the DBMS then the transaction consider to be in
aborted state.
Transactions States Cont....
Example:
READ(A);
A:=A-500;
WRITE(A);
Transactions States Cont....
READ(B);
B:=B+500;
WRITE(B);
COMMIT;
END TRANSACTION;
★ Active state:
★ Failed state:
★ Aborted state:
Undo the changes made so far, the old consistent values of data items A
and B are restored.
Transactions States Cont....
★ Committed:
If it successfully write a new values of A and B into log file or stable storage,
then the transaction is said to be committed state.
➔ Schedule.
➔ Serial Schedule.
Schedule:
Serial Schedule:
Concepts of Serializability:
Let,
If I and J refer to different data items, then we can swap I and J without
affecting the results and instruction in the schedule.
However if I and J refer to the same data item Q, then the order of the two
steps may matter.
We are dealing with only read and write instructions, there fair case that
need to consider
Serializability Cont...
1. I = read(Q), J = read(Q). The order I and J does not matter,since the same
value of Q is read by Ti and Tj.
2. I = read(Q), J = write(Q). If I comes before J, then Ti does not read the value of
Q that it written by Tj in instruction J.
Suppose A and B want to book the flight for their vacation. A open airlines
website to check the availability and cost of the ticket. There is only one ticket is
available. A finds it expensive and looks for other airlines fares if she gets any
offers.
Meanwhile, B logins to the same airline portal and book the ticket. Now, there is
no more flight ticket available.
A does not find any good offer on other airlines portal, she comes back to the
previous airline portal. And tried to book a flight ticket even it is not available.
This conflict is called (RW) Conflict
Examples:
Write - Write Conflict:
A and B share common Google-sheet online. Both read the file. A updates the
document and forgets to save the file. On another end, B updates the Google sheet
and save the file.
Concurrency control is used to address such conflicts which mostly occur with a
multi-user system. It helps you to make sure that database transactions are
performed concurrently without violating the data integrity of respective
databases.
Concurrency Control Cont...
★ Lost Updates
★ dirty read
★ Non-Repeatable Read
Types of Locks:
Versions of Locks:
T obtain locks
T release locks
Example:
Concurrency Control Techniques Cont...
Concurrency Control Techniques Cont...
Schedule 1:
Concurrency Control Techniques Cont...
Deadlock.
In a multi-process system, deadlock is an unwanted situation that arises in a shared
resource environment, where a process indefinitely waits for a resource that is held by
another process.
For example, assume a set of transactions {T0, T1, T2, ...,Tn}. T0 needs a resource X
to complete its task. Resource X is held by T1, and T1 is waiting for a resource Y,
which is held by T2. T2 is waiting for resource Z, which is held by T0. Thus, all the
processes wait for each other to release resources. In this situation, none of the
processes can finish their task. This situation is known as a deadlock.
Deadlocks are not healthy for a system. In case a system is stuck in a deadlock, the
transactions involved in the deadlock are either rolled back or restarted.
Deadlock Cont...
Deadlock Cont...
Necessary Condition:
There are two principal methods for dealing with the deadlock problems.
1. Deadlock Prevention
2. Deadlock Detection and Recovery
1. Deadlock Prevention:
If it finds that a deadlock situation might occur, then that transaction is never
allowed to be executed.
i) Ensure that no cyclic waits can occur by ordering the requests for lock.
1. Advance Locking
2. Ordering dataitem
1. Wait-die:
If a transaction requests to lock a resource (data item), which is already held with
a conflicting lock by another transaction, then one of the two possibilities may occur −
● If TS(Ti) < TS(Tj) − that is Ti, which is requesting a conflicting lock, is older than T j −
then Ti is allowed to wait until the data-item is available.
● If TS(Ti) > TS(tj) − that is Ti is younger than Tj − then Ti dies. Ti is restarted later with a
random delay but with the same timestamp.
This scheme allows the older transaction to wait but kills the younger one.
Deadlock Cont...
2. Wound-Wait Scheme
If a transaction requests to lock a resource (data item), which is already held with
conflicting lock by some another transaction, one of the two possibilities may occur −
● If TS(Ti) < TS(Tj), then Ti forces Tj to be rolled back − that is Tiwounds Tj. Tj is
restarted later with a random delay but with the same timestamp.
● If TS(Ti) > TS(Tj), then Ti is forced to wait until the resource is available.
This scheme, allows the younger transaction to wait; but when an older transaction
requests an item held by a younger one, the older transaction forces the younger one to
abort and release the item.
In both the cases, the transaction that enters the system at a later stage is aborted.
Deadlock Cont...
TimeOut Based:
Transaction is wait for fix time for any dataitem (resource), if transaction is fails to
get the results in a given time, then transaction is rolled back and it restart after
sometime.
Deadlock Detection:
The way to detect the deadlock is wait for graph. G=(V, E)
G -> Graph, V -> Vertices representation the ‘T’ E -> Set of Edges
In this method a graph is drawn based on the transaction and their lock on the resource. If the graph created has a
closed loop, then there is a deadlock. In DBMS maintains this graph for all the transactions waiting for the resources
and checks if there is a loop.
Suppose T1 and T2 are two transactions. Suppose T1 is requesting for a resource R which is held by T2. In this
case, wait for graph draws an arrow from T1 to T2. If T2 releases the resource R, then this arrow is deleted.
Deadlock Cont...
Deadlock Recovery:
Suppose T1 is the victim, then we have to kill that, then T2 will successfully get
execute. This means we can recover from the deadlock.
★ Cost of ‘T’
★ How many more ‘T’ would be affected and its rollback.
★ How many data items has locked
★ How much time it need to complete.
Deadlock Cont...
Deadlock Recovery:
2. Roll back:
Partial Rollback: It says we have selected the victim but we have rollback upto
the certain point, so that it can break the deadlock.
1. Logical error.
2. System error.
Recovery System Cont...
System Crash:
Disk Failure:
Hard disk drive, Permanent storage, Temporary storage and any other disk
failure.
Storage Structure:
A log is the most widely used recording database modification technique. The log is a
structure used for recording database modification. It is a sequence of log record
recording all the update activities in the database. We can use this log to recover from
failure and to bring back the database to the consistent state. An update log record
contains various fields:
1. Undo(Ti): Restores the values of all data item updated by transaction Ti to the old
values.
2. Redo(Ti): Sets the value of all data item updated by transaction Ti to the new values.
We need to undo a transaction T only when log contains the record <T start> only
when log contains the record <start> and <T commit> both.
Recovery System Cont...
Transaction modification technique
There are mainly two techniques to ensure the transaction atomicity despite failure are as
follow:
<To start>
<To commit>
<T1 start>
<Ti commit>
Recovery System Cont...
2) Deferred modification technique
Example
(A) (B)
<To commit>
<T1 start>
<T1 C, 600>
If a system fails just after the log record for the step write(B) of transaction To. The during recovery no redo
operation will be done as we have only <To start> in log record but not <To commit>.
Recovery System Cont...
If a system crash occurs just after the log record write C. the during recovery only redo (To) is done as we have only
<To start> and <To commit> in log disk. At the same time we have <T1 start> in log disk but not <T1
commit> so redo (T1) will not be done.
(C)
<To start>
<To A , 950>
<To B, 2050>
<To commit>
<T1 start>
<T1 C, 600>
<T1 commit>
If a system crash occurs just after the log record <T1 commit> the during the recovery we will perform both redo (T0) and
redo (TI) as we have both <To start> <To commit> and <T1 start>, <T1 commit> in log disk.
Recovery System Cont...
Check points:
When a system crash occurs, we must consult the log to determine those transactions
that need to be redone and those that need to be undone. In principle, we need to
search the entire log to determine this information. There are two major difficulties
with this approach:
1. The search process is time-consuming.
2. Most of the transactions that, according to our algorithm, need to be redone
have already written their updates into the database. Although redoing them will
cause no harm, it will nevertheless cause recovery to take longer.
To reduce these types of overhead, we introduce checkpoints.
Note:
A. Does not permit any updates to be performed while the checkpoint operation is in
progress.
B. Outputs all modified buffer blocks to disk when the checkpoint is performed.
Recovery System Cont...
A checkpoint is performed as follows:
1. Output onto stable storage all log records currently residing in main memory.
2. Output to the disk all modified buffer blocks.
3. Output onto stable storage a log record of the form <checkpoint L>, where
L is a list of transactions active at the time of the checkpoint.
Transactions are not allowed to perform any update actions, such as writing to a
buffer block or writing a log record, while a checkpoint is in progress.
The presence of a <checkpoint L> record in the log allows the system to
streamline its recovery procedure. Consider a transaction Ti that completed prior
to the checkpoint. For such a transaction, the <Ti commit> record (or < Ti abort>
record) appears in the log before the <checkpoint> record. Any database
modifications made by Ti must have been written to the database either prior to the
Recovery System Cont...
checkpoint or as part of the checkpoint itself. Thus, at recovery time, there is no
need to perform a redo operation on Ti .
After a system crash has occurred, the system examines the log to find the last
<checkpoint L> record (this can be done by searching the log backward, from
the end of the log, until the first <checkpoint L> record is found).
a. Whenever it finds a log record belonging to a transaction in the undo list, it performs undo
actions just as if the log record had been found
Recovery System Cont...
during the rollback of a failed transaction.
b. When the system finds a <Ti start> log record for a transaction Ti in
undo-list, it writes a <Ti abort> log record to the log, and removes Ti
from undo-list.
c. The undo phase terminates once undo-list becomes empty, that is, the
system has found <Ti start> log records for all transactions that were
initially in undo-list.
After the undo phase of recovery terminates, normal transaction processing
can resume.
Recovery System Cont...
Recovery System Cont...
Recovery System Cont...
Shadow Paging: