Unit4TransactionManagementpptx 2023 10-11-13!20!24
Unit4TransactionManagementpptx 2023 10-11-13!20!24
CE/IT
DBMS:Database Management
System
Transaction Unit no : 4
Transaction
DBMS (3130703)
Rachit
Adhvaryu
•Transaction concepts
•ACID
Department of
•Serializability
•Recovery
Unit no : 6
•Concurrency Control
Transaction
DBMS (3130703)
•Locking mechanism
•Deadlock
Anything under execution can be known as
Transaction.
A transaction is a sequence of operations
performed as a single logical unit of work that
contains one or more than one SQL statements.
1. Read(A); Operations
2. A := A – 50;
3. Write(A); Work as a
4. Read(B);
Transaction single Unit
5. B := B + 50;
6. Write(B);
Transactions
Atomicity (Either transaction is Complete or No
Transaction)
Consistency (consistent data must be available
whenever the database is accessed after any
Transaction transaction)
Isolation (Transactions details of one transaction
must be hidden from other transactions)
(ACID
Durability (All changes made by the transaction
Properties)
must remain permanent through out the database)
Atomicity
This property states that, in a transaction either
all of its operations are executed or none.
Either transaction execute 0% or 100%.
Transaction For example, consider a transaction to transfer
(ACID Rs. 1000 from account A to account B.
Properties) So, if Rs. 1000 is deducted from account A, then
it must be added to account B else in case of
error, it must be added back to Account A.
Consistency
Consistent data/values must be available after
any transaction.
If the database was in a consistent state before
the execution of a transaction; then it must
remain consistent after the execution of the
transaction is completed.
Transaction
(ACID If the amount has been debited from account A
Properties) and has been added to account B, then both the
accounts must show the amount after the
transaction has been performed.
For e.g. Initial amount in account A = 5000 and
account B = 3000. Amount to be transferred
from A to B = 1000.
Thus, after the transaction is completed, amount
Isolation
Transactions details of one transaction must be
hidden from other transactions until the
Transaction transaction is committed.
Intermediate transaction results must be hidden
from other concurrently executed transactions.
(ACID
For e.g. if account A start transferring amount to
Properties)
account B, then no other transaction should be
allowed to access account A until the current
transaction is fully completed.
Durability
All changes made by the transaction must
remain permanent through out the database.
After a transaction completes successfully, the
changes to the database must be permanent,
even if system fails.
Transaction
(ACID For e.g. Initial amount in account A = 5000 and
Properties) account B = 3000. Amount to be transferred
from A to B = 1000.
Thus, after the transaction is completed, amount
in Account A should be 4000 and in account B
should be 4000.
This change should remain permanent until some
other transaction changes any account’s amount.
Active
This is the initial state.
The transaction remains in this state while execution.
Partial Committed
It is a state where a transaction actually enter in its last
operation.
Transaction
Failed
(State An execution cannot be performed due to errors.
Diagram Once a transaction cannot be completed, any changes that
it made must be undone rolling it back.
or
Committed
Life Cycle) It is a state where a transaction has completed all its
operations and changes to the database are reflected
We cannot abort or rollback a committed transaction.
Aborted
The state after the transaction has been rolled back and
the database has been restored to start of the transaction
state.
It is a state where a transaction has
completed all its operations and changes to
the database are reflected
It is a state where a transaction actually
We cannot abort or rollback a committed
enter in its last operation.
transaction.
Partial
Committ Committe
ed d
Transaction
(State Active End
Diagram
or
Failed Aborted
Life Cycle)
This is the initial state.
An executioncannot
The be performed
The state due to
after the transaction has been
transaction remains in
errors.
this state rolled back and the database has been
while execution.
Once a transaction cannot
restored be completed,
to start of the transaction state.
any changes that it made must be undone
rolling it back.
1. Read (X)
Serializability)
Read (A)
Read (B)
T = A * 0.25
B = B + 50
Read (B) A=A-T
Write (B) Read (B)
B=B+T Write (A)
Commit B=B+T
Write (B)
Write (B)
Commit
Commit
Let there be two schedules with the same set of
transactions. Both the schedules are said to be
Transaction view serialized, if the following three conditions
are satisfied, for each data item T:
(View
Initial Read
Serializability)
Updated Read
Final Write
Initial Read
If in schedule R1, transaction T1 reads the initial
value of T, then in schedule R2 also transaction
T1 must read the initial value of T.
Schedules R1 and R2
R1
are not view
Transaction T1 : Read (T) equivalent because
(View T2 : Write (T) initial read operation
in R1 is done by T1
Serializability) R2 and in R2 it is done by
T2
T1 : Write (T) Schedules R1 and R3
T2 : Read (T) are view equivalent
because initial read
R3 operation in R1 is
T1 : Read (T) done by T1 and in R3
it is also done by T1
T2 : Read (T)
Update Read
If in schedule R1 transaction T1 executes
read(T), and that value was produced by
transaction T2 (if any), then in schedule R2 also
transaction T1 must read the value of T that was
produced by transaction T2.
R1 R2
Transaction T1 T2 T3 T1 T2 T3
(View Write Write
(A) Write Write (A)
Serializability) (A) Read (A) Read
(A) (A)
It involves 2 phases:
Prepare Phase (Preparing to Commit
transaction)
Commit/Abort Phase (Final Response)
Coordinator send
request asking for
ready to commit
Participant send
Reque
reply whether ready st
to commit or not Prepar prepa to
re
ed
e Prepar
Phase
C om m
it/Abo
rt
Commi
t D on e
Phase
Send “ack” to
inform whether Coordinator inform
commit done or not to do commit
Prepare Phase
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
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.
Concurrency
If a lock cannot be granted, the requesting
Control
(Lock Based transaction is made to wait till all incompatible
Protocol) locks held T2:by other transactions
Example have been
of Transactions
released.
lock-S(A); using Locking.
read
(A); A locking protocol is a
set of rules followed by all
unlock(A); transactions while
lock- requesting and releasing
The execution phase of transaction can be described
as following:
When execution of transaction starts, create a
list of data items and type of lock, it needs and
request for that lock.
When all the locks are granted, transaction
Concurrency continues execution of its operation.
Control As soon as the transaction releases its first lock,
it cannot demand for any lock; but can only
release the acquired locks.
(Lock Based
Protocol) Transaction
Lock Request Execution Lock Release
Transaction
T begin T end Time
Consider the following schedule:
Concurrency
Control
(Pitfalls of
Neither T3 nor T4 can make progress. lock-
X(B) causes T4 to wait for T3 to release its lock
Lock Based
Protocol) on B, while lock-X(A) causes T3 to wait for T4
to release its lock on A.
Such a situation is called a Deadlock.
To handle a deadlock, one of T3 or T4 must be
rolled back and its locks must be released.
The potential for deadlock exists in most
locking protocols. Deadlocks are a necessary
evil.
Starvation is also possible if concurrency
control manager is badly designed. For
Concurrency example:
Control Transaction may be waiting for an X-lock on
an item, while a sequence of other
transactions request and are granted an S-
(Pitfalls of
Lock Based lock on the same item.
Protocol) The same transaction is repeatedly rolled back
due to deadlocks.
Starvation can be managed in following way:
There is no other transaction holding a lock
that arises a certain type of conflicts.
There is no other transaction who requested
The Two Phase Lock protocol has 2 phases:
Growing Phase:
transaction may obtain locks
transaction may not release locks
Shrinking Phase:
Concurrency transaction may release locks
Control transaction may not obtain locks
(Two Phase It ensures serialized transactions based on lock
points (the point where a transaction acquired
its final lock).
Lock
Protocol)
Growing Phase Shrinking Phase
Transaction
T begin T end Time
The Two Phase Lock Conversions are as follows:
Growing Phase:
transaction receive Lock - S
Concurrency
Control transaction receive Lock - X
(Two Phase transaction convert Lock - S to Lock - X
Lock Shrinking Phase:
transaction releases Lock - S
Protocol – transaction releases Lock - X
Lock transaction convert Lock - X to Lock – S
Conversions) and release
It ensures serialized transactions.
The Two Phase Lock can be of 2 types:
Strict two phase locking protocol:
A transaction may release all the shared
locks after the Lock Point, but cannot
release any of the exclusive locks until
Concurrency the transaction commits or aborts.
Control It ensures that if the data is being
(Two Phase modified by one transaction, then other
transaction cannot read it until first
transaction commits.
Lock
Protocol) Rigorous two phase locking protocol:
A transaction is not allowed to release
any lock (either shared or exclusive) until
it commits.
Until the transaction commits, other
transaction can not acquire even a
In addition to S and X lock modes, there are three
additional lock modes with multiple granularity:
intention-shared (IS): indicates explicit
locking at a lower level of the tree but only
with shared locks.
intention-exclusive (IX): indicates explicit
locking at a lower level with exclusive or
Concurrency shared locks.
Control shared and intention-exclusive (SIX): the
subtree rooted by that node is locked
T1 ----------------------------
Lock
IX
Compatibili
ty Matrix
S
S IX
X
Data Items are represented in a tree like
structure. It has a parent as well as child.
If a lock is acquired on Parent node, then
automatically, all its children are locked.
If Page2 has been locked by
any transaction, then all its
Concurrency children i.e. Record1, Record
2, Field1 and Field2 will be
locked.
Control
If any transaction requests
lock on any child, then
(Multiple
Granularity) compiler checks whether a
Lock on any parent has not
been given.
At the same time, if other
transaction request lock on
Record2, then lock will not be
granted as its Parent Page 2
has been locked by some other
Transactions send lock and unlock requests to
Lock Manager. All transactions has to wait for
lock manager’s response.
Lock manager responds to the request with
either “Lock Granted” or “Wait” message.
Lock manager keeps all the records of lock
Concurrency grants and pending requests in a Lock Table.
Control Dark rectangles indicate
granted locks, white ones
(Implementing indicate waiting requests.
Locking) It has the record of type of
locks issued on the data
items.
The new requests are listed
in the queue from the
bottom of the table.
Granted
If a transaction releases a
Waiting
A is waiting for
DEADLOC
A K
B and C
C
A C
Wait-Die Approach:
If an older transaction is requesting a
resource which is held by younger
transaction, then older transaction waits.
If an younger transaction is requesting a
Deadlock resource which is held by older
Prevention transaction, then younger transaction is
killed and rolled back.
Wait-Die
O needs a resource held O Waits
by Y
Y needs a resource held Y Dies
by O
A Deadlock can be prevented using:
Wound-Wait Approach:
If an older transaction is requesting a
resource which is held by younger
transaction, then older transaction forces
younger transaction to kill the transaction
Deadlock and releases the resource.
Prevention If an younger transaction is requesting a
resource which is held by older
transaction, then younger transaction waits
till older transaction releases resource.
Wound-Wait
O needs a resource held Y Hurts /
by Y Dies
Y needs a resource held Y Waits
A Deadlock can be prevented using:
Timeout-Based Approach:
A transaction waits for a lock only for a
specified amount of time. After that, the
transaction is rolled back.
So deadlock never occurs.
Deadlock
Prevention
Thanks