Unit-7 Transaction Processing
Unit-7 Transaction Processing
Transaction
Processing
What is transaction?
A transaction is a sequence of operations performed as a single
logical unit of work.
A transaction is a logical unit of work that contains one or more
SQL statements.
Work as a single
Example of transaction logical unit
read (A)
A = A – 50
Transaction write (A)
Operations
read (B)
B = B + 50
write (B)
Property of
transaction
ACID property of transaction
ACID property
• Atomicity
• Consistency
• Isolation
• Durability
ACID property of transaction
• Atomicity 0%
– This property states that a transaction read (A)
must be treated as an atomic unit, that
is, either all of its operations are
A = A – 50
executed or none. write (A)
FAIL
– Either transaction execute 0% or read (B)
100%.
B = B + 50
– For example consider a transaction to
transfer Rs. 50 from account A to write (B)
account B. 100%
read (A)
Partial
Committed Committed A = A – 50
write (A)
Active End read (B)
B = B + 50
Failed Aborted
write (B)
Commit
Transaction State Diagram \ State Transition Diagram
1.Active:
2. Partially Committed:
Read (A)
A = A - 50
Write (A)
Read (B)
B = B + 100
Write (B)
Commit
Read (B)
B = B + 50
Write (B)
Commit
Example of interleaved schedule
T1 T2
Read (A)
A = A - 50
Write (A)
Read (A)
A = A - 100
Write (A)
Read (B)
B = B + 50
Write (B)
Commit
Read (B)
B = B + 100
Write (B)
Commit
Equivalent schedule
T1 T2 T1 T2
Read (A) Read (A)
0R
Two schedules are said to be conflict equivalence if all
conflicting operations in both the schedule must be executed in
the same order.
Conflict Serializability (example)
T1 T2 T1 T2
Read (A) Read (A)
A = A - 50 A = A - 50
Write (A) Write (A)
Read (B) Read (A)
B = B + 50 Temp = A * 0.1
Write (B) A = A - temp
Commit Write (A)
Read (A)
Temp = A * 0.1 Read (B)
A = A - temp B = B + 50
Write (A) Write (B)
Read (B) Commit Read (B)
B = B + temp B = B + temp
Write (B) Write (B)
Commit Commit
Conflict Serializability (example)
Conflict serializability (example)
Conflict Serializability (example)
Conflict Serializability (example)
Conflict Serializability (example)
Conflict Serializability
Example of a schedule that is not conflict serializable:
T1 T2
Read (A)
Write (A)
Read (A)
Initial read √
Write-read conflict √
Final write √
View
Serializable Conflict
Serializable
Concurrency
Control
What is concurrency?
Concurrency is the ability of a database to allow
multiple (more than one) users to access data at the
same time.
Three problems due to concurrency
1. The lost update problem
2. The dirty read problem
3. Unrepeatable Read Problem
4. Incorrect Summary Problem
The lost update problem
The dirty read problem
Unrepeatable Read Problem
Incorrect Summary Problem
Concurrency
Control Schemes
What is lock?
A lock is a variable associated with data item to control
concurrent access to that data item.
Lock variable
0
Database
What is lock?
A lock is a variable associated with data item to control
concurrent access to that data item.
Locking is a strategy that is used
to prevent such concurrent
updates to data.
Lock variable
1
Database
What is lock?
A lock is a variable associated with data item to control
concurrent access to that data item.
Lock variable
0
Database
Lock Based
Protocol
Lock based protocol
Lock based protocol
Data items can be locked in two modes :
1. Shared (S) mode: When we take this lock we can just read the item
but cannot write.
2. Exclusive (X) mode: When we take this lock we can read as well as
write the item.
Lock based protocol
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
qqqqqqqqqqqqqq
Lock based protocol
Lock based protocol
Lock based protocol
Lock based protocol
Lock based protocol
Lock based protocol
A transaction may be granted a lock on an item if the requested
lock is compatible with locks already held on the item by other
transactions.
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.
Any number of transactions can hold shared locks on an item,
but if any transaction holds an exclusive on the item no other
transaction may hold any lock on the item.
Lock based protocol
This locking protocol divides transaction execution phase into
three parts:
1. When transaction starts executing, transaction seeks grant for locks it
needs as it executes.
2. Where the transaction acquires all locks and no other lock is required.
Transaction keeps executing its operation.
3. As soon as the transaction releases its first lock, the third phase starts.
In this phase a transaction cannot demand for any lock but only
releases the acquired locks.
Transaction
Lock acquisition execution Lock releasing
phase phase
Transaction
Transaction
T begin T end Time
2PL (Two phase locking protocol)
qqqqqq
Modified
version of
Two phase
locking protocol
Strict two phase locking protocol
In this protocol, a transaction may release all the shared locks
after the Lock Point has been reached, but it cannot release any
of the exclusive locks until the transaction commits or aborts.
It ensures that if data is being modified by one transaction,
then other transaction cannot read it until first transaction
commits.
This protocol solves dirty read problem.
Rigorous two phase locking protocol
In this protocol, a transaction is not allowed to release any lock
(either shared or exclusive) until it commits.
This means that until the transaction commits, other
transaction might acquire a shared lock on a data item on
which the uncommitted transaction has a shared lock; but
cannot acquire any lock on a data item on which the
uncommitted transaction has an shared lock.
Lock Conversion : Upgrading and Downgrading
Conversion between the locks is possible by the two methods
listed below:
Upgrading: conversion from a read lock to write lock
Downgrading: conversion from a write lock to read lock
Transaction Transaction
Hold T1 Request
Request Hold
Table1 DEADLOCK Table2
T2 Hold
Request
Table Table
Deadlock
There are two Principle method to handle deadlock in system.
1.Deadlock Prevention:
T1
T2
T3
T4
At failure time:
• Ignore the transaction T1 as it has already been committed before
checkpoint.
• Redo transaction T2 and T3 as they are active at/after checkpoint, but have
committed before failure.
• Undo transaction T4 as it is active after checkpoint and has not committed.
Shadow Paging
Technique
Shadow paging technique
Shadow paging is an alternative to log-based recovery
This scheme is useful if transactions execute serially.
It maintain two page tables during the lifetime of a transaction
• current page table
• shadow page table
Shadow page table is stored on non-volatile storage.
Shadow page table is never modified during execution of
transaction.
Shadow paging technique
Page 1 Page 1 Page 1
Page 2 Page 2 Page 2
Page 3 Page 3 Page 3
Page 4 Page 4 Page 4
Page 5 Page 5 Page 5