Dbms Notes Ramamoorthy
Dbms Notes Ramamoorthy
Evaluation
Optimization
Transaction
X's Account
Open_Account(X)
Old_Balance = X.balance
New_Balance = Old_Balance - 800
X.balance = New_Balance
Close_Account(X)
Y's Account
Open_Account(Y)
Old_Balance = Y.balance
New_Balance = Old_Balance + 800
Y.balance = New_Balance
Close_Account(Y)
R(X);
X = X - 500;
W(X);
o The first operation reads X's value from database and stores
it in a buffer.
o The second operation will decrease the value of X by 500.
So buffer will contain 3500.
o The third operation will write the buffer's value to the
database. So X's final value will be 3500.
States of Transactions
ACID Properties
For example:
For example:
For example:
Concurrency Control
Lock-Based Protocol
1. Shared lock:
Example:
The following way shows how unlocking and locking work with
2-PL.
Transaction T1:
Transaction T2:
o Growing phase: from step 2-6
o Shrinking phase: from step 8-9
o Lock point: at 6
Deadlock in DBMS
For example:
Deadlock Avoidance
Deadlock Detection
o In a database, when a transaction waits indefinitely to
obtain a lock, then the DBMS should detect whether the
transaction is involved in a deadlock or not. The lock
manager maintains a Wait for the graph to detect the
deadlock cycle in the database.
The wait for a graph for the above scenario is shown below:
Deadlock Prevention
Wait-Die scheme
Let's assume there are two transactions Ti and Tj and let TS(T)
is a timestamp of any transaction T. If T2 holds a lock by some
other transaction and T1 is requesting for resources held by T2
then the following actions are performed by DBMS:
Recovery System
Recovery Facilities
Failure Classification
To find that where the problem has occurred, we generalize a
failure into the following categories:
1. Transaction failure
2. System crash
3. Disk failure
1. Transaction failure
The transaction failure occurs when it fails to execute or when it
reaches a point from where it can't go any further. If a few
transaction or process is hurt, then this is called as transaction
failure.
Reasons for a transaction failure could be –
2. System Crash
o System failure can occur due to power failure or other
hardware or software failure. Example: Operating system error.
Log-Based Recovery
The log is a sequence of records. Log of each transaction is
maintained in some stable storage so that if any failure
occurs, then it can be recovered from there.
If any operation is performed on the database, then it will
be recorded in the log.
But the process of storing the logs should be done before
the actual transaction is applied in the database.
Let's assume there is a transaction to modify the City of a
student. The following logs are written for this transaction.
When the transaction is initiated, then it writes 'start' log.
1. <Tn, Start>
When the transaction modifies the City from 'Noida' to
'Bangalore', then another log is written to the file.
1. <Tn, City, 'Noida', 'Bangalore' >
1. <Tn, Commit>
1. There are two approaches to modify the database:
Checkpoint
The recovery system reads log files from the end to start. It
reads log files from T4 to T1.
Recovery system maintains two lists, a redo-list, and an
undo-list.
The transaction is put into redo state if the recovery system
sees a log with <Tn, Start> and <Tn, Commit> or just <Tn,
Commit>. In the redo-list and their previous list, all the
transactions are removed and then redone before saving
their logs.
For example:
In the log file, transaction T2 and T3 will have <Tn, Start>
and <Tn, Commit>. The T1 transaction will have only <Tn,
commit> in the log file. That's why the transaction is
committed after the checkpoint is crossed. Hence it puts
T1, T2 and T3 transaction into redo list.
The transaction is put into undo state if the recovery system
sees a log with <Tn, Start> but no commit or abort log
found. In the undo-list, all the transactions are undone, and
their logs are removed.
For example:
Transaction T4 will have <Tn, Start>. So T4 will be put
into undo list since this transaction is not yet complete and
failed amid.