0% found this document useful (0 votes)
12 views60 pages

ADBMSUnit3pptx 2023 10 06 09 10 59

The document discusses transactions in database management systems, detailing their operations, states, and the importance of ACID properties for maintaining data integrity. It explains how transactions can be executed, committed, or rolled back, and highlights concurrency issues like dirty reads, unrepeatable reads, and lost updates. Additionally, it covers concurrency control protocols, including locking mechanisms to manage simultaneous transactions effectively.

Uploaded by

parthx932
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views60 pages

ADBMSUnit3pptx 2023 10 06 09 10 59

The document discusses transactions in database management systems, detailing their operations, states, and the importance of ACID properties for maintaining data integrity. It explains how transactions can be executed, committed, or rolled back, and highlights concurrency issues like dirty reads, unrepeatable reads, and lost updates. Additionally, it covers concurrency control protocols, including locking mechanisms to manage simultaneous transactions effectively.

Uploaded by

parthx932
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 60

DIPLOMA –

COMPUTER
ENGINEERING
Unit 3
Advance
Transaction and Database
Management
Concurrency System
(09CE1502)
 Transactions are a set of operations that are used to
perform some logical set of work.

 A transaction is made to change data in a database


which can be done by inserting new data, updating
Transactio the existing data, or by deleting the data that is no
n longer required.

 There are certain types of transaction states which


tell the user about the current condition of that
database transaction and what further steps to be
followed for the processing.
 You might have encountered a situation when your
system got crashed due to some hardware or
software issues and got rebooted to ensure that all
the data is restored in a consistent state.

Introducti  This protection of user's data even in case of a


on of system failure marks as one of the major advantages
of database management system.
Transactio
n  Various transactions are done as a part of
manipulating the data in a database, these
transactions can be seen as a set of operations that
are executed by the user program in DBMS.
 Execution of a similar transaction multiple times will
Introducti lead to the generation of multiple transactions.
on of
Transactio  For example, Withdrawing some amount of money
from the ATM can be seen as a transaction that can
n be done multiple times also.
A certain set of operations takes place when a
transaction is done that is used to perform some logical
set of operations.
For example: When we go to withdraw money from
ATM, we encounter the following set of operations:
Operation  Transaction Initiated

s in  You have to insert an ATM card


 Select your choice of language
Transactio  Select whether savings or current account
n  Enter the amount to withdraw
 Entering your ATM pin
 Transaction processes
 You collect the cash
 You press finish to end transaction
The above mentioned are the set of operations done by
Operation you. But in the case of a transaction in DBMS there are
three major operations that are used for a transaction
s in to get executed in an efficient manner. These are:
Transactio 1. Read/ Access Data
n 2. Write/ Change Data
3. Commit
Let's understand the above three sets of operations in
a transaction with a real-life example of transferring
money from Account1 to Account2.

Operation
Initial balance in both the banks before the start of the
s in transaction
Transactio Account1 - ₹ 5000 Account2 - ₹ 2000
n
This data before the start of the transaction is stored in
the secondary memory (Hard disk) which once initiated
is bought to the primary memory (RAM) of the system
for faster and better access.
Now for a transfer of ₹ 500 from Account1 to Account2
to occur, the following set of operations will take place.
Operation Read (Account1) --> 5000
s in Account1 = Account1 - 500
Transactio Write (Account1) --> 4500

n Read (Account2) --> 2000


Account2 = Account2 + 500
Write (Account2) --> 2500 commit
 The COMMIT statement permanently saves the changes
made by the current transaction. When a transaction is
successful, COMMIT is applied. If the system fails before
a COMMIT is applied, the transaction reaches its
previous state after ROLLBACK.

Operation  After commit operation the transaction ends and


s in updated values of Account1 = ₹ 4500 and Account2 = ₹
2500. Every single operation that occurs before the
Transactio commit is said to be in a partially committed state and is
stored in the primary memory (RAM). After the
n transaction is committed, the updated data is accepted
and updated in the secondary memory (Hard Disk).

 If in some case, the transaction failed anywhere before


committing, then that transaction gets aborted and have
to start from the beginning as it can’t be continued from
the previous state of failure. This is known as Roll Back.
 During the lifetime of a transaction, there are a lot of
states to go through. These states update the
operating system about the current state of the
transaction and also tell the user about how to plan
further processing of the transaction.

 These states decide the regulations which decide the


Transactio fate of a transaction whether it will commit or abort.
 Transaction states are as follows-
n states 1. Active state
2. Partially committed state
3. Committed state
4. Failed state
5. Aborted state
6. Terminated state
 When the operations of a transaction are running
then the transaction is said to be active state.

