Module 6
Module 6
Concurrency
Transaction concept, Transaction state
diagram, ACID properties,
Concurrent Execution, Seialzability : Conflict
and view Serializibility
Concurrent Control: Lock-based, timestamp-based
protocols.
Recovery System: Failure Classification, Log based
recovery,ARIES, Checkpoint, Shadow Paging.
Deadlock Hnadling: Avoidance, Prevention,Wait-die
Transaction concept
• Database Transaction is an atomic unit that
contains one or more SQL statements.
• It is a series of operations that performs as a
single unit of work against a database.
• It is a logical unit of work.
• It has a beginning and an end to specify its
boundary.
• Let's take an simple example of bank
transaction, Suppose a Bank clerk transfers Rs.
1000 from X's account to Y's account.
• X's Account
• open-account (X)
prev-balance = X.balance
curr-balance = prev-balance – 1000
X.balance = curr-balance
close-account (X)
• Decreasing Rs. 1000 from X's account, saving new
balance that is current balance and after
completion of transaction the last step is closing
the account.
• Y's Account
• open-account (Y)
prev - balance = Y.balance
curr - balance = prev-balance + 1000
Y.balance = curr-balance
close-account (Y)
• Adding Rs. 1000 in the Y's account and saving
new balance that is current balance and after
completion of transaction the last step is
closing the account.
1. Active
2. Partially Committed
3. Failed
4. Aborted
5. Committed
• 1. Active : Active is the initial state of every
transaction. The transaction stays in Active state
during execution.
2. Partially Committed : Partially committed
state defines that the transaction has executed
the final statement.
3. Failed : Failed state defines that the execution
of the transaction can no longer proceed
further.
4. Aborted : Aborted state defines that the
transaction has rolled back and the database is
being restored to the consistent state.
5. Committed : If the transaction has completed
its execution successfully, then it is said to be
committed.
Transaction Properties
• Following are the Transaction Properties, referred to by an
acronym ACID properties:
1. Atomicity
2. Consistency
3. Isolation
4. Durability
1. If in SH1, T1 reads the initial value of data item, then in SH2 , T1 should
read the initial value of that same data item.
2. If in SH1, T1 writes a value in the data item which is read by T2, then in
SH2, T1 should write the value in the data item before T2 reads it.
3. If in SH1, T1 performs the final write operation on that data item, then
in SH2, T1 should perform the final write operation on that data item.
• 1. Lock-Based Protocol
2. Timestamp Based Protocol
1. Lock-Based Protocol
If the locking is not done properly, then it will display the inconsistent and
corrupt data.
• It manages the order between the conflicting pairs among transactions at
the time of execution.
• There are two lock modes,
1. Shared Lock
2. Exclusive Lock
• 1. Shared Lock(S): Shared lock is placed when we are reading the data,
multiple shared locks can be placed on the data but when a shared lock is
placed no exclusive lock can be placed.
• For example, when two transactions are reading Steve’s account balance, let
them read by placing shared lock but at the same time if another transaction
wants to update the Steve’s account balance by placing Exclusive lock, do not
allow it until reading is finished.
• 2. Exclusive Lock(X): Exclusive lock is placed when we want to read and write
the data. This lock allows both the read and write operation, Once this lock is
placed on the data no other lock (shared or Exclusive) can be placed on the
data until Exclusive lock is released.
• For example, when a transaction wants to update the Steve’s account balance,
let it do by placing X lock on it but if a second transaction wants to read the
data(S lock) don’t allow it, if another transaction wants to write the data(X
lock) don’t allow that either.
So based on this we can create a table like this:
Lock Compatibility Matrix
S X
S True False
X False False
– If R_TS(X) > TS(T) or if W_TS(X) > TS(T), then abort and rollback T and
reject the operation. else,
– Execute W_item(X) operation of T and set W_TS(X) to TS(T).
2 Whenever a Transaction T issues a R_item(X) operation, check
the following conditions:
– If W_TS(X) > TS(T), then abort and reject T and reject the operation,
else
– If W_TS(X) <= TS(T), then execute the R_item(X) operation of T and set
R_TS(X) to the larger of TS(T) and current R_TS(X).
• One drawback of Basic TO protocol is that
it Cascading Rollback is still possible. Suppose
we have a Transaction T1 and T2 has used a
value written by T1. If T1 is aborted and
resubmitted to the system then, T2 must also
be aborted and rolled back. So the problem of
Cascading aborts still prevails.
Strict Timestamp Ordering –
Hold and Wait states that a process is holding a resource, requesting for
additional resources which are being held by other processes in the
system.
Circular Wait states that one process is waiting for a resource which is
being held by second process and the second process is waiting for the
third process and so on and the last process is waiting for the first process.
It makes a circular chain of waiting.
Deadlock Prevention
• Deadlock Prevention ensures that the system never enters a deadlock state.
2. No Hold and Wait : Removing hold and wait condition can be done if a
process acquires all the resources that are needed before starting out.
4. Removing Circular Wait : The circular wait can be removed only if the
resources are maintained in a hierarchy and process can hold the resources in
increasing the order of precedence.
• Wait-Die Scheme
• In this scheme, 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 Tj − 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 Avoidance
1. In Deferred Database Modification, all the logs for the transaction are
created and stored into stable storage system. In the above example, three
log records are created and stored it in some storage system, the database
will be updated with those steps.