Unit 3_Transaction Management & Serializability
Unit 3_Transaction Management & Serializability
Concurrent execution of
multiple transactions
To preserve the integrity of
data the database system
must ensure ACID properties
A Atomicity
C Consistency
I Isolation
D Durability
Atomicity. Either all operations of the
transaction are properly reflected in the
database or none are.
TRANSACTION STATES
• Active – the initial state; the transaction stays in this
state while it is executing
• Partially committed – after the final statement has
been executed.
• Failed -- after the discovery that normal execution can
no longer proceed.
• Aborted – after the transaction has been rolled back
and the database restored to its state prior to the start
of the transaction. Two options after it has been
aborted:
– restart the transaction
• can be done only if no internal logical error
– kill the transaction
• Committed – after successful completion.
Concurrent Executions
• Multiple transactions are allowed to run
concurrently in the system. Advantages are:
increased processor and disk utilization, leading to better
transaction throughput
E.g. one transaction can be using the CPU while another is
reading from or writing to the disk
reduced average response time for transactions: short
transactions need not wait behind long ones.
• Concurrency control schemes – mechanisms to
achieve isolation
that is, to control the interaction among the concurrent
transactions in order to prevent them from destroying the
consistency of the database
Schedules
• Schedule – a sequences of instructions that specify the order
in which instructions of concurrent transactions are executed
– a schedule for a set of transactions must consist of all instructions
of those transactions
– must preserve the order in which the instructions appear in each
individual transaction.
• A transaction that successfully completes its execution will
have a commit instructions as the last statement
– by default transaction assumed to execute commit instruction as
its last step
• A transaction that fails to successfully complete its execution
will have an abort instruction as the last statement
Schedule 1
• Let T1 transfer $50 from A to B, and T2 transfer 10% of
the balance from A to B.
• A serial schedule in which T1 is followed by T2 :
Schedule 2
• A serial schedule where T2 is followed by T1
Schedule 3
• Let T1 and T2 be the transactions defined previously.
The following schedule is not a serial schedule, but it is
equivalent to Schedule 1.
Concurrent
schedule
In Schedules 1, 2
and 3,
the sum A + B is
preserved.
Schedule 4
• The following concurrent schedule does not
preserve the value of (A + B ).
This problem occurred because two
transactions are working on the same
resource without knowing each other’s
activity
•:transaction reads values written
by another transaction that hasn’t
committed yet.
(a)Recoverable schedule
(b) Cascadeless schedule
(a)Recoverable schedule
read
write
read
write
X-lock is requested using
lock-X instruction.
Compatible??
Ex: for performing locking
If a lock cannot be granted, the
requesting transaction is made
to wait till all incompatible locks
held by other transactions have
been released. The lock is then
granted
Necessary condition for deadlock
T1: lock-s(A);
read (A);
lock-S(B);
read (B);
unlock(B);
unlock(B);
display(A+B);
The point in the schedule where
the transaction has obtained its
final lock(the end of growing
phase).
T2: read(A1);
read(A2);
.display(A1+A2);
Two-phase locking with lock conversions:
– First Phase:
•can acquire a lock-S on item
•can acquire a lock-X on item
•can convert a lock-S to a lock-X (upgrade)
– Second Phase:
•can release a lock-S
•can release a lock-X
•can convert a lock-X to a lock-S (downgrade)
•Another method for finding the serializability
order
OR
write accepted
write rejected
write
wite rejected
rejected
A schedule that is possible under the time stamp protocol
assume TS(T25) < TS (T26)
•ensures •may not
freedom cascade-free
•may not
from deadlock recoverable.
• Problem with timestamp-ordering protocol:
• Suppose Ti aborts, but Tj has read a data item
written by Ti , then Tj must abort; if Tj had been
allowed to commit earlier, the schedule is not
recoverable.
• Further, any transaction that has read a data
item written by Tj must abort this can lead to
cascading rollback --- that is, a chain of
rollbacks
• Solution 1:
• A transaction is structured such that its writes are all
performed at the end of its processing
• All writes of a transaction form an atomic action; no
transaction may execute while a transaction is being
written
• A transaction that aborts is restarted with a new
timestamp
• Solution 2: Limited form of locking: wait for data to be
committed before reading it
• Solution 3: Use commit dependencies to ensure
recoverability
Consider the example Example :
Don’t worry, We
have a soloution
THOMAS WRITE
RULE
• Modified version of the timestamp-ordering protocol
in which obsolete write operations may be ignored
under certain circumstances.
1. Transaction failure
•Logical error : (due to internal
condition) bad input, data
not found, resource limit
exceed.
3. Disk failure
: head crash or failure during data
transfer operation.
•Volatile storage:
•does not survive system crashes
•examples: main memory, cache memory
•Nonvolatile storage:
•survives system crashes
•examples: disk, tape, flash memory,
non-volatile (battery backed up) RAM
•but may still fail, losing data
•Stable storage:
•a mythical form of storage that survives all
failures
•approximated by maintaining multiple copies on
distinct nonvolatile media
Blocks : Fixed length storage units
x2
x1
y1
memory disk
• 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(BX) 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
Transaction Log
• also know as journal log / redo-log
• It is a physical file
• It usually contain
• transaction identifier
• data –item identifier(or time stamp)
• old value
• new value
We denote various type of log record as
•Transaction updates/alternation
•On commit all the changes to the db are made
permanent and log files discarded
•Disadvantage
•Frequent I/O operations while the
transaction Is active .
•Commercial RDMS is neither deferred nor immediate
• Database updated at fixed interval of time
•Irrespective of transaction commit/un-commit state.
•Check pointing : updating transaction at fixed intervals of
time is called check-pointing.
• @ check point time log files changes applied to the
database.
•
• During recovery we need to consider only the most recent
transaction Ti that started before the checkpoint, and
transactions that started after Ti.
1. Scan backwards from end of log to find the most recent
<checkpoint L> record
– Only transactions that are in L or started after the
checkpoint need to be redone or undone
– Transactions that committed or aborted before the
checkpoint already have all their updates output to stable
storage.
• Some earlier part of the log may be needed for undo
operations
1. Continue scanning backwards till a record <Ti start> is
found for every transaction Ti in L.
– Parts of log prior to earliest <Ti start> record above are not
needed for recovery, and can be erased whenever desired.
•Alternative to log-based crash-recovery techniques.
•Advantage: require few disk access than log-based.
• pages : fixed length portioned block in database.
• page table :
•The page table has n entries—one for each
database page.
•Each entry contains a pointer to a page on
disk .