Active
 If all the read and write operations are performed
state without any error then it progresses to the partially
committed state, if somehow any operation fails, then
it goes to a state known as failed state.
Partially  After all the read and write operations are completed,
the changes which were previously made in the main
Committe memory are now made permanent in the database,
after which the state will progress to committed state
d State but in case of a failure it will go to the failed state.
 If the transaction completes all sets of operations
successfully, all the changes made during the
Committe partially committed state are permanently stored and
d state the transaction is stated to be completed, thus the
transaction can progress to finally get terminated in
the terminated state.
 If any operation during the transaction fails due to
some software or hardware issues, then it goes to the
Failed failed state . The occurrence of a failure during a
state transaction makes a permanent change to data in the
database. The changes made into the local memory
data are rolled back to the previous consistent state.
 If the transaction fails during its execution, it goes
from failed state to aborted state and because in the
previous states all the changes were only made in the
Aborted main memory, these uncommitted changes are either
deleted or rolled back.
state
 The transaction at this point can restart and start
afresh from the active state.
 If the transaction gets aborted after roll-back or the
Terminate transaction comes from the committed state, then the
database comes to a consistent state and is ready for
d state further new transactions since the previous
transaction is now terminated.
Transactio
n states
 It is important to ensure that the database remains
consistent before and after the transaction.
 To ensure the consistency of database, certain properties
are followed by all the transactions occurring in the system.

ACID  These properties are called as ACID Properties of a


transaction.
property
 This property ensures that either the transaction
occurs completely or it does not occur at all.
 In other words, it ensures that no transaction occurs
partially.
Atomicity  That is why, it is also referred to as “All or nothing
rule“.
 It is the responsibility of Transaction Control Manager
to ensure atomicity of the transactions.
 Consider the following transaction T consisting
of T1 and T2: Transfer of 100 from account X to
account Y.

Atomicity
 If the transaction fails after completion of T1 but
before completion of T2.( say, after write(X) but
before write(Y)), then amount has been deducted
from X but not added to Y.
 This results in an inconsistent database state.
Therefore, the transaction must be executed in
entirety in order to ensure correctness of database
state.
 This property ensures that integrity constraints are
maintained.
 In other words, it ensures that the database remains
Consistenc consistent before and after the transaction.
y  It is the responsibility of DBMS and application
programmer to ensure consistency of the database.
Consistenc  Referring to the example above,
y The total amount before and after the transaction
must be maintained.
 Total before T occurs = 500 + 200 = 700.
 Total after T occurs = 400 + 300 = 700.
 Therefore, database is consistent. Inconsistency
occurs in case T1 completes but T2 fails. As a result T
is incomplete.
 This property ensures that multiple transactions can
occur simultaneously without causing any
inconsistency.
 During execution, each transaction feels as if it is
getting executed alone in the system.
 A transaction does not realize that there are other
transactions as well getting executed parallelly.

Isolation  Changes made by a transaction becomes visible to


other transactions only after they are written in the
memory.
 The resultant state of the system after executing all
the transactions is same as the state that would be
achieved if the transactions were executed serially
one after the other.
 It is the responsibility of concurrency control manager
to ensure isolation for all the transactions.
 Let X= 500, Y = 500.
Consider two transactions T and T”.

Isolation  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).
 This results in database inconsistency, due to a loss of
50 units. Hence, transactions must take place in
isolation and changes should be visible only after they
have been made to the main memory.
 This property ensures that all the changes made by a
transaction after its successful execution are written
successfully to the disk.
 It also ensures that these changes exist permanently
Durability and are never lost even if there occurs a failure of any
kind.
 It is the responsibility of recovery manager to ensure
durability in the database.
Database  Database concurrency basically refers to the ability of
Concurren the database to support multiple users and processes
simultaneously.
cy  Concurrency improves system performance and
throughput but not without its side effects.
Concurren  When multiple transactions execute concurrently in
cy an uncontrolled or unrestricted manner, then it might
lead to several problems.
problems  Such problems are called as concurrency problems.
in DBMS
Concurrency problem in
Dirty read

transaction
Concurren
cy Unrepeatable
problems read
in DBMS
Lost update
 The dirty read problem in DBMS occurs when a
transaction reads the data that has been updated by
another transaction that is still uncommitted. It arises
due to multiple uncommitted transactions executing
simultaneously.
 This read is called as dirty read because-
Dirty read  There is always a chance that the uncommitted
problem transaction might roll back later.
 Thus, uncommitted transaction might make other
transactions read a value that does not even
exist.
 This leads to inconsistency of the database.
 Consider two transactions A and B performing
