Unit 4
Unit 4
TRANSACTION MANAGEMENT
UNIT-4 Syllabus
• Transaction Concept,
• Transaction State,
• Implementation of Atomicity and Durability,
• Chapter-15
Concurrent Executions,
Transactio
• Serializability, ns
• Recoverability,
• Implementation of Isolation,
• Testing for serializability,
• Lock Based Protocols, Chapter-16
• Timestamp Based Protocols, Concurrency
• Validation- Based Protocols, Control
• Multiple Granularity,
Chapter-17
• Recovery and Atomicity, Recovery
• Log–Based Recovery, System
• Recovery with Concurrent Transactions.
Transaction Concept
• A transaction is a unit of program execution that accesses and
possibly updates various data items.
• A transaction is a set of operations or very small unit of a program to do
a logical task.
• E.g., transaction to transfer $50 from account A to account B:
1. read(A)
2. A := A – 50
3. write(A)
4. read(B)
5. B := B + 50
6. write(B)
• Durability requirement — once the user has been notified that the transaction
has completed (i.e., the transfer of the $50 has taken place), the updates to the
database by the transaction must persist even if there are software or hardware
failures.
Example of Fund Transfer (Cont.)
• Consistency requirement in above example:
– The sum of A and B is unchanged by the execution of the
transaction
• In general, consistency requirements include
– Explicitly specified integrity constraints such as primary keys and
foreign keys
– Implicit integrity constraints
• e.g., sum of balances of all accounts, minus sum of loan
amounts must equal value of cash-in-hand
– A transaction must see a consistent database.
– During transaction execution the database may be temporarily
inconsistent.
– When the transaction completes successfully the database must
be consistent
• Erroneous transaction logic can lead to inconsistency
Example of Fund Transfer (Cont.)
• Isolation requirement — if between steps 3 and 6,
another transaction T2 is allowed to access the partially
updated database, it will see an inconsistent database
(the sum A + B will be less than it should be).
T1 T2
1. read(A)
2. A := A – 50
3. write(A)
read(A), read(B), print(A+B)
4. read(B)
5. B := B + 50
6. write(B
1.Conflict serializability
2. View serializability
Schedule Schedule
3 6
Conflict Serializability (Cont.)
• Example of a schedule that is not conflict serializable:
S1 is View Equivalent
to S2
Example of view serializability
S1 S2( Serial Schedule)
T1 T2 T1 T2
READ(A) READ(A)
WRITE(A) WRITE(A)
READ(A) READ(B)
WRITE(A) WRITE(B)
READ(B) READ(A)
WRITE(B) WRITE(A)
READ(B) READ(B)
WRITE(B) WRITE(B)
T T
1 2
• Precedence Graph:
T T
2 1
• The precedence graph contains the edge T1 →T2, because T1
executes read(A) before T2 executes write(A).
• It also contains the edge T2→T1, because T2 executes read(B)
before T1 executes write(B).
•The graph contains a cycle, this schedule is not conflict serialzable
Example1: Check for Conflict
Serializability
Schedule
T1 T2 T3
READ(X)
READ(Z) T2
READ(Z)
READ(X)
READ(Y) z y
WRITE(X) Acycli
c
READ(Y) X
WRITE(Z) T1 T3
WRITE(Y)
Schedule is Conflict
Serializable
Example2: Check for Conflict
Serializability
Schedule
T1 T2 T3
READ(X)
READ(X) T2
WRITE(X)
WRITE(X)
READ(X) x x
Cycli
Xc
T1 T3
running transactions.
Locks
– Transaction may obtain locks
– Transaction may not release locks
• Phase 2: Shrinking Phase
– Transaction may release locks
Time
– Transaction may not obtain locks
• The protocol assures
serializability. It can be proved that
the transactions can be serialized
in the order of their lock points
(i.e., the point where a transaction
acquired its final lock).
Cascading rollback may occur under two-phase locking.
As an illustration, consider the partial schedule of below Figure . Each
transaction
observes the two-phase locking protocol, but the failure of T5 after the
read(A)
step of T7 leads to cascading rollback of T6 and T7.
The Two-Phase Locking Protocol (Cont.)
• Two-phase locking does not ensure freedom from
deadlocks
• Extensions to basic two-phase locking needed to
ensure recoverability of freedom from cascading
roll-back
– Strict two-phase locking: a transaction must hold all
its exclusive locks till it commits/aborts.
• Ensures recoverability and avoids cascading roll-backs
– Rigorous two-phase locking: a transaction must hold
all locks till commit/abort.
• Transactions can be serialized in the order in which they
commit.
• Most databases implement rigorous two-phase
locking, but refer to it as simply two-phase locking
T3: lock-X(B); T4: lock-S(A);
read(B); read(A);
B := B − 50; lock-S(B);
write(B); read(B);
lock-X(A); display(A + B);
read(A); unlock(A);
A := A + 50;
unlock(B).
write(A);
unlock(B);
unlock(A).
Transaction T4 (transaction
Transaction T3 (transaction
T1 with unlocking delayed).
T2 with unlocking
delayed).
The Two-Phase Locking Protocol
(Cont.)
• Two-phase locking is not a
necessary condition for
serializability
– There are conflict serializable
schedules that cannot be obtained if
the two-phase locking protocol is
used.
• In the absence of extra information
(e.g., ordering of access to data),
two-phase locking is necessary for
conflict serializability in the following
sense:
– Given a transaction Ti that does not
follow two-phase locking, we can find
a transaction Tj that uses two-phase
locking, and a schedule for Ti and Tj
that is not conflict serializable.
Locking Protocols
• Given a locking protocol (such as 2PL)
– A schedule S is legal under a locking
protocol if it can be generated by a set of
transactions that follow the protocol
– A protocol ensures serializability if all
legal schedules under that protocol are
serializable
Lock Conversions
• Two-phase locking protocol with lock
conversions:
• Growing 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)
• Shrinking Phase:
– can release a lock-S
– can release a lock-X
– can convert a lock-X to a lock-S (downgrade)
• This protocol ensures serializability
Automatic Acquisition of Locks
• A transaction Ti issues the standard read/write
instruction, without explicit locking calls.
• The operation read(D) is processed as:
if Ti has a lock on D
then
read(D)
else begin
if necessary wait until no other
transaction has a lock-X on
D
grant Ti a lock-S on D;
read(D)
end
Automatic Acquisition of Locks (Cont.)
• The operation write(D) is processed as:
if Ti has a lock-X on D
then
write(D)
else begin
if necessary wait until no other trans. has
any lock on D,
if Ti has a lock-S on D
then
upgrade lock on D to lock-X
else
grant Ti a lock-X on D
write(D)
end;
• All locks are released after commit or abort
Implementation of Locking
• A lock manager can be implemented as a separate
process to which transactions send lock and unlock
requests
• The lock manager replies to a lock request by sending a
lock grant messages (or a message asking the
transaction to roll back, in case of a deadlock)
• The requesting transaction waits until its request is
answered
• The lock manager maintains a data-structure called a
lock table to record granted locks and pending
requests
• The lock table is usually implemented as an in-memory
hash table indexed on the name of the data item being
locked
Lock Table
• Black rectangles indicate granted
locks, white ones indicate waiting
requests
• Lock table also records the type of
lock granted or requested
• New request is added to the end of
the queue of requests for the data
item, and granted if it is compatible
with all earlier locks
• Unlock requests result in the
request being deleted, and later
requests are checked to see if they
Grante can now be granted
d • If transaction aborts, all waiting or
Waitin granted requests of the transaction
g are deleted
– lock manager may keep a list of
locks held by each transaction,
to implement this efficiently
Graph-Based Protocols
• Graph-based protocols are an alternative to
two-phase locking
• Impose a partial ordering → on the set D = {d1,
d2 ,..., dh} of all data items.
– If di → dj then any transaction accessing both di
and dj must access di before accessing dj.
– Implies that the set D may now be viewed as a
directed acyclic graph, called a database graph.
• The tree-protocol is a simple kind of graph
protocol.
Tree Protocol
transaction transaction
with smaller with larger
timestamp timestamp
read(B
) read(B)
B:= B-50
read(A)
A:= A+50
read(A)
(validate)
display
(A+B) (validate
)
write (B)
write (A)
Multiple Granularity
• Allow data items to be of various sizes and define a
hierarchy of data granularities, where the small granularities
are nested within larger ones
• Can be represented graphically as a tree (but don't confuse
with tree-locking protocol)
• When a transaction locks a node in the tree explicitly, it
implicitly locks all the node's descendents in the same
mode.
• Granularity of locking (level in tree where locking is done):
– fine granularity (lower in tree): high concurrency, high locking
overhead
– coarse granularity (higher in tree): low locking overhead, low
concurrency
Example of Granularity Hierarchy
IX ✔ ✔ × × ×
S ✔ × ✔ × ×
S IX ✔ × × × ×
X × × × × ×
Multiple Granularity Locking Scheme
• Transaction Ti can lock a node Q, using the following
rules:
1. The lock compatibility matrix must be observed.
2. The root of the tree must be locked first, and may be locked in
any mode.
3. A node Q can be locked by Ti in S or IS mode only if the
parent of Q is currently locked by Ti in either IX or IS mode.
4. A node Q can be locked by Ti in X, SIX, or IX mode only if the
parent of Q is currently locked by Ti in either IX or SIX mode.
5. Ti can lock a node only if it has not previously unlocked any
node (that is, Ti is two-phase).
6. Ti can unlock a node Q only if none of the children of Q are
currently locked by Ti.
• Observe that locks are acquired in root-to-leaf order,
whereas they are released in leaf-to-root order.
Recovery System
• There are various types of failure that may occur in a system
like transaction failure, system failure, disk fialure. In any
failure , information may be lost.
• The database system must take actions in advance to ensure
that the atomicity and durability properties of transactions.
• An integral part of a database system is a recovery scheme
that can restore the database to the consistent state that
existed before the failure.
• Storage structures:
– Volatile storage
– Nonvolatile storage
– Stable storage
• Stable storage plays a critical role in recovery algorithms.
Data Access
• Physical blocks are those blocks residing on the disk.
• Buffer blocks are the blocks residing temporarily in main
memory.
• Block movements between disk and main memory are
initiated through the following two operations:
– input(B) transfers the physical block B to main memory.
– output(B) transfers the buffer block B to the disk, and
replaces the appropriate physical block there.
• 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.
• We assume, for simplicity, that each data item fits in, and
is stored inside, a single block.
Data Access (Cont.)
• Transaction transfers data items between system buffer
blocks and its private work-area using the following
operations :
– 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.
– both these commands may necessitate the issue of an input(BX)
instruction before the assignment, if the block BX in which X
resides is not already in memory.
• Transactions
– Perform read(X) while accessing X for the first time;
– All subsequent accesses are to the local copy.
– After last access, transaction executes write(X).
• output(BX) need not immediately follow write(X). System
can perform the output operation when it deems fit.
Example ofbuffer
Data Access
Buffer Block A input(A
X A
)
Buffer Block Y B
B output(B)
read(X
) write(Y
)
x2
x1
y1
<T0 start>
<T0, A, 1000, 950>
To, B, 2000, 2050
A = 950
B = 2050
<T0 commit>
<T1 start> x
<T1, C, 700, 600> BC output before
1
C = 600 T1 commits
BB, BC
<T1 commit>
BA
BA output after
• Note: BX denotes block containing X.
T0 commits
Immediate Database Modification
(Cont.)
• Recovery procedure has two operations instead of one:
– 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
– 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
• Both operations must be idempotent
– That is, even if the operation is executed multiple times the
effect is the same as if it is executed once
• Needed since operations may get re-executed during recovery
• When recovering after failure:
– Transaction Ti needs to be undone if the log contains the
record
<Ti start>, but does not contain the record <Ti commit>.
– Transaction Ti needs to be redone if the log contains both the
record <Ti start> and the record <Ti commit>.
• Undo operations are performed first, then redo operations.
Immediate DB Modification Recovery Example
checkpoin system
t failure