DBMS_Module6
DBMS_Module6
• Suppose T has been executed till Read (Y) and then T’’ starts. As a result , interleaving of
operations takes place due to which T’’ reads correct value of X but incorrect value of Y
and sum computed by
T’’: (X+Y = 50, 000+500=50, 500)
is thus not consistent with the sum at end of transaction:
T: (X+Y = 50, 000 + 450 = 50, 450).
• Hence, transactions must take place in isolation and changes should be visible only after they
have been made to the main memory.
Durability
• This property ensures that once the transaction has completed
execution, the updates and modifications to the database are
stored in and written to disk and they persist even if a system
failure occurs.
• These updates now become permanent and are stored in
non-volatile memory.
• The effects of the transaction, thus, are never lost.
Transaction control command
• These are used to manage the changes made to the data in a
table by DML statements.
• It also allows statements to be grouped together into logical
transactions.
• We have following Transaction Control Language (TCL)
commands:
• COMMIT
• SAVEPOINT
• ROLLBACK
The COMMIT command
• COMMIT command is used to permanently save any transaction into the
database.
• When we use any DML command like INSERT, UPDATE or DELETE, the
changes made by these commands are not permanent, until the current
session is closed, the changes made by these commands can be rolled
back.
• To avoid that, we use the COMMIT command to mark the changes as
permanent.
• Following is commit command's syntax,
COMMIT
The SAVEPOINT command
Result:
Example…
Result:
Query:
Concurrent execution
• In a multi-user system, multiple users can access and use the
same database at one time, which is known as the concurrent
execution of the database.
• It means that the same database is executed simultaneously
on a multi-user system by different users.
• The thing is that the simultaneous execution that is performed
should be done in an interleaved manner, and no operation
should affect the other executing operations, thus maintaining
the consistency of the database.
Concurrency execution…
• Thus, on making the concurrent execution of the transaction
operations, there occur several challenging problems that
need to be solved.
• Some of the problems we have discussed already:
• The Lost Update Problem
• The Temporary Update (or Dirty Read) Problem
• The Incorrect Summary Problem
Serializibility
• When multiple transactions are running concurrently then
there is a possibility that the database may be left in an
inconsistent state.
• A serializable schedule is the one that always leaves the
database in consistent state.
• Serializability is a concept that helps us to check which
schedules are serializable.
Serializibility…
• A serial schedule is always a serializable schedule because in serial
schedule, a transaction only starts when the other transaction
finished execution.
• However, a non-serial schedule needs to be checked for
Serializability.
• A non-serial schedule of n number of transactions is said to be
serializable schedule, if it is equivalent to the serial schedule of
those n transactions.
• A serial schedule doesn’t allow concurrency, only one transaction
executes at a time and the other starts when the already running
transaction finished.
Examples of serial and nonserial schedules involving transactions T1 and T2
(a) Serial scheduleA: T1 followed by T2 (b) Serial schedule B: T2 followed by
T1 (c), (d) Two nonserial schedules C and D with interleaving of operations
Characterizing Schedules Based on
Serializability
Problem with serial schedules
• Limit concurrency by prohibiting interleaving of
operations
• Unacceptable in practice
Solution: determine which non serial schedules are
equivalent to a serial schedule and allow those to
occur
Serializable schedule of n transactions
• Equivalent to some serial schedule of same n
transactions
Characterizing Schedules Based on
Serializability…
Result equivalent schedules
• Produce the same final state of the database
• May be accidental
• Cannot be used alone to define equivalence of
schedules.
Two schedules that are result equivalent for the initial value of X = 100
but are not result equivalent in general
Types of Serializibility
• Note: data item X can be read multiple times in a schedule but the
first read operation on X is called the initial read.
Final write
• A final write must be the same between both the schedules. In
schedule S1, if a transaction T1 updates A at last then in S2,
final writes operations should also be done by T1.
Update read
• In schedule S1, if Ti is reading A which is updated by Tj then in
S2 also, Ti should read A which is updated by Tj.
Initial Read:
Example • In schedule S1, transaction T1 first reads the data
item X. In S2 also transaction T1 first reads the data
item X.
• Initial read condition is satisfied in S1 & S2.
Final Write:
• In schedule S1, the final write operation on Y is done
by transaction T2. In S2 also transaction T2 performs
the final write on Y.
• Final write condition is satisfied in S1 & S2.
Update Read:
• In S1, transaction T2 reads the value of X, written by
T1. In S2, the same transaction T2 reads the X after it
is written by T1.
• Update Read condition is satisfied in S1 & S2.
Concurrency Control: Lock-Based Protocols
• A lock is a mechanism to control concurrent access to a data
item
• Data items can be locked in two modes :
1. exclusive (X) mode. Data item can be both read as well as
written. X-lock is requested using lock-X instruction.
2. shared (S) mode. Data item can only be read. S-lock is
requested using lock-S instruction.
• Lock requests are made to the concurrency-control manager
by the programmer. Transaction can proceed only after
request is granted.
Lock-Based Protocols (Cont.)
• Lock-compatibility matrix
x2
x1
y1
memory disk
Data Access (Cont.)
• Each transaction Ti has its private work-area in which local
copies of all data items accessed and updated by it are kept.
• Ti's local copy of a data item X is called xi.
• Transferring data items between system buffer blocks and its
private work-area done by:
• read(X) assigns the value of data item X to the local variable xi.
• write(X) assigns the value of local variable xi to data item X in the
buffer block.
• Note: output(X) need not immediately follow write(X). System can
perform the output operation when it deems fit.
• Transactions
• Must perform read(X) before accessing X for the first time
(subsequent reads can be from local copy)
• write(X) can be executed at any time before the transaction commits
Recovery and Atomicity
• To ensure atomicity despite failures, we first output
information describing the modifications to stable storage
without modifying the database itself.
• We study log-based recovery mechanisms in detail
• We first present key concepts
• And then present the actual recovery algorithm
• Less used alternative: shadow-copy and shadow-paging
shadow-copy
Shadow Paging
Log-Based Recovery
• A log is kept on stable storage.
• The log is a sequence of log records, and maintains a record of update
activities on the database.
• When transaction Ti starts, it registers itself by writing a
<Ti start>log record
• Before Ti executes write(X), a log record
<Ti, X, V1, V2>
is written, where V1 is the value of X before the write (the old
value), and V2 is the value to be written to X (the new value).
• When Ti finishes it last statement, the log record <Ti commit> is
written.
• Two approaches using logs
• Deferred database modification
• Immediate database modification
Immediate Database Modification
• The immediate-modification scheme allows updates of an uncommitted
transaction to be made to the buffer, or the disk itself, before the
transaction commits
• Update log record must be written before database item is written
• We assume that the log record is output directly to stable storage
• Output of updated blocks to stable storage can take place at any time
before or after transaction commit
• Order in which blocks are output can be different from the order in
which they are written.
• The deferred-modification scheme performs updates to buffer/disk only
at the time of transaction commit
• Simplifies some aspects of recovery
• But has overhead of storing local copy
Immediate Database Modification Example
<T0 start>
<T0, A, 1000, 950>
<To, B, 2000, 2050
A = 950
B = 2050
<T0 commit>
<T1 start>
<T1, C, 700, 600> BC output before T1
C = 600 commits
BB , BC
<T1 commit>
BA
• Note: BX denotes block containing X. BA output after T0
commits
Concurrency Control and Recovery
• With concurrent transactions, all transactions share a single disk
buffer and a single log
• A buffer block can have data items updated by one or more transactions
• We assume that if a transaction Ti has modified an item, no other
transaction can modify the same item until Ti has committed or
aborted
• Otherwise how to perform undo if T1 updates A, then T2 updates A and commits,
and finally T1 has to abort?
• Can be ensured by obtaining exclusive locks on updated items and holding
the locks till end of transaction (strict two-phase locking)
• Log records of different transactions may be interspersed in the
log.
Undo and Redo Operations
• Undo of a log record <Ti, X, V1, V2> writes the old value V1 to X
• Redo of a log record <Ti, X, V1, V2> writes the new value V2 to X
• Undo and Redo of Transactions
• undo(Ti) restores the value of all data items updated by Ti to their old
values, going backwards from the last log record for Ti
• each time a data item X is restored to its old value V a special log record <Ti , X, V>
is written out
• when undo of a transaction is complete, a log record
<Ti abort> is written out.
• redo(Ti) sets the value of all data items updated by Ti to the new values,
going forward from the first log record for Ti
• No logging is done in this case
Undo and Redo on Recovering from Failure
• When recovering after failure:
• Transaction Ti needs to be undone if the log
• contains the record <Ti start>,
• but does not contain either the record <Ti commit> or <Ti abort>.
• Transaction Ti needs to be redone if the log
• contains the records <Ti start>
• and contains the record <Ti commit> or <Ti abort>
• Note that If transaction Ti was undone earlier and the
<Ti abort> record written to the log, and then a failure occurs,
on recovery from failure Ti is redone
• such a redo redoes all the original actions including the steps that
restored old values Known as repeating history
• Seems wasteful, but simplifies recovery greatly
Immediate DB Modification Recovery Example