read/write operations on a data DT in the database
DB. The current value of DT is 1000: The following
table shows the read/write operations in A and B
transactions.

Dirty read
problem
 Transaction A reads the value of data DT as 1000 and
modifies it to 1500 which gets stored in the
temporary buffer.

Dirty read  The transaction B reads the data DT as 1500 and


commits it and the value of DT permanently gets
problem changed to 1500 in the database DB.
 Then some server errors occur in transaction A and it
wants to get rollback to its initial value, i.e., 1000 and
then the dirty read problem occurs.
Unrepeata  The unrepeatable read problem occurs when two or
ble read more different values of the same data are read
during the read operations in the same transaction.
problem
 Consider two transactions A and B performing
read/write operations on a data DT in the database
DB. The current value of DT is 1000: The following
table shows the read/write operations in A and B
transactions.

Unrepeata
ble read
problem
 Transaction A and B initially read the value of DT as
1000.
Unrepeata  Transaction A modifies the value of DT from 1000 to
ble read 1500 and then again transaction B reads the value
and finds it to be 1500.
problem  Transaction B finds two different values of DT in its
two different read operations.
Lost  The Lost Update problem arises when an update in
update the data is done over another update but by two
different transactions.
problem
 Example: Consider two transactions A and B
performing read/write operations on a data DT in the
database DB. The current value of DT is 1000: The
following table shows the read/write operations in A
and B transactions.

Lost
update
problem
 Transaction A initially reads the value of DT as 1000.

Lost  Transaction A modifies the value of DT from 1000 to


1500 and then again transaction B modifies the value
update to 1800.

problem  Transaction A again reads DT and finds 1800 in DT


and therefore the update done by transaction A has
been lost.
 Concurrency Control is the working concept that is
required for controlling and managing the concurrent
Concurren execution of database operations and thus avoiding
the inconsistencies in the database.
cy control  Thus, for maintaining the concurrency of the
database, we have the concurrency control protocols.
Concurren • Lock Based Concurrency Control Protocol
cy control • Time Stamp Concurrency Control Protocol

protocols • Validation Based Concurrency Control Protocol


 In this type of protocol, any transaction cannot read or
write data until it acquires an appropriate lock on it.
There are two types of lock:

Shared Lock (S):


 It is also known as a Read-only lock. In a shared lock, the
Locking data item can only read by the transaction.
based  It can be shared between the transactions because
when the transaction holds a lock, then it can't update
protocol the data on the data item.
Exclusive Lock (X):
 In the exclusive lock, the data item can be both reads as
well as written by the transaction.
 This lock is exclusive, and in this lock, multiple
transactions do not modify the same data
simultaneously.
 Any number of transactions can hold shared locks on
an item, but if any transaction holds an exclusive(X)
on the item no other transaction may hold any lock
Locking on the item.
based  If a lock cannot be granted, the requesting
transaction is made to wait till all incompatible locks
protocol held by other transactions have been released. Then
the lock is granted.
 Simplistic lock protocol
Types of  Pre-claiming Lock Protocol
locking  Two-phase locking (2PL)
protocol  Strict Two-phase locking (Strict-2PL)
 It is the simplest way of locking the data while
Simplistic transaction.
lock  Simplistic lock-based protocols allow all the
transactions to get the lock on the data before insert
protocol or delete or update on it.
 It will unlock the data item after completing the
transaction.
 Pre-claiming Lock Protocols evaluate the transaction
to list all the data items on which they need locks.

Pre-  Before initiating an execution of the transaction, it


requests DBMS for all the lock on all those data items.
claiming  If all the locks are granted then this protocol allows
Lock the transaction to begin. When the transaction is
completed then it releases all the lock.
Protocol  If all the locks are not granted then this protocol
allows the transaction to rolls back and waits until all
the locks are granted.
 The two-phase locking protocol divides the execution
phase of the transaction into three parts.
 In the first part, when the execution of the transaction
Two phase starts, it seeks permission for the lock it requires.
locking  In the second part, the transaction acquires all the
locks. The third phase is started as soon as the
protocol transaction releases its first lock.
 In the third phase, the transaction cannot demand
any new locks. It only releases the acquired locks.
 There are two phases of 2PL:

Growing phase: In the growing phase, a new lock on the


data item may be acquired by the transaction, but none can
be released.

Two phase Shrinking phase: In the shrinking phase, existing lock


locking held by the transaction may be released, but no new locks
can be acquired.
protocol
 In the below example, if lock conversion is allowed then
the following phase can happen:
1. Upgrading of lock (from S(a) to X (a)) is allowed in
growing phase.
2. Downgrading of lock (from X(a) to S(a)) must be done in
shrinking phase.
Time T1 T2
T1 LOCK-S (A)
T2 LOCK-S (A)

