Académique Documents
Professionnel Documents
Culture Documents
Introduction:
A transaction is an executing program that forms a logical unit of the database
processing. A transaction includes one or more database operations like insertion,
deletion, modification or retrieval operations. The transaction boundaries can be specified
by begin transaction and end transaction statements.
Examples of transaction processing systems:
Reservation systems, Credit card processing system, Insurance Processing.
Multiprogramming operating systems execute some commands from one process, then
suspend that process and execute some commands from the next process and so on. A
process is then resumed at the point where it was suspended whenever it gets its turn to
use the CPU .Concurrent execution of processes is actually said to be interleaved.
Interleaving keeps the CPU busy.
Granularity:
The size of the data item is called the granularity. A data item can be a field of some
record in the database, record or disk block.
BLOCK:
The basic unit of data transfer from disk to main memory is one block.A transaction
includes two basic database access operations.
1. read_item(X):
It reads a database item named X into a program variable. It works as follows,
1. The address of the disk block that contain item X is found.
2. The disk block is copied into a buffer in main memory.
3. The item X is copied from the buffer to the program variable named X.
2. write_item(X):
It writes the value of the program variable X into the database item X. It works as
follows,
1. The address of the disk block that contains item X is found.
2. The disk block is copied into a buffer in main memory.
3. The item X is copied from the program variable named X into its correct location in
the buffer.
4. The updated block from the buffer is stored back to disk either immediately or at
sometime later.
Concurrency control and recovery mechanisms are mainly concerned with the database
access commands in a transaction.Problems that can occur are,
Transactions execute concurrently and may access and update the same database items.
Database access commands in a transaction can occur concurrently.
Database failures:
Failures can be Transaction failure, Media failure or System failure.
Transaction failures can be due to,
1. Transaction error: This is due to the logical programming error or erroneous
value in the input.
2. Local errors: It may be necessary to cancel a transaction because some
exception condition may occur.
3. Concurrency control enforcement: The concurrency control method may
require a transaction to be aborted or restarted later.
System failures can be due to,
1. Computer crash: This may be due to hardware, software or network
problem.
2. Physical problems and catastrophes : Problems due to power, earthquake,
fire, theft etc.
Media failure can be due to,
1. Disk failure: This is due to read or write malfunction or disk read/write
head crash etc.
Transaction Operations:
Transaction States:
Active State:
When transaction begins it goes into active state where it issues READ and
WRITE operations.
Partially Committed state:
When the transaction ends it moves to this state. Some recovery protocols will ensure
that a system failure will not result in an inability to record the changes of the
transaction permanently.
Committed State:
In this state the transaction has reached its commit point and the changes can be
made permanent to the database.
Failed State:
If any check fails or if transaction is aborted during its active state the transaction
goes to this state.
Terminated State:
This is when transaction leaves the system.
System Log:
It is the DBMS journal to keep track of all the transaction operations to affect the values
of the database items.To identify each transaction there is a unique id.
The entries in the log are as follows,
[start_transaction,T]This indicates that the transaction has started execution.
[write_item,T,X,old_value,new_value]This indicates that T has changed the value of X
from old value to new value.
[Read_item,T,X]This indicates T has read value X
[commit,T]This indicates T has completed successfully.
[abort,T]This indicates T has been aborted.
Atomicity:
2.
3.
Schedules :
When transactions are executing concurrently in an interleaved fashion the order of
execution of operations from different transactions is called the schedule.
Two operations are said to conflict if,
1.They belong to different transactions.
2.They access the same item X.
3.Atleast one of them do a write operation.
Complete schedule:
A schedule S is a complete schedule if,
The operations in Schedule S are exactly those operations in T1,T2....Tn including
a commit or abort operation at the last.
For any pair of operation from the same transaction the order of appearance of
operations in the schedule should be same as in transaction .
For the conflicting operations one of the two must appear before the other in the
schedule.
In this a transaction can neither read or write item X until the last transaction that
has wrote X has committed or aborted.
Serializability of schedules:
Based on serializability they can be classified into three,
1.Serial schedule:
Schedules are serial if the operations of one transaction is consecutively executed
without any interleaved operations from another transaction.
2.Nonserial schedule:
A schedule is non serial if the sequence interleaves operations of other
transactions.
3.Conflict serializable schedule:
A schedule is conflict serializable if it is conflict equivalent to some serial
schedule S.We can reorder the non conflicting operations in S until we form the
equivalent serial schedule S.
Between lock_ item(x) and unlock-item(x) operations the transaction T is holding the
lock on x, therefore allowing only one transaction to hold the lock on an item.
Shared/Exclusive Locks:
Several transactions should be allowed to access the item X if it is for reading purpose
only.If a transaction has to do write operation then it should be given exclusive access to
x.There are 3 lock operations.
1.read_ lock(x)
2.write_lock(x)
3.Unlock(x).
Therefore LOCK(x) can have the above 3 possible states.
The locking operations can be handled by maintaining lock table and keeping track of the
number of transactions holding a shared lock on the item in the lock table.
Each record in the lock table will have 4 fields.
1. Data item name.
2. LOCK.
3. Number of reads.
4. Locking transactions.
The state of the LOCK is write locked or read locked .No interleaving is allowed unless
the transaction started terminates by giving the lock or the transaction is placed on a
waiting queue for the item.
Algorithm for read_lock(x):
B: If LOCK(x)=unlocked then begin LOCK(x)read locked
No_of_reads(x)1
End
Else if LOCK(x)=read locked
then no_of_reads(x)no_of-reads(x)+1
Else begin wait (until LOCK(x)=unlocked and the lock manager wakes up the
transaction)
goto B
End.
Algorithm for write_lock(x):
If LOCK(x)=unlocked then LOCK(x)write locked
Else begin wait (until LOCK(x)=unlocked and the lock manager wakes up the
transaction)
goto B
End.
Algorithm for unlock(x):
If LOCK(x)=write-locked
then begin LOCK(x)=unlocked , wake up one of the waiting transactions, if any
End.
Else if LOCK(x)=read_locked
Begin
No-of-reads(X)no_of_reads(x)-1;
If no_of_reads(x)=0 then begin LOCK(x)=unlocked,wakeup one of the waiting
transaction if any
end
end;
The following rules should be enforced by the Shared/Exclusive Locks,
1) Transaction T must issue the operation read-lock(x) or write_lock(x) before any
read_item(x)operation is performed in T.
2) A transaction T must issue the operation write-lock(x) before any write_item(x)
operation is performed in T.
3) A transaction T must issue the operation unlock(x) after all read_item(x) and
write_item(x) operation is completed in T.
4) A transaction T will not issue a read_lock(x) if it already holds a read lock or
write lock on item x.
5) A transaction T will not issue a write_lock(x) if it already holds a read lock or
write lock on item x.
6) A transaction T will not issue a unlock(x) unless it already holds a read lock or
write lock on item x.
Upgrading the lock:
A transaction T can issue a read_lock(x) and then change to another lock by issuing a
write_lock(x) operation. This is called Upgrading the lock.
Downgrading the lock:
A transaction T can issue a write_lock(x) and then change to another lock by issuing a
read_lock(x) operation. This is called downgrading the lock.
Deadlock:
Deadlock occurs when each transaction T in asset of two or more transactions is waiting
for some item that is locked by some other transaction T in the set. Each transaction in
the set is waiting for the other transaction to release the lock. This state where none of
them can proceed further is called deadlock.
Deadlock prevention protocols:
The protocol used in conservative locking requires that a transaction locks all the items it
needs in advance, if the items are not obtained, the transaction waits and tries to lock all
items.
Another protocol requires that all items are ordered and the transactions have to lock
them in the order.
Another protocol works based on the timestamp, a unique identifier for each transaction.
Two schemes,
1. Wait-die scheme:
If TS (Ti)<TS (Tj)then
Ti is older than Tj,
Ti is allowed to wait.
Else
Ti is younger than Tj,
Abort Ti so that Ti dies,
Restart it later with the same timestamp.
In wait-die the older transaction waits on the younger transaction and the younger
transaction requesting the item held by older transaction is aborted and restarted.
2. Wound-wait scheme:
If TS (Ti) <TS (Tj) then
Ti is older than Tj
Abort Tj (Ti wounds Tj)
Restart later with the same timestamp.
Else
Ti is younger than Tj
Ti is allowed to wait.
In this the younger one is allowed to wait on the older one whereas the older one waiting
for the item held by the younger transaction pre-empts the younger transaction by
aborting it.
3. No waiting Algorithm:
In this if a transaction is unable to obtain the lock it is immediately aborted and
restarted after a certain time delay without checking whether a deadlock will
actually occur or not.
4. Cautious Waiting Algorithm:
In this the transaction Ti needs to lock an item X which is locked by another transaction
Tj with a conflicting lock. If Tj is not waiting for some other locked item, then Ti is
blocked and allowed to wait, otherwise abort Ti.
Deadlock detection and Timeouts:
Dead lock is detected using wait-for-graph. A node is created for every transaction in the
wait for graph. A directed edge exists between Ti and Tj, if Ti is waiting for a lock held
by Tj. If a cycle occurs in the graph then the system is deadlocked. If the system is
deadlocked a transaction has to be selected to be aborted and this selection is called
victim selection. Time out method can also be used where each transaction when it
exceeds the system defined timeout period will be aborted regardless of if the deadlock
occurred or not.
Starvation:
Starvation occurs when the transaction cannot proceed for indefinite period of time while
other transactions are normally carried out. This happens when the same transaction is
repeatedly chosen as the victim for aborting. The solution to this can be to use the first
come first served queue or to increase the priority of the transactions they wait longer.
3. Write Phase:
If the validation phase is successful the transaction updates are applied to
the database, otherwise the updates are discarded and the transactions are
restarted.