Two phase T3 LOCK-X(B)

locking T4 …… ……

protocol T5
T6
UNLOCK (A)
LOCK-X (C)
T7 UNLOCK (B)
T8 UNLOCK (A)
T9 UNLOCK (C)
T10 …… ……
 This is just a skeleton transaction which shows how
unlocking and locking works with 2-PL. Note for:

Transaction T1:
 Growing Phase is from steps 1-3.

Two phase  Shrinking Phase is from steps 4-7.


 Lock Point at 3
locking
protocol Transaction T2:
 Growing Phase is from steps 2-6.
 Shrinking Phase is from steps 8-9.
 Lock Point at 6
 The first phase of Strict-2PL is similar to 2PL. In the
Strict Two- first phase, after acquiring all the locks, the
transaction continues to execute normally.
phase  The only difference between 2PL and strict 2PL is that
locking Strict-2PL does not release a lock after using it.
 Strict-2PL waits until the whole transaction to commit,
(Strict- and then it releases all the locks at a time.
2PL)  Strict-2PL protocol does not have shrinking phase of
lock release.
 The Timestamp Ordering Protocol is used to order the
Time transactions based on their Timestamps. The order of
transaction is nothing but the ascending order of the
Stamp transaction creation.
Concurren  The priority of the older transaction is higher that's
why it executes first. To determine the timestamp of
cy Control the transaction, this protocol uses system time or
logical counter.
Protocol
 The lock-based protocol is used to manage the order
between conflicting pairs among transactions at the
execution time. But Timestamp based protocols start
Time working as soon as a transaction is created.
Stamp  Let's assume there are two transactions T1 and T2.
Suppose the transaction T1 has entered the system at
Concurren 010 times and transaction T2 has entered the system
cy Control at 016 times. T1 has the higher priority, so it executes
first as it is entered the system first.
Protocol  The timestamp ordering protocol also maintains the
timestamp of last 'read' and 'write' operation on a
data.
 An algorithm must ensure that, for each item
Time accessed by Conflicting Operations in the schedule,
the order in which the item is accessed does not
Stamp violate the ordering.
Concurren  To ensure this, use two Timestamp Values relating to
each database item X.
cy Control 1. W­_TS(X) is the largest timestamp of any transaction
Protocol that executed write(X) successfully.
2. R_TS(X) is the largest timestamp of any transaction
that executed read(X) successfully.
 Basic Timestamp Ordering –
1. Whenever a Transaction T issues a W_item(X)
operation, check the following conditions:

Time If R_TS(X) > TS(T) and if W_TS(X) > TS(T), then


abort and rollback T and reject the operation. else,
Stamp Execute W_item(X) operation of T and set
Concurren W_TS(X) to TS(T).

cy Control 2. Whenever a Transaction T issues a R_item(X)


operation, check
Protocol the following conditions:
If W_TS(X) > TS(T), then abort and reject T and
reject the operation, else
If W_TS(X) <= TS(T), then execute the
R_item(X) operation of T and set R_TS(X) to the larger
of TS(T) and current R_TS(X).
Validation  Validation phase is also known as optimistic
Based concurrency control technique. In the validation
based protocol, the transaction is executed in the
Concurren following three phases:

cy Control 1. Read Phase

Protocol 2. Validation Phase


3. Write Phase
 In this phase, the transaction T is read and executed.
Read It is used to read the value of various data items and
stores them in temporary local variables. It can
Phase perform all the write operations on temporary
variables without an update to the actual database.
Validation  In this phase, the temporary variable value will be
validated against the actual data to see if it violates
Phase the serializability.
Write  If the validation of the transaction is validated, then
the temporary results are written to the database or
Phase system otherwise the transaction is rolled back.
 Here each phase has the following different
timestamps:

Validation 1. Start(Ti): It contains the time when Ti started its


Based execution.
Concurren 2. Validation (Ti): It contains the time when Ti finishes
its read phase and starts its validation phase.
cy Control 3. Finish(Ti): It contains the time when Ti finishes its
Protocol write phase.
 This protocol is used to determine the time stamp for
the transaction for serialization using the time stamp
of the validation phase, as it is the actual phase which
Validation determines if the transaction will commit or rollback.

Based  Hence TS(T) = validation(T).


 The serializability is determined during the validation
Concurren process. It can't be decided in advance.
cy Control  While executing the transaction, it ensures a greater
degree of concurrency and also less number of
Protocol conflicts.
 Thus it contains transactions which have less number
of rollbacks.
Thank You…

You might also like